source: text.c @ 7d4fbcd

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 7d4fbcd was 7d4fbcd, checked in by James M. Kretchmar <kretch@mit.edu>, 22 years ago
Initial check in
  • Property mode set to 100644
File size: 2.3 KB
Line 
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include "owl.h"
5
6int owl_text_truncate_lines(char *out, char *in, int aline, int lines) {
7  /* start with line aline (where the first line is 1) and print
8   *  'lines' lines
9   */
10  char *ptr1, *ptr2;
11  int i;
12
13  strcpy(out, "");
14 
15  if (aline==0) aline=1; /* really illegal use */
16
17  /* find the starting line */
18  ptr1=in;
19  if (aline!=1) {
20     for (i=0; i<aline-1; i++) {
21      ptr1=strchr(ptr1, '\n');
22      if (!ptr1) return(-1);
23      ptr1++;
24    }
25  }
26  /* ptr1 now holds the starting point */
27
28  /* copy in the next 'lines' lines */
29  if (lines<1) return(-1);
30 
31  for (i=0; i<lines; i++) {
32    ptr2=strchr(ptr1, '\n');
33    if (!ptr2) {
34      strcat(out, ptr1);
35      return(-1);
36    }
37    strncat(out, ptr1, ptr2-ptr1+1);
38    ptr1=ptr2+1;
39  }
40  return(0);
41}
42
43void owl_text_truncate_cols(char *out, char *in, int acol, int bcol) {
44  char *ptr1, *ptr2, *tmpbuff, *last;
45  int len;
46 
47  /* the first column is column 0 */
48
49  /* the message is expected to end in a new line for now */
50
51  tmpbuff=owl_malloc(strlen(in)+20);
52
53  strcpy(tmpbuff, "");
54  last=in+strlen(in)-1;
55  ptr1=in;
56  while (ptr1<last) {
57    ptr2=strchr(ptr1, '\n');
58    if (!ptr2) {
59      /* but this shouldn't happen if we end in a \n */
60      break;
61    }
62   
63    if (ptr2==ptr1) {
64      strcat(tmpbuff, "\n");
65      ptr1++;
66      continue;
67    }
68
69    /* we need to check that we won't run over here */
70    if ( (ptr2-ptr1) < (bcol-acol) ) {
71      len=ptr2-(ptr1+acol);
72    } else {
73      len=bcol-acol;
74    }
75    if ((ptr1+len)>=last) {
76      len-=last-(ptr1+len);
77    }
78
79    strncat(tmpbuff, ptr1+acol, len);
80    strcat(tmpbuff, "\n");
81
82    ptr1=ptr2+1;
83  }
84  strcpy(out, tmpbuff);
85  owl_free(tmpbuff);
86}
87
88
89void owl_text_indent(char *out, char *in, int n) {
90  char *ptr1, *ptr2, *last;
91  int i;
92
93  strcpy(out, "");
94
95  last=in+strlen(in)-1;
96  ptr1=in;
97  while (ptr1<=last) {
98    for (i=0; i<n; i++) {
99      strcat(out, " ");
100    }
101    ptr2=strchr(ptr1, '\n');
102    if (!ptr2) {
103      strcat(out, ptr1);
104      break;
105    } else {
106      strncat(out, ptr1, ptr2-ptr1+1);
107    }
108    ptr1=ptr2+1;
109  }
110}
111
112
113int owl_text_num_lines(char *in) {
114  int lines, i;
115
116  lines=0;
117  for (i=0; in[i]!='\0'; i++) {
118    if (in[i]=='\n') lines++;
119  }
120
121  /* if the last char wasn't a \n there's one more line */
122  if (in[i-1]!='\n') lines++;
123
124  return(lines);
125}
126
Note: See TracBrowser for help on using the repository browser.