File: slib_format.cc

package info (click to toggle)
speech-tools 1:2.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,848 kB
  • sloc: cpp: 67,350; ansic: 12,175; sh: 4,047; java: 3,748; makefile: 1,109; lisp: 711; perl: 501; awk: 85; xml: 9
file content (361 lines) | stat: -rw-r--r-- 11,014 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*************************************************************************/
/*                                                                       */
/*                Centre for Speech Technology Research                  */
/*                     University of Edinburgh, UK                       */
/*                       Copyright (c) 1996,1997                         */
/*                        All Rights Reserved.                           */
/*                                                                       */
/*  Permission is hereby granted, free of charge, to use and distribute  */
/*  this software and its documentation without restriction, including   */
/*  without limitation the rights to use, copy, modify, merge, publish,  */
/*  distribute, sublicense, and/or sell copies of this work, and to      */
/*  permit persons to whom this work is furnished to do so, subject to   */
/*  the following conditions:                                            */
/*   1. The code must retain the above copyright notice, this list of    */
/*      conditions and the following disclaimer.                         */
/*   2. Any modifications must be clearly marked as such.                */
/*   3. Original authors' names are not deleted.                         */
/*   4. The authors' names are not used to endorse or promote products   */
/*      derived from this software without specific prior written        */
/*      permission.                                                      */
/*                                                                       */
/*  THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK        */
/*  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      */
/*  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   */
/*  SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE     */
/*  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    */
/*  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   */
/*  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          */
/*  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       */
/*  THIS SOFTWARE.                                                       */
/*                                                                       */
/*************************************************************************/
/*                     Author :  Alan W Black                            */
/*                     Date   :  December 1996                           */
/*-----------------------------------------------------------------------*/
/*                                                                       */
/*  A format function for formated output (like printf)                  */
/*                                                                       */
/*  Its amazing how much I have to write myself to get this to work when */
/*  most people believe this a c library function.                       */
/*                                                                       */
/*=======================================================================*/

#include <cstdlib>
#include <cstdio>
#include "EST_cutils.h"
#include "siod.h"
#include "siodp.h"

static int format_string(LISP fd,const char *formatstr, const char *str);
static int format_lisp(LISP fd,const char *formatstr, LISP a);
static int format_int(LISP fd,const char *formatstr, int i);
static int format_float(LISP fd,const char *formatstr, float f);
static int format_double(LISP fd,const char *formatstr, double d);
static int format_char(LISP fd, char c);
static int get_field_width(const char *directive);
static char *get_directive(const char *fstr);
static char directive_type(const char *fstr);
static void output_string(LISP fd,const char *str);
static int count_arg_places(const char *formatstring);

static EST_String outstring;
static EST_Regex anumber_rx("[0-9]+");

LISP l_format(LISP args)
{
    // A format function for formated output 
    // Hmm not sure how to do this without writing lots myself
    const char *formatstring = get_c_string(car(cdr(args)));
    LISP lfd = car(args);
    LISP fargs = cdr(cdr(args));
    int i;
    LISP a;

    if (count_arg_places(formatstring) != siod_llength(fargs))
	err("format: wrong number of args for format string",NIL);

    outstring="";

    for (i=0,a=fargs; formatstring[i] != '\0'; i++)
    {
	if (formatstring[i] != '%')
	    format_char(lfd,formatstring[i]);
	else if (formatstring[i+1] == '%')
	{
	    format_char(lfd,formatstring[i]);
	    i++;    // skip quoted %
	}
	else if (directive_type(formatstring+i) == 's')
	{
	    i+= format_string(lfd,formatstring+i,get_c_string(car(a)));
	    a = cdr(a);
	}
	else if (directive_type(formatstring+i) == 'l')
	{
	    i+= format_lisp(lfd,formatstring+i,car(a));
	    a = cdr(a);
	}
	else if ((directive_type(formatstring+i) == 'd') ||
		 (directive_type(formatstring+i) == 'x'))
	{
	    i += format_int(lfd,formatstring+i,(int)get_c_int(car(a)));
	    a = cdr(a);
	}
	else if (directive_type(formatstring+i) == 'f')
	{
	    i += format_float(lfd,formatstring+i,(float)get_c_double(car(a)));
	    a = cdr(a);
	}
	else if (directive_type(formatstring+i) == 'g')
	{
	    i += format_double(lfd,formatstring+i,get_c_double(car(a)));
	    a = cdr(a);
	}
	else if (directive_type(formatstring+i) == 'c')
	{
	    format_char(lfd,(char)get_c_int(car(a)));
	    i++;
	    a = cdr(a);
	}
	else
	{
	    cerr << "SIOD format: unsupported format directive %"
		<< directive_type(formatstring+i) << endl;
	    err("",NIL);
	}
    }

    if (lfd == NIL)
	return strintern(outstring);
    else
	return NIL;
}

static int format_string(LISP fd,const char *formatstr, const char *str)
{
    // Output str to fd using directive at start of formatstr
    // Returns the number character in the format directive
    char *directive = get_directive(formatstr);
    int width = get_field_width(directive);
    char *buff;

    if (width > (signed)strlen(str))
	buff = walloc(char,width+10);
    else
	buff = walloc(char,strlen(str)+1);

    sprintf(buff,directive,str);

    output_string(fd,buff);
    width = strlen(directive)-1;
    wfree(buff);
    wfree(directive);
    
    return width;
}

