[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 | |
---|
| 117 | |
---|
[ab31454] | 118 | int owl_text_num_lines(char *in) |
---|
| 119 | { |
---|
[7d4fbcd] | 120 | int lines, i; |
---|
| 121 | |
---|
| 122 | lines=0; |
---|
| 123 | for (i=0; in[i]!='\0'; i++) { |
---|
| 124 | if (in[i]=='\n') lines++; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /* if the last char wasn't a \n there's one more line */ |
---|
| 128 | if (in[i-1]!='\n') lines++; |
---|
| 129 | |
---|
| 130 | return(lines); |
---|
| 131 | } |
---|
| 132 | |
---|
[3abf28b] | 133 | |
---|
| 134 | /* caller must free the return */ |
---|
[ab31454] | 135 | char *owl_text_htmlstrip(char *in) |
---|
| 136 | { |
---|
[8d24696] | 137 | char *ptr1, *end, *ptr2, *ptr3, *out, *out2; |
---|
[3abf28b] | 138 | |
---|
| 139 | out=owl_malloc(strlen(in)+30); |
---|
| 140 | strcpy(out, ""); |
---|
| 141 | |
---|
| 142 | ptr1=in; |
---|
| 143 | end=in+strlen(in); |
---|
| 144 | |
---|
| 145 | while(ptr1<end) { |
---|
| 146 | /* look for an open bracket */ |
---|
| 147 | ptr2=strchr(ptr1, '<'); |
---|
| 148 | |
---|
[dafd919] | 149 | /* if none, copy in from here to end and exit */ |
---|
[3abf28b] | 150 | if (ptr2==NULL) { |
---|
| 151 | strcat(out, ptr1); |
---|
[8d24696] | 152 | break; |
---|
[3abf28b] | 153 | } |
---|
| 154 | |
---|
| 155 | /* otherwise copy in everything before the open bracket */ |
---|
| 156 | if (ptr2>ptr1) { |
---|
| 157 | strncat(out, ptr1, ptr2-ptr1); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | /* find the close bracket */ |
---|
| 161 | ptr3=strchr(ptr2, '>'); |
---|
| 162 | |
---|
| 163 | /* if there is no close, copy as you are and exit */ |
---|
| 164 | if (!ptr3) { |
---|
| 165 | strcat(out, ptr2); |
---|
[8d24696] | 166 | break; |
---|
[3abf28b] | 167 | } |
---|
| 168 | |
---|
| 169 | /* look for things we know */ |
---|
| 170 | if (!strncasecmp(ptr2, "<BODY ", 6) || |
---|
[dafd919] | 171 | !strncasecmp(ptr2, "<FONT", 5) || |
---|
[3abf28b] | 172 | !strncasecmp(ptr2, "<HTML", 5) || |
---|
| 173 | !strncasecmp(ptr2, "</FONT", 6) || |
---|
| 174 | !strncasecmp(ptr2, "</HTML", 6) || |
---|
| 175 | !strncasecmp(ptr2, "</BODY", 6)) { |
---|
| 176 | |
---|
| 177 | /* advance to beyond the angle brakcet and go again */ |
---|
| 178 | ptr1=ptr3+1; |
---|
| 179 | continue; |
---|
| 180 | } |
---|
[75e3879] | 181 | if (!strncasecmp(ptr2, "<BR>", 4)) { |
---|
| 182 | strcat(out, "\n"); |
---|
| 183 | ptr1=ptr3+1; |
---|
| 184 | continue; |
---|
| 185 | } |
---|
[3abf28b] | 186 | |
---|
| 187 | /* if it wasn't something we know, copy to the > and go again */ |
---|
[75e3879] | 188 | strncat(out, ptr2, ptr3-ptr2+1); |
---|
[3abf28b] | 189 | ptr1=ptr3+1; |
---|
| 190 | } |
---|
[8d24696] | 191 | |
---|
| 192 | out2=owl_util_substitute(out, "<", "<"); |
---|
| 193 | owl_free(out); |
---|
| 194 | out=owl_util_substitute(out2, ">", ">"); |
---|
| 195 | owl_free(out2); |
---|
| 196 | out2=owl_util_substitute(out, "&", "&"); |
---|
| 197 | owl_free(out); |
---|
| 198 | out=owl_util_substitute(out2, """, "\""); |
---|
| 199 | owl_free(out2); |
---|
| 200 | out2=owl_util_substitute(out, " ", " "); |
---|
| 201 | owl_free(out); |
---|
| 202 | out=owl_util_substitute(out2, " ", " "); |
---|
| 203 | owl_free(out2); |
---|
| 204 | out2=owl_util_substitute(out, " ", " "); |
---|
| 205 | owl_free(out); |
---|
| 206 | out=owl_util_substitute(out2, "&endash;", "--"); |
---|
| 207 | owl_free(out2); |
---|
| 208 | out2=owl_util_substitute(out, "&emdash;", "---"); |
---|
| 209 | owl_free(out); |
---|
| 210 | |
---|
| 211 | return(out2); |
---|
[3abf28b] | 212 | } |
---|
[ab31454] | 213 | |
---|
| 214 | /* caller must free the return */ |
---|
| 215 | char *owl_text_wordwrap(char *in, int col) |
---|
| 216 | { |
---|
| 217 | char *out; |
---|
| 218 | int cur, lastspace, len, lastnewline; |
---|
| 219 | |
---|
| 220 | out=owl_strdup(in); |
---|
| 221 | len=strlen(in); |
---|
| 222 | cur=0; |
---|
| 223 | lastspace=-1; |
---|
| 224 | lastnewline=-1; |
---|
| 225 | |
---|
| 226 | while (cur<(len-1)) { |
---|
| 227 | if (out[cur]==' ') { |
---|
| 228 | lastspace=cur; |
---|
| 229 | cur++; |
---|
| 230 | continue; |
---|
| 231 | } else if (out[cur]=='\n') { |
---|
| 232 | lastnewline=cur; |
---|
| 233 | cur++; |
---|
| 234 | continue; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | /* do we need to wrap? */ |
---|
| 238 | if ( (cur-(lastnewline+1)) > col ) { |
---|
| 239 | if (lastspace==-1 || |
---|
| 240 | (lastnewline>0 && (lastspace<=lastnewline))) { |
---|
| 241 | /* we can't help, sorry */ |
---|
| 242 | cur++; |
---|
| 243 | continue; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | /* turn the last space into a newline */ |
---|
| 247 | out[lastspace]='\n'; |
---|
| 248 | lastnewline=lastspace; |
---|
| 249 | lastspace=-1; |
---|
| 250 | cur++; |
---|
| 251 | continue; |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | cur++; |
---|
| 255 | continue; |
---|
| 256 | } |
---|
| 257 | return(out); |
---|
| 258 | } |
---|