[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 | |
---|
[7d4fbcd] | 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 | |
---|