comparison th_args.c @ 584:390c66af09cf

Move th_print_wrap() and th_print_pad() to th_args from th_string.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 12 Jan 2020 14:27:09 +0200
parents 11f56a652cb0
children 1a4359ecc5d5
comparison
equal deleted inserted replaced
583:d60e1d751b5b 584:390c66af09cf
175 175
176 return optionsOK; 176 return optionsOK;
177 } 177 }
178 178
179 179
180 void th_print_pad(FILE *fh, int count, const char och)
181 {
182 while (count--)
183 fputc(och, fh);
184 }
185
186
187 void th_print_wrap(FILE *fh, const char *str,
188 const int spad, int const rpad, const int width)
189 {
190 size_t pos = 0;
191 BOOL first = TRUE;
192
193 while (str[pos])
194 {
195 // Pre-pad line
196 int linelen = first ? spad : rpad;
197 th_print_pad(fh, first ? 0 : rpad, ' ');
198 first = FALSE;
199
200 // Skip whitespace at line start
201 while (th_isspace(str[pos]) || str[pos] == '\n')
202 pos++;
203
204 // Handle each word
205 while (str[pos] && str[pos] != '\n')
206 {
207 size_t next;
208 int wlen;
209
210 // Find word length and next break
211 for (wlen = 0, next = pos;
212 str[next] && !th_isspace(str[next]) &&
213 str[next] != '\n';
214 next++, wlen++);
215
216 // Check if we have too much of text?
217 if (linelen + wlen >= width)
218 break;
219
220 // Print what we have
221 for (;pos < next; pos++, linelen++)
222 fputc(str[pos], fh);
223
224 // Check if we are at end of input or hard linefeed
225 if (str[next] == '\n' || str[next] == 0)
226 break;
227 else
228 {
229 fputc(str[pos], fh);
230 pos++;
231 linelen++;
232 }
233 }
234 fprintf(fh, "\n");
235 }
236 }
237
238
180 /** 239 /**
181 * Print help for commandline arguments/options 240 * Print help for commandline arguments/options
182 * @param fh stdio file handle to output to 241 * @param fh stdio file handle to output to
183 * @param opts options list array 242 * @param opts options list array
184 * @param nopts number of elements in options list array 243 * @param nopts number of elements in options list array