comparison mcabber/utils.c @ 5:4dcabf02f474

[/trunk] Changeset 20 by mikael * Correctly split multi-lines messsages * Don't use an 8kB buffer needlessly for wrtiting debug log
author mikael
date Sat, 19 Mar 2005 11:09:36 +0000
parents b3b2332715fb
children 4c1affca7aea
comparison
equal deleted inserted replaced
4:eb31418b48df 5:4dcabf02f474
25 va_list ap; 25 va_list ap;
26 char *buffer = NULL; 26 char *buffer = NULL;
27 27
28 if (DebugEnabled) { 28 if (DebugEnabled) {
29 fp = fopen("/tmp/mcabberlog", "a+"); 29 fp = fopen("/tmp/mcabberlog", "a+");
30 buffer = (char *) calloc(1, 8192); 30 buffer = (char *) calloc(1, 64);
31 31
32 ahora = time(NULL); 32 ahora = time(NULL);
33 strftime(buffer, 1024, "[%H:%M:%S] ", localtime(&ahora)); 33 strftime(buffer, 64, "[%H:%M:%S] ", localtime(&ahora));
34 fprintf(fp, "%s", buffer); 34 fprintf(fp, "%s", buffer);
35 35
36 va_start(ap, fmt); 36 va_start(ap, fmt);
37 vfprintf(fp, fmt, ap); 37 vfprintf(fp, fmt, ap);
38 va_end(ap); 38 va_end(ap);
40 free(buffer); 40 free(buffer);
41 fclose(fp); 41 fclose(fp);
42 } 42 }
43 } 43 }
44 44
45 char **ut_SplitMessage(char *mensaje, int *nsubmsgs, unsigned int maxlong) 45 char **ut_SplitMessage(char *message, int *nsubmsgs, unsigned int maxlong)
46 { 46 {
47 /* BUGs: recorta la palabra si la longitud maxlong es menor que la palabra 47 /* BUGs: recorta la palabra si la longitud maxlong es menor que la palabra
48 // maxlong = 4 48 // maxlong = 4
49 // mensaje = "peaso bug!" 49 // message = "peaso bug!"
50 // submsgs[0] = "peas" 50 // submsgs[0] = "peas"
51 // submsgs[1] = "bug!" 51 // submsgs[1] = "bug!"
52 // por lo demas, rula de arte. De todos modos, podrias verificarla ??? 52 // por lo demas, rula de arte. De todos modos, podrias verificarla ???
53 */ 53 */
54 char *running; 54 char *running;
55 char *aux; 55 char *aux;
56 char *aux2; 56 char *aux2;
57 char **submsgs; 57 char **submsgs;
58 char *buffer = (char *) malloc(strlen(mensaje) * 2); 58 char *buffer = (char *) malloc(strlen(message) * 2);
59 int i = 0; 59 int i = 0;
60 60
61 submsgs = (char **) malloc(50 * sizeof(char *)); /* limitamos, a priori, el maximo de lineas devueltas... */ 61 submsgs = (char **) malloc(50 * sizeof(char *)); /* limitamos, a priori, el maximo de lineas devueltas... */
62 62
63 running = strdup(mensaje); /* duplicamos mensaje */ 63 running = strdup(message);
64 aux2 = strdup(mensaje); /* hacemos otra copia */ 64 aux2 = strdup(message);
65 while (strlen(aux2) > maxlong) { /* mintras quede texto... */ 65
66 memset(buffer, 0, strlen(mensaje) * 2); /* borramos el buffer */ 66 aux = index(running, '\n'); /* is there a CR? */
67 running[maxlong] = '\0'; /* cortamos la cadena a la maxima longitud */ 67
68 aux = rindex(running, ' '); /* posicion del ultimo blanco */ 68 while ((strlen(aux2) > maxlong) || (aux)) {
69 if (aux != NULL) /* hay blanco! */ 69 memset(buffer, 0, strlen(message) * 2);
70 running[maxlong] = '\0'; /* cut at $maxlong chars */
71
72 if (aux != NULL) { /* there is a CR */
70 strncpy(buffer, running, strlen(running) - strlen(aux)); 73 strncpy(buffer, running, strlen(running) - strlen(aux));
71 else 74 } else {
72 strcpy(buffer, running); /* se supone que esto es pa evitar el bug explicado arriba, pero no rula */ 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 }
73 81
74 submsgs[i] = (char *) malloc(strlen(buffer) + 1); /*reservamos memoria */ 82 submsgs[i] = (char *) malloc(strlen(buffer) + 1); /*reservamos memoria */
75 strcpy(submsgs[i], buffer); /*copiamos el buffer de arriba */ 83 strcpy(submsgs[i], buffer); /*copiamos el buffer de arriba */
76 i++; /*aumentamos numero de mensajillos */ 84 i++; /*aumentamos numero de mensajillos */
77 aux2 += strlen(buffer) + 1; /*eliminamos texto particionado */ 85 aux2 += strlen(buffer) + 1; /*eliminamos texto particionado */
78 sprintf(running, "%s", aux2); /*y lo copiamos de nuevo a la string de "curro" */ 86 sprintf(running, "%s", aux2); /*y lo copiamos de nuevo a la string de "curro" */
79 } 87 aux = index(running, '\n'); /* is there is a CR now? */
80 /* la ultima parte del mensaje, si la hay ;-) */ 88 }
89 /* last part of the message */
81 if (strlen(aux2) > 0) { 90 if (strlen(aux2) > 0) {
82 submsgs[i] = (char *) malloc(strlen(aux2) + 1); 91 submsgs[i] = (char *) malloc(strlen(aux2) + 1);
83 strcpy(submsgs[i], aux2); 92 strcpy(submsgs[i], aux2);
84 i++; 93 i++;
85 } 94 }