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