1 |
/* |
2 |
* pretty.c |
3 |
* I/O Formatting Functions |
4 |
* by Ben Allan |
5 |
* Created: 01/98 |
6 |
* Version: $Revision: 1.4 $ |
7 |
* Version control file: $RCSfile: pretty.c,v $ |
8 |
* Date last modified: $Date: 2001/01/28 03:39:36 $ |
9 |
* Last modified by: $Author: ballan $ |
10 |
* |
11 |
* This file is part of the ASCEND Language Interpreter |
12 |
* |
13 |
* Copyright (C) 1998 Carnegie Mellon University |
14 |
* |
15 |
* The ASCEND Language Interpreter is free software; you can |
16 |
* redistribute it and/or modify it under the terms of the GNU |
17 |
* General Public License as published by the Free Software |
18 |
* Foundation; either version 2 of the License, or (at your option) |
19 |
* any later version. |
20 |
* |
21 |
* The ASCEND Language Interpreter is distributed in hope that it |
22 |
* will be useful, but WITHOUT ANY WARRANTY; without even the implied |
23 |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
24 |
* See the GNU General Public License for more details. |
25 |
* |
26 |
* You should have received a copy of the GNU General Public License |
27 |
* along with the program; if not, write to the Free Software |
28 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check |
29 |
* the file named COPYING. COPYING is found in ../compiler. |
30 |
*/ |
31 |
|
32 |
#include <ctype.h> |
33 |
#include "utilities/ascConfig.h" |
34 |
#include "utilities/ascMalloc.h" |
35 |
#include "general/pretty.h" |
36 |
|
37 |
static int outstring(FILE *fp, char *s, int indent) |
38 |
{ |
39 |
int loop = indent; |
40 |
while(loop>0) { |
41 |
FPUTC(' ',(fp)); |
42 |
loop--; |
43 |
} |
44 |
return (indent + FPRINTF(fp,"%s\n",s)); |
45 |
} |
46 |
|
47 |
/* |
48 |
* remember not to change the content of the string, in the net. |
49 |
*/ |
50 |
int print_long_string(FILE *fp, char *string, int width, int indent) |
51 |
{ |
52 |
char oldchar; |
53 |
char *head, *tail, *lastspace; |
54 |
int lw,count; |
55 |
|
56 |
/* there's an off by 1 in the logic, so ... */ |
57 |
width++; |
58 |
if (fp == NULL || string == NULL) { |
59 |
return 0; |
60 |
} |
61 |
if (width < 4 || indent < 0) { |
62 |
return FPRINTF(fp,"%s",string); |
63 |
} |
64 |
lw = 0; count = 0; |
65 |
head = string; |
66 |
lastspace = NULL; |
67 |
|
68 |
while (*head != '\0') { |
69 |
tail = head; |
70 |
while (*tail != '\0' && lw < width) { |
71 |
if (isspace(*tail)) { |
72 |
lastspace = tail; |
73 |
} |
74 |
lw++; |
75 |
tail++; |
76 |
} |
77 |
if (lw < width) { /* found END of line */ |
78 |
return (count + outstring(fp,head,indent)); |
79 |
} |
80 |
if (lastspace == NULL) { /* drat, line overrun */ |
81 |
/* find next space or end */ |
82 |
while (*tail != '\0' && !isspace(*tail)) { |
83 |
lw++; |
84 |
tail++; |
85 |
} |
86 |
if (*tail != '\0') { /* found a break */ |
87 |
oldchar = *tail; |
88 |
*tail = '\0'; |
89 |
count += outstring(fp,head,indent); |
90 |
*tail = oldchar; |
91 |
head = tail; |
92 |
lw = 0; |
93 |
} else { /* found END */ |
94 |
return (count + outstring(fp,head,indent)); |
95 |
} |
96 |
} else { /* found break */ |
97 |
oldchar = *lastspace; |
98 |
lastspace[0] = '\0'; |
99 |
count += outstring(fp,head,indent); |
100 |
*lastspace = oldchar; |
101 |
head = lastspace; |
102 |
lastspace = NULL; |
103 |
lw = 0; |
104 |
} |
105 |
head++; |
106 |
} |
107 |
return count; |
108 |
} |
109 |
|
110 |
/* Computes whether the string s starts with */ |
111 |
/*EOL*/ |
112 |
/* or not. Does not change the string content. */ |
113 |
static int isEOL(char *s) { |
114 |
if (s[0] == '/' && |
115 |
s[1] == '*' && |
116 |
s[2] == 'E' && |
117 |
s[3] == 'O' && |
118 |
s[4] == 'L' && |
119 |
s[5] == '*' && |
120 |
s[6] == '/') { |
121 |
return 1; |
122 |
} |
123 |
return 0; |
124 |
} |
125 |
|
126 |
/* |
127 |
* remember not to change the content of the string, in the net. |
128 |
*/ |
129 |
int print_long_string_EOL(FILE *fp,char *string, int indent) |
130 |
{ |
131 |
char *head, *tail; |
132 |
int count = 0; |
133 |
|
134 |
if (fp == NULL || string == NULL) { |
135 |
return 0; |
136 |
} |
137 |
if (indent < 0) { |
138 |
FPRINTF(fp,"%s",string); |
139 |
} |
140 |
head = string; |
141 |
|
142 |
while (*head != '\0') { |
143 |
tail = head; |
144 |
while (*tail != '\0') { |
145 |
if (isEOL(tail)) { |
146 |
tail[0] = '\0'; |
147 |
count += outstring(fp, head, indent); |
148 |
tail[0] = '/'; |
149 |
tail += 6; |
150 |
head = tail; |
151 |
break; |
152 |
} else { |
153 |
tail++; |
154 |
} |
155 |
} |
156 |
if (*tail == '\0') { |
157 |
/* end of string with leftovers */ |
158 |
count += outstring(fp, head, indent); |
159 |
break; |
160 |
} |
161 |
head++; |
162 |
} |
163 |
return count; |
164 |
} |
165 |
|
166 |
#ifdef PLSTEST |
167 |
int main() |
168 |
{ |
169 |
int w = 12; |
170 |
char *a, *b; |
171 |
a = (char *)malloc(80); |
172 |
b = (char *)malloc(80); |
173 |
sprintf(b,"%s","12345/*EOL*/345 67890123 45 678 90\n1234 5678901/*EOL*/ 9012"); |
174 |
sprintf(a,"%s","123456789012345 67890123 45 678 90\n1234 56789012345678 9012"); |
175 |
|
176 |
fprintf(stderr,">a>>%s<<<\n\n",a); |
177 |
|
178 |
for (;w>=4;w--) { |
179 |
fprintf(stderr,"\n#fmt %d\n",w); |
180 |
print_long_string(stderr,a,w,4); |
181 |
} |
182 |
|
183 |
fprintf(stderr,">b>>%s<<<\n",b); |
184 |
fprintf(stderr,">>>%s<<<\n",b); |
185 |
fprintf(stderr,"\n#fmt EOL\n"); |
186 |
print_long_string_EOL(stderr,b,4); |
187 |
fprintf(stderr,">>><<<\n"); |
188 |
|
189 |
free(a); free(b); |
190 |
return 0; |
191 |
} |
192 |
#endif /* test */ |