comparison th_util.c @ 162:578d9298cc1e

Actually, move th_print_wrap() to th_string module.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 09 Feb 2015 18:52:56 +0200
parents a765a51cbd5c
children 2b07e452fd78
comparison
equal deleted inserted replaced
161:a765a51cbd5c 162:578d9298cc1e
137 assert(th_initialized == TRUE); 137 assert(th_initialized == TRUE);
138 138
139 va_start(ap, fmt); 139 va_start(ap, fmt);
140 THPRINT_V(level, fmt, ap); 140 THPRINT_V(level, fmt, ap);
141 va_end(ap); 141 va_end(ap);
142 }
143
144
145 static void th_pad(FILE *outFile, int count)
146 {
147 while (count--)
148 fputc(' ', outFile);
149 }
150
151
152 void th_print_wrap(FILE *fh, const char *str, int spad, int rpad, int width)
153 {
154 size_t pos = 0;
155 BOOL first = TRUE;
156
157 while (str[pos])
158 {
159 // Pre-pad line
160 int linelen = first ? spad : rpad;
161 th_pad(fh, first ? 0 : rpad);
162 first = FALSE;
163
164 // Skip whitespace at line start
165 while (th_isspace(str[pos]) || str[pos] == '\n') pos++;
166
167 // Handle each word
168 while (str[pos] && str[pos] != '\n')
169 {
170 size_t next;
171 int wlen;
172
173 // Find word length and next break
174 for (wlen = 0, next = pos; str[next] && !th_isspace(str[next]) && str[next] != '\n'; next++, wlen++);
175
176 // Check if we have too much of text?
177 if (linelen + wlen >= width)
178 break;
179
180 // Print what we have
181 for (;pos < next; pos++, linelen++)
182 fputc(str[pos], fh);
183
184 // Check if we are at end of input or hard linefeed
185 if (str[next] == '\n' || str[next] == 0)
186 break;
187 else
188 {
189 fputc(str[pos], fh);
190 pos++;
191 linelen++;
192 }
193 }
194 fprintf(fh, "\n");
195 }
196 } 142 }
197 143
198 144
199 /* Error handling 145 /* Error handling
200 */ 146 */