[7d4fbcd] | 1 | #include <stdio.h> |
---|
| 2 | #include <string.h> |
---|
| 3 | #include <stdlib.h> |
---|
[995eb4b] | 4 | #include <ctype.h> |
---|
[7d4fbcd] | 5 | #include "owl.h" |
---|
| 6 | |
---|
[1aee7d9] | 7 | static const char fileIdent[] = "$Id$"; |
---|
| 8 | |
---|
[2de4f20] | 9 | /* start with line aline (where the first line is 1) and print 'lines' |
---|
| 10 | * lines |
---|
| 11 | */ |
---|
[ab31454] | 12 | int owl_text_truncate_lines(char *out, char *in, int aline, int lines) |
---|
| 13 | { |
---|
[7d4fbcd] | 14 | char *ptr1, *ptr2; |
---|
| 15 | int i; |
---|
| 16 | |
---|
| 17 | strcpy(out, ""); |
---|
| 18 | |
---|
| 19 | if (aline==0) aline=1; /* really illegal use */ |
---|
| 20 | |
---|
| 21 | /* find the starting line */ |
---|
| 22 | ptr1=in; |
---|
| 23 | if (aline!=1) { |
---|
| 24 | for (i=0; i<aline-1; i++) { |
---|
| 25 | ptr1=strchr(ptr1, '\n'); |
---|
| 26 | if (!ptr1) return(-1); |
---|
| 27 | ptr1++; |
---|
| 28 | } |
---|
| 29 | } |
---|
| 30 | /* ptr1 now holds the starting point */ |
---|
| 31 | |
---|
| 32 | /* copy in the next 'lines' lines */ |
---|
| 33 | if (lines<1) return(-1); |
---|
| 34 | |
---|
| 35 | for (i=0; i<lines; i++) { |
---|
| 36 | ptr2=strchr(ptr1, '\n'); |
---|
| 37 | if (!ptr2) { |
---|
| 38 | strcat(out, ptr1); |
---|
| 39 | return(-1); |
---|
| 40 | } |
---|
| 41 | strncat(out, ptr1, ptr2-ptr1+1); |
---|
| 42 | ptr1=ptr2+1; |
---|
| 43 | } |
---|
| 44 | return(0); |
---|
| 45 | } |
---|
| 46 | |
---|
[2de4f20] | 47 | |
---|
| 48 | /* the first column is column 0. The message is expected to end in a |
---|
| 49 | * new line for now */ |
---|
[ab31454] | 50 | void owl_text_truncate_cols(char *out, char *in, int acol, int bcol) |
---|
| 51 | { |
---|
[28ee32b] | 52 | char *ptr_s, *ptr_e, *ptr_c, *tmpbuff, *last; |
---|
[dd24b6a] | 53 | int col, cnt, padding; |
---|
[28ee32b] | 54 | |
---|
[7d4fbcd] | 55 | tmpbuff=owl_malloc(strlen(in)+20); |
---|
| 56 | |
---|
| 57 | strcpy(tmpbuff, ""); |
---|
| 58 | last=in+strlen(in)-1; |
---|
[28ee32b] | 59 | ptr_s=in; |
---|
| 60 | while (ptr_s<last) { |
---|
| 61 | ptr_e=strchr(ptr_s, '\n'); |
---|
| 62 | if (!ptr_e) { |
---|
[7d4fbcd] | 63 | /* but this shouldn't happen if we end in a \n */ |
---|
| 64 | break; |
---|
| 65 | } |
---|
| 66 | |
---|
[28ee32b] | 67 | if (ptr_e==ptr_s) { |
---|
[7d4fbcd] | 68 | strcat(tmpbuff, "\n"); |
---|
[28ee32b] | 69 | ptr_s++; |
---|
[7d4fbcd] | 70 | continue; |
---|
| 71 | } |
---|
| 72 | |
---|
[28ee32b] | 73 | col = 0; |
---|
| 74 | cnt = 0; |
---|
[dd24b6a] | 75 | padding = 0; |
---|
[28ee32b] | 76 | ptr_c = ptr_s; |
---|
| 77 | while(col < bcol && ptr_c < ptr_e) { |
---|
| 78 | gunichar c = g_utf8_get_char(ptr_c); |
---|
[47519e1b] | 79 | if (col + mk_wcwidth(c) > bcol) break; |
---|
| 80 | col += mk_wcwidth(c); |
---|
[dd24b6a] | 81 | ptr_c = g_utf8_next_char(ptr_c); |
---|
| 82 | if (col >= acol) { |
---|
| 83 | if (cnt == 0) { |
---|
| 84 | ptr_s = ptr_c; |
---|
| 85 | padding = col - acol; |
---|
| 86 | } |
---|
| 87 | ++cnt; |
---|
[28ee32b] | 88 | } |
---|
[dd24b6a] | 89 | } |
---|
| 90 | if (cnt) { |
---|
| 91 | while(padding-- > 0) { |
---|
| 92 | strcat(tmpbuff, " "); |
---|
[28ee32b] | 93 | } |
---|
[dd24b6a] | 94 | strncat(tmpbuff, ptr_s, ptr_c - ptr_s - 1); |
---|
[28ee32b] | 95 | } |
---|
| 96 | strcat(tmpbuff, "\n"); |
---|
| 97 | ptr_s = ptr_e + 1; |
---|
| 98 | #if 0 |
---|
[7d4fbcd] | 99 | /* we need to check that we won't run over here */ |
---|
[28ee32b] | 100 | if ( (ptr_e-ptr_s) < (bcol-acol) ) { |
---|
| 101 | len=ptr_e-(ptr_s+acol); |
---|
[7d4fbcd] | 102 | } else { |
---|
| 103 | len=bcol-acol; |
---|
| 104 | } |
---|
[28ee32b] | 105 | if ((ptr_s+len)>=last) { |
---|
| 106 | len-=last-(ptr_s+len); |
---|
[7d4fbcd] | 107 | } |
---|
| 108 | |
---|
[28ee32b] | 109 | strncat(tmpbuff, ptr_s+acol, len); |
---|
[7d4fbcd] | 110 | strcat(tmpbuff, "\n"); |
---|
| 111 | |
---|
[28ee32b] | 112 | ptr_s=ptr_e+1; |
---|
| 113 | #endif |
---|
[7d4fbcd] | 114 | } |
---|
| 115 | strcpy(out, tmpbuff); |
---|
| 116 | owl_free(tmpbuff); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | |
---|
[ab31454] | 120 | void owl_text_indent(char *out, char *in, int n) |
---|
| 121 | { |
---|
[7d4fbcd] | 122 | char *ptr1, *ptr2, *last; |
---|
| 123 | int i; |
---|
| 124 | |
---|
| 125 | strcpy(out, ""); |
---|
| 126 | |
---|
| 127 | last=in+strlen(in)-1; |
---|
| 128 | ptr1=in; |
---|
| 129 | while (ptr1<=last) { |
---|
| 130 | for (i=0; i<n; i++) { |
---|
| 131 | strcat(out, " "); |
---|
| 132 | } |
---|
| 133 | ptr2=strchr(ptr1, '\n'); |
---|
| 134 | if (!ptr2) { |
---|
| 135 | strcat(out, ptr1); |
---|
| 136 | break; |
---|
| 137 | } else { |
---|
| 138 | strncat(out, ptr1, ptr2-ptr1+1); |
---|
| 139 | } |
---|
| 140 | ptr1=ptr2+1; |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | |
---|
[ab31454] | 144 | int owl_text_num_lines(char *in) |
---|
| 145 | { |
---|
[7d4fbcd] | 146 | int lines, i; |
---|
| 147 | |
---|
| 148 | lines=0; |
---|
| 149 | for (i=0; in[i]!='\0'; i++) { |
---|
| 150 | if (in[i]=='\n') lines++; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | /* if the last char wasn't a \n there's one more line */ |
---|
[1bb1e67] | 154 | if (i>0 && in[i-1]!='\n') lines++; |
---|
[7d4fbcd] | 155 | |
---|
| 156 | return(lines); |
---|
| 157 | } |
---|
| 158 | |
---|
[3abf28b] | 159 | |
---|
| 160 | /* caller must free the return */ |
---|
[ab31454] | 161 | char *owl_text_htmlstrip(char *in) |
---|
| 162 | { |
---|
[8d24696] | 163 | char *ptr1, *end, *ptr2, *ptr3, *out, *out2; |
---|
[3abf28b] | 164 | |
---|
| 165 | out=owl_malloc(strlen(in)+30); |
---|
| 166 | strcpy(out, ""); |
---|
| 167 | |
---|
| 168 | ptr1=in; |
---|
| 169 | end=in+strlen(in); |
---|
| 170 | |
---|
| 171 | while(ptr1<end) { |
---|
| 172 | /* look for an open bracket */ |
---|
| 173 | ptr2=strchr(ptr1, '<'); |
---|
| 174 | |
---|
[dafd919] | 175 | /* if none, copy in from here to end and exit */ |
---|
[3abf28b] | 176 | if (ptr2==NULL) { |
---|
| 177 | strcat(out, ptr1); |
---|
[8d24696] | 178 | break; |
---|
[3abf28b] | 179 | } |
---|
| 180 | |
---|
| 181 | /* otherwise copy in everything before the open bracket */ |
---|
| 182 | if (ptr2>ptr1) { |
---|
| 183 | strncat(out, ptr1, ptr2-ptr1); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | /* find the close bracket */ |
---|
| 187 | ptr3=strchr(ptr2, '>'); |
---|
| 188 | |
---|
| 189 | /* if there is no close, copy as you are and exit */ |
---|
| 190 | if (!ptr3) { |
---|
| 191 | strcat(out, ptr2); |
---|
[8d24696] | 192 | break; |
---|
[3abf28b] | 193 | } |
---|
| 194 | |
---|
| 195 | /* look for things we know */ |
---|
[3bcf125] | 196 | if (!strncasecmp(ptr2, "<BODY", 5) || |
---|
[dafd919] | 197 | !strncasecmp(ptr2, "<FONT", 5) || |
---|
[3abf28b] | 198 | !strncasecmp(ptr2, "<HTML", 5) || |
---|
| 199 | !strncasecmp(ptr2, "</FONT", 6) || |
---|
| 200 | !strncasecmp(ptr2, "</HTML", 6) || |
---|
| 201 | !strncasecmp(ptr2, "</BODY", 6)) { |
---|
| 202 | |
---|
| 203 | /* advance to beyond the angle brakcet and go again */ |
---|
| 204 | ptr1=ptr3+1; |
---|
| 205 | continue; |
---|
| 206 | } |
---|
[75e3879] | 207 | if (!strncasecmp(ptr2, "<BR>", 4)) { |
---|
| 208 | strcat(out, "\n"); |
---|
| 209 | ptr1=ptr3+1; |
---|
| 210 | continue; |
---|
| 211 | } |
---|
[3abf28b] | 212 | |
---|
| 213 | /* if it wasn't something we know, copy to the > and go again */ |
---|
[75e3879] | 214 | strncat(out, ptr2, ptr3-ptr2+1); |
---|
[3abf28b] | 215 | ptr1=ptr3+1; |
---|
| 216 | } |
---|
[8d24696] | 217 | |
---|
[e3d9c77] | 218 | out2=owl_text_substitute(out, "<", "<"); |
---|
[8d24696] | 219 | owl_free(out); |
---|
[e3d9c77] | 220 | out=owl_text_substitute(out2, ">", ">"); |
---|
[8d24696] | 221 | owl_free(out2); |
---|
[e3d9c77] | 222 | out2=owl_text_substitute(out, "&", "&"); |
---|
[8d24696] | 223 | owl_free(out); |
---|
[e3d9c77] | 224 | out=owl_text_substitute(out2, """, "\""); |
---|
[8d24696] | 225 | owl_free(out2); |
---|
[e3d9c77] | 226 | out2=owl_text_substitute(out, " ", " "); |
---|
[8d24696] | 227 | owl_free(out); |
---|
[e3d9c77] | 228 | out=owl_text_substitute(out2, " ", " "); |
---|
[8d24696] | 229 | owl_free(out2); |
---|
[e3d9c77] | 230 | out2=owl_text_substitute(out, " ", " "); |
---|
[8d24696] | 231 | owl_free(out); |
---|
[e3d9c77] | 232 | out=owl_text_substitute(out2, "&endash;", "--"); |
---|
[8d24696] | 233 | owl_free(out2); |
---|
[e3d9c77] | 234 | out2=owl_text_substitute(out, "&emdash;", "---"); |
---|
[8d24696] | 235 | owl_free(out); |
---|
| 236 | |
---|
| 237 | return(out2); |
---|
[3abf28b] | 238 | } |
---|
[ab31454] | 239 | |
---|
| 240 | /* caller must free the return */ |
---|
| 241 | char *owl_text_wordwrap(char *in, int col) |
---|
| 242 | { |
---|
| 243 | char *out; |
---|
| 244 | int cur, lastspace, len, lastnewline; |
---|
| 245 | |
---|
| 246 | out=owl_strdup(in); |
---|
| 247 | len=strlen(in); |
---|
| 248 | cur=0; |
---|
| 249 | lastspace=-1; |
---|
| 250 | lastnewline=-1; |
---|
| 251 | |
---|
| 252 | while (cur<(len-1)) { |
---|
| 253 | if (out[cur]==' ') { |
---|
| 254 | lastspace=cur; |
---|
| 255 | cur++; |
---|
| 256 | continue; |
---|
| 257 | } else if (out[cur]=='\n') { |
---|
| 258 | lastnewline=cur; |
---|
| 259 | cur++; |
---|
| 260 | continue; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | /* do we need to wrap? */ |
---|
| 264 | if ( (cur-(lastnewline+1)) > col ) { |
---|
| 265 | if (lastspace==-1 || |
---|
| 266 | (lastnewline>0 && (lastspace<=lastnewline))) { |
---|
| 267 | /* we can't help, sorry */ |
---|
| 268 | cur++; |
---|
| 269 | continue; |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | /* turn the last space into a newline */ |
---|
| 273 | out[lastspace]='\n'; |
---|
| 274 | lastnewline=lastspace; |
---|
| 275 | lastspace=-1; |
---|
| 276 | cur++; |
---|
| 277 | continue; |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | cur++; |
---|
| 281 | continue; |
---|
| 282 | } |
---|
| 283 | return(out); |
---|
| 284 | } |
---|
[e3d9c77] | 285 | |
---|
[f82e233] | 286 | /* this modifies 'in' */ |
---|
| 287 | void owl_text_wordunwrap(char *in) |
---|
| 288 | { |
---|
| 289 | int i, j; |
---|
| 290 | |
---|
| 291 | j=strlen(in); |
---|
| 292 | for (i=0; i<j; i++) { |
---|
| 293 | if ( (in[i]=='\n') && |
---|
| 294 | ((i>0) && (i<(j-1))) && |
---|
| 295 | (in[i-1]!='\n') && |
---|
| 296 | (in[i+1]!='\n') ) |
---|
| 297 | in[i]=' '; |
---|
| 298 | } |
---|
| 299 | } |
---|
| 300 | |
---|
[e3d9c77] | 301 | /* exactly like strstr but case insensitive */ |
---|
| 302 | char *stristr(char *a, char *b) |
---|
| 303 | { |
---|
[28ee32b] | 304 | char *x, *y; |
---|
| 305 | char *ret = NULL; |
---|
| 306 | if ((x = g_utf8_casefold(a, -1)) != NULL) { |
---|
| 307 | if ((y = g_utf8_casefold(b, -1)) != NULL) { |
---|
| 308 | ret = strstr(x, y); |
---|
| 309 | if (ret != NULL) { |
---|
| 310 | ret = ret - x + a; |
---|
| 311 | } |
---|
| 312 | g_free(y); |
---|
| 313 | } |
---|
| 314 | g_free(x); |
---|
[e3d9c77] | 315 | } |
---|
| 316 | return(ret); |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | /* return 1 if a string is only whitespace, otherwise 0 */ |
---|
| 320 | int only_whitespace(char *s) |
---|
| 321 | { |
---|
[28ee32b] | 322 | if (g_utf8_validate(s,-1,NULL)) { |
---|
| 323 | char *p; |
---|
| 324 | for(p = s; p[0]; p=g_utf8_next_char(p)) { |
---|
| 325 | if (!g_unichar_isspace(g_utf8_get_char(p))) return 0; |
---|
| 326 | } |
---|
| 327 | } |
---|
| 328 | else { |
---|
| 329 | int i; |
---|
| 330 | for (i=0; s[i]; i++) { |
---|
| 331 | if (!isspace((int) s[i])) return(0); |
---|
| 332 | } |
---|
[e3d9c77] | 333 | } |
---|
| 334 | return(1); |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | char *owl_getquoting(char *line) |
---|
| 338 | { |
---|
| 339 | if (line[0]=='\0') return("'"); |
---|
| 340 | if (strchr(line, '\'')) return("\""); |
---|
| 341 | if (strchr(line, '"')) return("'"); |
---|
| 342 | if (strchr(line, ' ')) return("'"); |
---|
| 343 | return(""); |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | /* Return a string with any occurances of 'from' replaced with 'to'. |
---|
| 347 | * Does not currently handle backslash quoting, but may in the future. |
---|
| 348 | * Caller must free returned string. |
---|
| 349 | */ |
---|
| 350 | char *owl_text_substitute(char *in, char *from, char *to) |
---|
| 351 | { |
---|
| 352 | |
---|
| 353 | char *out; |
---|
| 354 | int outlen, tolen, fromlen, inpos=0, outpos=0; |
---|
| 355 | |
---|
| 356 | if (!*from) return owl_strdup(in); |
---|
| 357 | |
---|
| 358 | outlen = strlen(in)+1; |
---|
| 359 | tolen = strlen(to); |
---|
| 360 | fromlen = strlen(from); |
---|
[34509d5] | 361 | out = owl_malloc(outlen); |
---|
[e3d9c77] | 362 | |
---|
| 363 | while (in[inpos]) { |
---|
| 364 | if (!strncmp(in+inpos, from, fromlen)) { |
---|
| 365 | outlen += tolen; |
---|
| 366 | out = owl_realloc(out, outlen); |
---|
| 367 | strcpy(out+outpos, to); |
---|
| 368 | inpos += fromlen; |
---|
| 369 | outpos += tolen; |
---|
| 370 | } else { |
---|
| 371 | out[outpos] = in[inpos]; |
---|
| 372 | inpos++; outpos++; |
---|
| 373 | } |
---|
| 374 | } |
---|
| 375 | out[outpos] = '\0'; |
---|
| 376 | return(out); |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | /* replace all instances of character a in buff with the character |
---|
| 380 | * b. buff must be null terminated. |
---|
| 381 | */ |
---|
| 382 | void owl_text_tr(char *buff, char a, char b) |
---|
| 383 | { |
---|
| 384 | int i; |
---|
| 385 | |
---|
| 386 | owl_function_debugmsg("In: %s", buff); |
---|
| 387 | for (i=0; buff[i]!='\0'; i++) { |
---|
| 388 | if (buff[i]==a) buff[i]=b; |
---|
| 389 | } |
---|
| 390 | owl_function_debugmsg("Out: %s", buff); |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | /* Return a string which is like 'in' except that every instance of |
---|
| 394 | * any character in 'toquote' found in 'in' is preceeded by the string |
---|
| 395 | * 'quotestr'. For example, owl_text_quote(in, "+*.", "\") would |
---|
| 396 | * place a backslash before every '+', '*' or '.' in 'in'. It is |
---|
| 397 | * permissable for a character in 'quotestr' to be in 'toquote'. |
---|
| 398 | * On success returns the string, on error returns NULL. |
---|
| 399 | */ |
---|
| 400 | char *owl_text_quote(char *in, char *toquote, char *quotestr) |
---|
| 401 | { |
---|
[72db971] | 402 | int i, x, r, place, escape; |
---|
[e3d9c77] | 403 | int in_len, toquote_len, quotestr_len; |
---|
| 404 | char *out; |
---|
| 405 | |
---|
| 406 | in_len=strlen(in); |
---|
| 407 | toquote_len=strlen(toquote); |
---|
| 408 | quotestr_len=strlen(quotestr); |
---|
| 409 | out=owl_malloc((in_len*quotestr_len)+30); |
---|
| 410 | place=0; |
---|
[72db971] | 411 | escape = 0; |
---|
| 412 | for (i=0; i<in_len; i++) { |
---|
| 413 | if(strchr(toquote, in[i]) != NULL) |
---|
| 414 | escape++; |
---|
| 415 | } |
---|
| 416 | out = owl_malloc(in_len + quotestr_len*escape+1); |
---|
[e3d9c77] | 417 | for (i=0; i<in_len; i++) { |
---|
| 418 | |
---|
| 419 | /* check if it's a character that needs quoting */ |
---|
| 420 | for (x=0; x<toquote_len; x++) { |
---|
| 421 | if (in[i]==toquote[x]) { |
---|
| 422 | /* quote it */ |
---|
| 423 | for (r=0; r<quotestr_len; r++) { |
---|
| 424 | out[place+r]=quotestr[r]; |
---|
| 425 | } |
---|
| 426 | place+=quotestr_len; |
---|
| 427 | break; |
---|
| 428 | } |
---|
| 429 | } |
---|
| 430 | |
---|
| 431 | /* either way, we now copy over the character */ |
---|
| 432 | out[place]=in[i]; |
---|
| 433 | place++; |
---|
| 434 | } |
---|
| 435 | out[place]='\0'; |
---|
| 436 | return(out); |
---|
| 437 | } |
---|