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+"); |
|
30 buffer = (char *) calloc(1, 8192); |
|
31 |
|
32 ahora = time(NULL); |
|
33 strftime(buffer, 1024, "[%H:%M:%S] ", localtime(&ahora)); |
|
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 |
|
45 char **ut_SplitMessage(char *mensaje, int *nsubmsgs, unsigned int maxlong) |
|
46 { |
|
47 /* BUGs: recorta la palabra si la longitud maxlong es menor que la palabra |
|
48 // maxlong = 4 |
|
49 // mensaje = "peaso bug!" |
|
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; |
|
58 char *buffer = (char *) malloc(strlen(mensaje) * 2); |
|
59 int i = 0; |
|
60 |
|
61 submsgs = (char **) malloc(50 * sizeof(char *)); /* limitamos, a priori, el maximo de lineas devueltas... */ |
|
62 |
|
63 running = strdup(mensaje); /* duplicamos mensaje */ |
|
64 aux2 = strdup(mensaje); /* hacemos otra copia */ |
|
65 while (strlen(aux2) > maxlong) { /* mintras quede texto... */ |
|
66 memset(buffer, 0, strlen(mensaje) * 2); /* borramos el buffer */ |
|
67 running[maxlong] = '\0'; /* cortamos la cadena a la maxima longitud */ |
|
68 aux = rindex(running, ' '); /* posicion del ultimo blanco */ |
|
69 if (aux != NULL) /* hay blanco! */ |
|
70 strncpy(buffer, running, strlen(running) - strlen(aux)); |
|
71 else |
|
72 strcpy(buffer, running); /* se supone que esto es pa evitar el bug explicado arriba, pero no rula */ |
|
73 |
|
74 submsgs[i] = (char *) malloc(strlen(buffer) + 1); /*reservamos memoria */ |
|
75 strcpy(submsgs[i], buffer); /*copiamos el buffer de arriba */ |
|
76 i++; /*aumentamos numero de mensajillos */ |
|
77 aux2 += strlen(buffer) + 1; /*eliminamos texto particionado */ |
|
78 sprintf(running, "%s", aux2); /*y lo copiamos de nuevo a la string de "curro" */ |
|
79 } |
|
80 /* la ultima parte del mensaje, si la hay ;-) */ |
|
81 if (strlen(aux2) > 0) { |
|
82 submsgs[i] = (char *) malloc(strlen(aux2) + 1); |
|
83 strcpy(submsgs[i], aux2); |
|
84 i++; |
|
85 } |
|
86 (*nsubmsgs) = i; |
|
87 free(buffer); |
|
88 return submsgs; |
|
89 } |
|
90 |
|
91 /* Desc: get the rightmost substring |
|
92 * |
|
93 * In : string, match |
|
94 * Out : ptr to substring (or NULL if not found) |
|
95 * |
|
96 * Note: this one has no namespace, cos it belongs to <string.h> |
|
97 */ |
|
98 char *ut_strrstr(const char *s1, const char *s2) |
|
99 { |
|
100 int l = strlen(s2); |
|
101 |
|
102 if (l) { |
|
103 const char *s = strchr(s1, '\0') - l; |
|
104 while (s >= s1) { |
|
105 if (*s == *s2) { |
|
106 int _l = l - 1; |
|
107 const char *_s = s + 1, *_s2 = s2 + 1; |
|
108 while (_l) { |
|
109 if (*_s++ != *_s2++) { |
|
110 break; |
|
111 } |
|
112 _l--; |
|
113 } |
|
114 if (!_l) { |
|
115 return (char *) s; |
|
116 } |
|
117 } |
|
118 s--; |
|
119 } |
|
120 } |
|
121 |
|
122 return NULL; |
|
123 } |
|
124 |
|
125 char *gettag(char *buffer, char *what) |
|
126 { |
|
127 char *aux; |
|
128 char *aux2; |
|
129 char *result = (char *) malloc(1024); |
|
130 char *tmp = (char *) malloc(1024); |
|
131 memset(result, 0, 1024); |
|
132 memset(tmp, 0, 1024); |
|
133 |
|
134 sprintf(tmp, "<%s>", what); |
|
135 aux = strstr(buffer, tmp); |
|
136 if (aux) { |
|
137 aux += strlen(tmp); |
|
138 sprintf(tmp, "</%s>", what); |
|
139 aux2 = strstr(aux, tmp); |
|
140 if (aux2) { |
|
141 strncpy(result, aux, strlen(aux) - strlen(aux2)); |
|
142 free(tmp); |
|
143 return result; |
|
144 } |
|
145 } |
|
146 free(tmp); |
|
147 free(result); |
|
148 return "UNKtag"; |
|
149 } |
|
150 |
|
151 |
|
152 char *getattr(char *buffer, char *what) |
|
153 { |
|
154 char *aux; |
|
155 char *aux2; |
|
156 char *result = (char *) malloc(1024); |
|
157 memset(result, 0, 1024); |
|
158 |
|
159 aux = strstr(buffer, what); |
|
160 if (aux) { |
|
161 aux += strlen(what); |
|
162 aux2 = strstr(aux, "'"); |
|
163 if (aux2) { |
|
164 strncpy(result, aux, strlen(aux) - strlen(aux2)); |
|
165 return result; |
|
166 } |
|
167 } |
|
168 free(result); |
|
169 return "UNKattr"; |
|
170 } |
|
171 |
|
172 void ut_CenterMessage(char *text, int width, char *output) |
|
173 { |
|
174 char *blank; |
|
175 int ntest, nn; |
|
176 |
|
177 memset(output, 0, strlen(output)); |
|
178 |
|
179 ntest = (width - strlen(text)) / 2; |
|
180 blank = (char *) malloc(ntest + 1); |
|
181 |
|
182 for (nn = 0; nn < ntest; nn++) |
|
183 blank[nn] = ' '; |
|
184 blank[ntest] = '\0'; |
|
185 |
|
186 strcpy(output, blank); |
|
187 strcat(output, text); |
|
188 strcat(output, blank); |
|
189 |
|
190 free(blank); |
|
191 } |