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