0
|
1 #include <stdio.h> |
|
2 #include <string.h> |
|
3 #include <stdlib.h> |
|
4 #include <ncurses.h> |
|
5 #include <stdarg.h> |
|
6 #include <time.h> |
|
7 |
|
8 /* Variables globales a UTILS.C */ |
|
9 int DebugEnabled = 0; |
|
10 |
|
11 void ut_InitDebug(int level) |
|
12 { |
|
13 FILE *fp = fopen("/tmp/mcabberlog", "w"); |
|
14 |
|
15 DebugEnabled = level; |
|
16 fprintf(fp, "Debug inicializado...\n" |
|
17 "-----------------------------------\n"); |
|
18 fclose(fp); |
|
19 } |
|
20 |
|
21 void ut_WriteLog(const char *fmt, ...) |
|
22 { |
|
23 FILE *fp = NULL; |
|
24 time_t ahora; |
|
25 va_list ap; |
|
26 char *buffer = NULL; |
|
27 |
|
28 if (DebugEnabled) { |
|
29 fp = fopen("/tmp/mcabberlog", "a+"); |
5
|
30 buffer = (char *) calloc(1, 64); |
0
|
31 |
|
32 ahora = time(NULL); |
5
|
33 strftime(buffer, 64, "[%H:%M:%S] ", localtime(&ahora)); |
0
|
34 fprintf(fp, "%s", buffer); |
|
35 |
|
36 va_start(ap, fmt); |
|
37 vfprintf(fp, fmt, ap); |
|
38 va_end(ap); |
|
39 |
|
40 free(buffer); |
|
41 fclose(fp); |
|
42 } |
|
43 } |
|
44 |
5
|
45 char **ut_SplitMessage(char *message, int *nsubmsgs, unsigned int maxlong) |
0
|
46 { |
|
47 /* BUGs: recorta la palabra si la longitud maxlong es menor que la palabra |
|
48 // maxlong = 4 |
5
|
49 // message = "peaso bug!" |
0
|
50 // submsgs[0] = "peas" |
|
51 // submsgs[1] = "bug!" |
|
52 // por lo demas, rula de arte. De todos modos, podrias verificarla ??? |
|
53 */ |
|
54 char *running; |
|
55 char *aux; |
|
56 char *aux2; |
|
57 char **submsgs; |
5
|
58 char *buffer = (char *) malloc(strlen(message) * 2); |
0
|
59 int i = 0; |
|
60 |
|
61 submsgs = (char **) malloc(50 * sizeof(char *)); /* limitamos, a priori, el maximo de lineas devueltas... */ |
|
62 |
5
|
63 running = strdup(message); |
|
64 aux2 = strdup(message); |
|
65 |
|
66 aux = index(running, '\n'); /* is there a CR? */ |
|
67 |
|
68 while ((strlen(aux2) > maxlong) || (aux)) { |
|
69 memset(buffer, 0, strlen(message) * 2); |
|
70 running[maxlong] = '\0'; /* cut at $maxlong chars */ |
|
71 |
|
72 if (aux != NULL) { /* there is a CR */ |
0
|
73 strncpy(buffer, running, strlen(running) - strlen(aux)); |
5
|
74 } else { |
|
75 aux = rindex(running, ' '); /* look for last blank */ |
|
76 if (aux != NULL) /* hay blanco! */ |
|
77 strncpy(buffer, running, strlen(running) - strlen(aux)); |
|
78 else |
|
79 strcpy(buffer, running); /* se supone que esto es pa evitar el bug explicado arriba, pero no rula */ |
|
80 } |
0
|
81 |
|
82 submsgs[i] = (char *) malloc(strlen(buffer) + 1); /*reservamos memoria */ |
|
83 strcpy(submsgs[i], buffer); /*copiamos el buffer de arriba */ |
|
84 i++; /*aumentamos numero de mensajillos */ |
|
85 aux2 += strlen(buffer) + 1; /*eliminamos texto particionado */ |
|
86 sprintf(running, "%s", aux2); /*y lo copiamos de nuevo a la string de "curro" */ |
5
|
87 aux = index(running, '\n'); /* is there is a CR now? */ |
0
|
88 } |
5
|
89 /* last part of the message */ |
0
|
90 if (strlen(aux2) > 0) { |
|
91 submsgs[i] = (char *) malloc(strlen(aux2) + 1); |
|
92 strcpy(submsgs[i], aux2); |
|
93 i++; |
|
94 } |
|
95 (*nsubmsgs) = i; |
|
96 free(buffer); |
|
97 return submsgs; |
|
98 } |
|
99 |
|
100 /* Desc: get the rightmost substring |
|
101 * |
|
102 * In : string, match |
|
103 * Out : ptr to substring (or NULL if not found) |
|
104 * |
|
105 * Note: this one has no namespace, cos it belongs to <string.h> |
|
106 */ |
|
107 char *ut_strrstr(const char *s1, const char *s2) |
|
108 { |
|
109 int l = strlen(s2); |
|
110 |
|
111 if (l) { |
|
112 const char *s = strchr(s1, '\0') - l; |
|
113 while (s >= s1) { |
|
114 if (*s == *s2) { |
|
115 int _l = l - 1; |
|
116 const char *_s = s + 1, *_s2 = s2 + 1; |
|
117 while (_l) { |
|
118 if (*_s++ != *_s2++) { |
|
119 break; |
|
120 } |
|
121 _l--; |
|
122 } |
|
123 if (!_l) { |
|
124 return (char *) s; |
|
125 } |
|
126 } |
|
127 s--; |
|
128 } |
|
129 } |
|
130 |
|
131 return NULL; |
|
132 } |
|
133 |
|
134 char *gettag(char *buffer, char *what) |
|
135 { |
|
136 char *aux; |
|
137 char *aux2; |
|
138 char *result = (char *) malloc(1024); |
|
139 char *tmp = (char *) malloc(1024); |
|
140 memset(result, 0, 1024); |
|
141 memset(tmp, 0, 1024); |
|
142 |
|
143 sprintf(tmp, "<%s>", what); |
|
144 aux = strstr(buffer, tmp); |
|
145 if (aux) { |
|
146 aux += strlen(tmp); |
|
147 sprintf(tmp, "</%s>", what); |
|
148 aux2 = strstr(aux, tmp); |
|
149 if (aux2) { |
|
150 strncpy(result, aux, strlen(aux) - strlen(aux2)); |
|
151 free(tmp); |
|
152 return result; |
|
153 } |
|
154 } |
|
155 free(tmp); |
|
156 free(result); |
|
157 return "UNKtag"; |
|
158 } |
|
159 |
|
160 |
|
161 char *getattr(char *buffer, char *what) |
|
162 { |
|
163 char *aux; |
|
164 char *aux2; |
|
165 char *result = (char *) malloc(1024); |
|
166 memset(result, 0, 1024); |
|
167 |
|
168 aux = strstr(buffer, what); |
|
169 if (aux) { |
|
170 aux += strlen(what); |
|
171 aux2 = strstr(aux, "'"); |
|
172 if (aux2) { |
|
173 strncpy(result, aux, strlen(aux) - strlen(aux2)); |
|
174 return result; |
|
175 } |
|
176 } |
|
177 free(result); |
|
178 return "UNKattr"; |
|
179 } |
|
180 |
|
181 void ut_CenterMessage(char *text, int width, char *output) |
|
182 { |
|
183 char *blank; |
|
184 int ntest, nn; |
|
185 |
|
186 memset(output, 0, strlen(output)); |
|
187 |
|
188 ntest = (width - strlen(text)) / 2; |
|
189 blank = (char *) malloc(ntest + 1); |
|
190 |
|
191 for (nn = 0; nn < ntest; nn++) |
|
192 blank[nn] = ' '; |
|
193 blank[ntest] = '\0'; |
|
194 |
|
195 strcpy(output, blank); |
|
196 strcat(output, text); |
|
197 strcat(output, blank); |
|
198 |
|
199 free(blank); |
|
200 } |