comparison mcabber/src/utils.c @ 24:e88b15cbf2de

[/trunk] Changeset 40 by mikael * Change structure -> src directory for mcabber source code...
author mikael
date Sun, 27 Mar 2005 20:16:02 +0000
parents
children a8f8492abd44
comparison
equal deleted inserted replaced
23:d7107507424b 24:e88b15cbf2de
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, "Debugging mode started...\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, 64);
31
32 ahora = time(NULL);
33 strftime(buffer, 64, "[%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 *message, int *nsubmsgs, unsigned int maxlong)
46 {
47 /* BUGs: recorta la palabra si la longitud maxlong es menor que la palabra
48 // maxlong = 4
49 // message = "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(message) * 2);
59 int maxnlines = 50;
60 int i = 0;
61
62 submsgs = (char **) malloc(maxnlines * sizeof(char *));
63
64 running = strdup(message);
65 aux2 = strdup(message);
66
67 aux = index(running, '\n'); /* is there a CR? */
68
69 while ((strlen(aux2) > maxlong) || (aux)) {
70 memset(buffer, 0, strlen(message) * 2);
71 running[maxlong] = '\0'; /* cut at $maxlong chars */
72
73 if (aux != NULL) { /* there is a CR */
74 strncpy(buffer, running, strlen(running) - strlen(aux));
75 } else {
76 aux = rindex(running, ' '); /* look for last blank */
77 if (aux != NULL) /* hay blanco! */
78 strncpy(buffer, running, strlen(running) - strlen(aux));
79 else
80 strcpy(buffer, running); /* se supone que esto es pa evitar el bug explicado arriba, pero no rula */
81 }
82
83 submsgs[i] = (char *) malloc(strlen(buffer) + 1); /*reservamos memoria */
84 strcpy(submsgs[i], buffer); /*copiamos el buffer de arriba */
85 i++; /*aumentamos numero de mensajillos */
86 aux2 += strlen(buffer) + 1; /*eliminamos texto particionado */
87 sprintf(running, "%s", aux2); /*y lo copiamos de nuevo a la string de "curro" */
88
89 // Check if we have allocated enough space
90 if (i >= maxnlines) {
91 maxnlines += 50;
92 submsgs = (char **) realloc(submsgs, maxnlines * sizeof(char *));
93 }
94 aux = index(running, '\n'); /* is there is a CR now? */
95 }
96 /* last part of the message */
97 if (strlen(aux2) > 0) {
98 submsgs[i] = (char *) malloc(strlen(aux2) + 1);
99 strcpy(submsgs[i], aux2);
100 i++;
101 }
102 (*nsubmsgs) = i;
103 free(buffer);
104 return submsgs;
105 }
106
107 /* Desc: get the rightmost substring
108 *
109 * In : string, match
110 * Out : ptr to substring (or NULL if not found)
111 *
112 * Note: this one has no namespace, cos it belongs to <string.h>
113 */
114 char *ut_strrstr(const char *s1, const char *s2)
115 {
116 int l = strlen(s2);
117
118 if (l) {
119 const char *s = strchr(s1, '\0') - l;
120 while (s >= s1) {
121 if (*s == *s2) {
122 int _l = l - 1;
123 const char *_s = s + 1, *_s2 = s2 + 1;
124 while (_l) {
125 if (*_s++ != *_s2++) {
126 break;
127 }
128 _l--;
129 }
130 if (!_l) {
131 return (char *) s;
132 }
133 }
134 s--;
135 }
136 }
137
138 return NULL;
139 }
140
141 char *gettag(char *buffer, char *what)
142 {
143 char *aux;
144 char *aux2;
145 char *result = (char *) malloc(1024);
146 char *tmp = (char *) malloc(1024);
147 memset(result, 0, 1024);
148 memset(tmp, 0, 1024);
149
150 sprintf(tmp, "<%s>", what);
151 aux = strstr(buffer, tmp);
152 if (aux) {
153 aux += strlen(tmp);
154 sprintf(tmp, "</%s>", what);
155 aux2 = strstr(aux, tmp);
156 if (aux2) {
157 strncpy(result, aux, strlen(aux) - strlen(aux2));
158 free(tmp);
159 return result;
160 }
161 }
162 free(tmp);
163 free(result);
164 return "UNKtag";
165 }
166
167
168 char *getattr(char *buffer, char *what)
169 {
170 char *aux;
171 char *aux2;
172 char *result = (char *) malloc(1024);
173 memset(result, 0, 1024);
174
175 aux = strstr(buffer, what);
176 if (aux) {
177 aux += strlen(what);
178 aux2 = strstr(aux, "'");
179 if (aux2) {
180 strncpy(result, aux, strlen(aux) - strlen(aux2));
181 return result;
182 }
183 }
184 free(result);
185 return "UNKattr";
186 }
187
188 void ut_CenterMessage(char *text, int width, char *output)
189 {
190 char *blank;
191 int ntest, nn;
192
193 memset(output, 0, strlen(output));
194
195 ntest = (width - strlen(text)) / 2;
196 blank = (char *) malloc(ntest + 1);
197
198 for (nn = 0; nn < ntest; nn++)
199 blank[nn] = ' ';
200 blank[ntest] = '\0';
201
202 strcpy(output, blank);
203 strcat(output, text);
204 strcat(output, blank);
205
206 free(blank);
207 }