| 1 | #include <stdio.h> |
|---|
| 2 | #include <string.h> |
|---|
| 3 | #include <stdlib.h> |
|---|
| 4 | #include "owl.h" |
|---|
| 5 | |
|---|
| 6 | static const char fileIdent[] = "$Id$"; |
|---|
| 7 | |
|---|
| 8 | int owl_text_truncate_lines(char *out, char *in, int aline, int lines) { |
|---|
| 9 | /* start with line aline (where the first line is 1) and print |
|---|
| 10 | * 'lines' lines |
|---|
| 11 | */ |
|---|
| 12 | char *ptr1, *ptr2; |
|---|
| 13 | int i; |
|---|
| 14 | |
|---|
| 15 | strcpy(out, ""); |
|---|
| 16 | |
|---|
| 17 | if (aline==0) aline=1; /* really illegal use */ |
|---|
| 18 | |
|---|
| 19 | /* find the starting line */ |
|---|
| 20 | ptr1=in; |
|---|
| 21 | if (aline!=1) { |
|---|
| 22 | for (i=0; i<aline-1; i++) { |
|---|
| 23 | ptr1=strchr(ptr1, '\n'); |
|---|
| 24 | if (!ptr1) return(-1); |
|---|
| 25 | ptr1++; |
|---|
| 26 | } |
|---|
| 27 | } |
|---|
| 28 | /* ptr1 now holds the starting point */ |
|---|
| 29 | |
|---|
| 30 | /* copy in the next 'lines' lines */ |
|---|
| 31 | if (lines<1) return(-1); |
|---|
| 32 | |
|---|
| 33 | for (i=0; i<lines; i++) { |
|---|
| 34 | ptr2=strchr(ptr1, '\n'); |
|---|
| 35 | if (!ptr2) { |
|---|
| 36 | strcat(out, ptr1); |
|---|
| 37 | return(-1); |
|---|
| 38 | } |
|---|
| 39 | strncat(out, ptr1, ptr2-ptr1+1); |
|---|
| 40 | ptr1=ptr2+1; |
|---|
| 41 | } |
|---|
| 42 | return(0); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | void owl_text_truncate_cols(char *out, char *in, int acol, int bcol) { |
|---|
| 46 | char *ptr1, *ptr2, *tmpbuff, *last; |
|---|
| 47 | int len; |
|---|
| 48 | |
|---|
| 49 | /* the first column is column 0 */ |
|---|
| 50 | |
|---|
| 51 | /* the message is expected to end in a new line for now */ |
|---|
| 52 | |
|---|
| 53 | tmpbuff=owl_malloc(strlen(in)+20); |
|---|
| 54 | |
|---|
| 55 | strcpy(tmpbuff, ""); |
|---|
| 56 | last=in+strlen(in)-1; |
|---|
| 57 | ptr1=in; |
|---|
| 58 | while (ptr1<last) { |
|---|
| 59 | ptr2=strchr(ptr1, '\n'); |
|---|
| 60 | if (!ptr2) { |
|---|
| 61 | /* but this shouldn't happen if we end in a \n */ |
|---|
| 62 | break; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | if (ptr2==ptr1) { |
|---|
| 66 | strcat(tmpbuff, "\n"); |
|---|
| 67 | ptr1++; |
|---|
| 68 | continue; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | /* we need to check that we won't run over here */ |
|---|
| 72 | if ( (ptr2-ptr1) < (bcol-acol) ) { |
|---|
| 73 | len=ptr2-(ptr1+acol); |
|---|
| 74 | } else { |
|---|
| 75 | len=bcol-acol; |
|---|
| 76 | } |
|---|
| 77 | if ((ptr1+len)>=last) { |
|---|
| 78 | len-=last-(ptr1+len); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | strncat(tmpbuff, ptr1+acol, len); |
|---|
| 82 | strcat(tmpbuff, "\n"); |
|---|
| 83 | |
|---|
| 84 | ptr1=ptr2+1; |
|---|
| 85 | } |
|---|
| 86 | strcpy(out, tmpbuff); |
|---|
| 87 | owl_free(tmpbuff); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | void owl_text_indent(char *out, char *in, int n) { |
|---|
| 92 | char *ptr1, *ptr2, *last; |
|---|
| 93 | int i; |
|---|
| 94 | |
|---|
| 95 | strcpy(out, ""); |
|---|
| 96 | |
|---|
| 97 | last=in+strlen(in)-1; |
|---|
| 98 | ptr1=in; |
|---|
| 99 | while (ptr1<=last) { |
|---|
| 100 | for (i=0; i<n; i++) { |
|---|
| 101 | strcat(out, " "); |
|---|
| 102 | } |
|---|
| 103 | ptr2=strchr(ptr1, '\n'); |
|---|
| 104 | if (!ptr2) { |
|---|
| 105 | strcat(out, ptr1); |
|---|
| 106 | break; |
|---|
| 107 | } else { |
|---|
| 108 | strncat(out, ptr1, ptr2-ptr1+1); |
|---|
| 109 | } |
|---|
| 110 | ptr1=ptr2+1; |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | int owl_text_num_lines(char *in) { |
|---|
| 116 | int lines, i; |
|---|
| 117 | |
|---|
| 118 | lines=0; |
|---|
| 119 | for (i=0; in[i]!='\0'; i++) { |
|---|
| 120 | if (in[i]=='\n') lines++; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | /* if the last char wasn't a \n there's one more line */ |
|---|
| 124 | if (in[i-1]!='\n') lines++; |
|---|
| 125 | |
|---|
| 126 | return(lines); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | /* caller must free the return */ |
|---|
| 131 | char *owl_text_htmlstrip(char *in) { |
|---|
| 132 | char *ptr1, *end, *ptr2, *ptr3, *out; |
|---|
| 133 | |
|---|
| 134 | out=owl_malloc(strlen(in)+30); |
|---|
| 135 | strcpy(out, ""); |
|---|
| 136 | |
|---|
| 137 | ptr1=in; |
|---|
| 138 | end=in+strlen(in); |
|---|
| 139 | |
|---|
| 140 | while(ptr1<end) { |
|---|
| 141 | /* look for an open bracket */ |
|---|
| 142 | ptr2=strchr(ptr1, '<'); |
|---|
| 143 | |
|---|
| 144 | /* if not, copy in from here to end and exit */ |
|---|
| 145 | if (ptr2==NULL) { |
|---|
| 146 | strcat(out, ptr1); |
|---|
| 147 | return(out); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | /* otherwise copy in everything before the open bracket */ |
|---|
| 151 | if (ptr2>ptr1) { |
|---|
| 152 | strncat(out, ptr1, ptr2-ptr1); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | /* find the close bracket */ |
|---|
| 156 | ptr3=strchr(ptr2, '>'); |
|---|
| 157 | |
|---|
| 158 | /* if there is no close, copy as you are and exit */ |
|---|
| 159 | if (!ptr3) { |
|---|
| 160 | strcat(out, ptr2); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | /* look for things we know */ |
|---|
| 164 | if (!strncasecmp(ptr2, "<BODY ", 6) || |
|---|
| 165 | !strncasecmp(ptr2, "<FONT ", 6) || |
|---|
| 166 | !strncasecmp(ptr2, "<HTML", 5) || |
|---|
| 167 | !strncasecmp(ptr2, "</FONT", 6) || |
|---|
| 168 | !strncasecmp(ptr2, "</HTML", 6) || |
|---|
| 169 | !strncasecmp(ptr2, "</BODY", 6)) { |
|---|
| 170 | |
|---|
| 171 | /* advance to beyond the angle brakcet and go again */ |
|---|
| 172 | ptr1=ptr3+1; |
|---|
| 173 | continue; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | /* if it wasn't something we know, copy to the > and go again */ |
|---|
| 177 | strncat(out, ptr2, ptr3-ptr2); |
|---|
| 178 | ptr1=ptr3+1; |
|---|
| 179 | } |
|---|
| 180 | return(out); |
|---|
| 181 | } |
|---|