static int format_lisp(LISP fd,const char *formatstr, LISP a)
{
    // Output a as str to fd using directive at start of formatstr
    // Returns the number character in the format directive
    char *directive = get_directive(formatstr);
    int width = get_field_width(directive);
    EST_String buff;

    if (width != 0)
	err("format: width in %l not supported",NIL);

    buff = siod_sprint(a);

    output_string(fd,buff);
    width = strlen(directive)-1;
    wfree(directive);
    
    return width;
}

static int format_int(LISP fd, const char *formatstr, int i)
{
    // Output i to fd using directive at start of formatstr
    // Returns the number character in the format directive
    char *directive = get_directive(formatstr);
    int width = get_field_width(directive);
    char *buff;

    if (width > 20)
	buff = walloc(char,width+10);
    else
	buff = walloc(char,20);

    sprintf(buff,directive,i);

    output_string(fd,buff);
    width = strlen(directive)-1;
    wfree(buff);
    wfree(directive);
    
    return width;
}

static int format_float(LISP fd, const char *formatstr, float f)
{
    // Output f to fd using directive at start of formatstr
    // Returns the number character in the format directive
    char *directive = get_directive(formatstr);
    int width = get_field_width(directive);
    char *buff;

    if (width > 20)
	buff = walloc(char,width+10);
    else
	buff = walloc(char,20);

    sprintf(buff,directive,f);

    output_string(fd,buff);
    width = strlen(directive)-1;
    wfree(buff);
    wfree(directive);
    
    return width;
}

static int format_double(LISP fd, const char *formatstr, double d)
{
    // Output f to fd using directive at start of formatstr
    // Returns the number character in the format directive
    char *directive = get_directive(formatstr);
    int width = get_field_width(directive);
    char *buff;

    if (width > 30)
	buff = walloc(char,width+10);
    else
	buff = walloc(char,30);

    sprintf(buff,directive,d);

    output_string(fd,buff);
    width = strlen(directive)-1;
    wfree(buff);
    wfree(directive);
    
    return width;
}

static int format_char(LISP fd, char c)
{
    // Output c to fd using directive at start of formatstr
    // Returns the number character in the format directive
    char buff[10];

    sprintf(buff,"%c",c);

    output_string(fd,buff);
    
    return 0;
}

static int get_field_width(const char *directive)
{
    // Look inside the directive for any explicit width info
    
    if (strlen(directive) == 2)
	return 0;
    else 
    {
	EST_String nums = directive;
	nums = nums.at(1,strlen(directive)-2);
	if (nums.matches(anumber_rx))
	    return atoi(nums);
	else if (nums.contains("."))
	{
	    EST_String n1 = nums.before(".");
	    EST_String n2 = nums.after(".");
	    return atoi(n1) + atoi(n2);
	}
	else
	{
	    cerr << "SIOD format: can't find width in directive "
		<< directive << endl;
	    err("",NIL);
	}
    }
    return 0;
}

static char *get_directive(const char *fstr)
{
    // Copy the format directive from the start of this string
    int i;

    for (i=0; fstr[i] != '\0'; i++)
	if ((fstr[i] >= 'a') &&
	    (fstr[i] <= 'z'))
	    break;
    if (fstr[i] == '\0')
	err("format: premature end of format structure",NIL);
    char *direct = walloc(char,i+2);
    memmove(direct,fstr,i+1);
    direct[i+1] = '\0';
    return direct;
}

static char directive_type(const char *fstr)
{
    // return the next lower case character.  This identifies the
    // type of the argument to be inserted in the format string
    int i;
    
    for (i=0; fstr[i] != '\0'; i++)
	if ((fstr[i] >= 'a') &&
	    (fstr[i] <= 'z'))
	{
	    return fstr[i];
	}

    err("SIOD format: premature end of format structure",NIL);
    return '\0';

}

static void output_string(LISP fd, const char *str)
{
    if (fd == NIL)
	outstring += str;
    else if (fd == truth)
	fprintf(stdout,"%s",str);
    else if (TYPEP(fd,tc_c_file))
	fprintf(get_c_file(fd,NULL),"%s",str);
    else
	err("format: not a file",fd);
}

static int count_arg_places(const char *formatstring)
{
    // count number of places in the format string.
    int c,i;

    for (c=i=0; formatstring[i] != '\0'; i++)
	if (formatstring[i] == '%')
	{
	    if (formatstring[i+1] == '%')
		i++;
	    else
		c++;
	}

    return c;
}

void init_subrs_format()
{
 init_lsubr("format",l_format,
 "(format FD FORMATSTRING ARG0 ARG1 ...)\n\
  Output ARGs to FD using FROMATSTRING.  FORMATSTRING is like a printf\n\
  formatstrng.  FD may be a filedescriptor, or t (standard output) or\n\
  nil (return as a string).  Note not all printf format directive are\n\
  supported.  %l is additionally support for Lisp objects.\n\
  [see Scheme I/O]");
}