[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 | { |
---|
[3abf28b] | 137 | char *ptr1, *end, *ptr2, *ptr3, *out; |
---|
| 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); |
---|
| 152 | return(out); |
---|
| 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); |
---|
[75e3879] | 166 | return(out); |
---|
[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 | } |
---|
| 191 | return(out); |
---|
| 192 | } |
---|
[ab31454] | 193 | |
---|
| 194 | /* caller must free the return */ |
---|
| 195 | char *owl_text_wordwrap(char *in, int col) |
---|
| 196 | { |
---|
| 197 | char *out; |
---|
| 198 | int cur, lastspace, len, lastnewline; |
---|
| 199 | |
---|
| 200 | out=owl_strdup(in); |
---|
| 201 | len=strlen(in); |
---|
| 202 | cur=0; |
---|
| 203 | lastspace=-1; |
---|
| 204 | lastnewline=-1; |
---|
| 205 | |
---|
| 206 | while (cur<(len-1)) { |
---|
| 207 | if (out[cur]==' ') { |
---|
| 208 | lastspace=cur; |
---|
| 209 | cur++; |
---|
| 210 | continue; |
---|
| 211 | } else if (out[cur]=='\n') { |
---|
| 212 | lastnewline=cur; |
---|
| 213 | cur++; |
---|
| 214 | continue; |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | /* do we need to wrap? */ |
---|
| 218 | if ( (cur-(lastnewline+1)) > col ) { |
---|
| 219 | if (lastspace==-1 || |
---|
| 220 | (lastnewline>0 && (lastspace<=lastnewline))) { |
---|
| 221 | /* we can't help, sorry */ |
---|
| 222 | cur++; |
---|
| 223 | continue; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | /* turn the last space into a newline */ |
---|
| 227 | out[lastspace]='\n'; |
---|
| 228 | lastnewline=lastspace; |
---|
| 229 | lastspace=-1; |
---|
| 230 | cur++; |
---|
| 231 | continue; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | cur++; |
---|
| 235 | continue; |
---|
| 236 | } |
---|
| 237 | return(out); |
---|
| 238 | } |
---|