52 static int format_string(LISP fd,
const char *formatstr,
const char *str);
53 static int format_lisp(LISP fd,
const char *formatstr, LISP a);
54 static int format_int(LISP fd,
const char *formatstr,
int i);
55 static int format_float(LISP fd,
const char *formatstr,
float f);
56 static int format_double(LISP fd,
const char *formatstr,
double d);
57 static int format_char(LISP fd,
char c);
58 static int get_field_width(
const char *directive);
59 static char *get_directive(
const char *fstr);
60 static char directive_type(
const char *fstr);
61 static void output_string(LISP fd,
const char *str);
62 static int count_arg_places(
const char *formatstring);
73 LISP fargs =
cdr(
cdr(args));
77 if (count_arg_places(formatstring) !=
siod_llength(fargs))
78 err(
"format: wrong number of args for format string",
NIL);
82 for (i=0,a=fargs; formatstring[i] !=
'\0'; i++)
84 if (formatstring[i] !=
'%')
85 format_char(lfd,formatstring[i]);
86 else if (formatstring[i+1] ==
'%')
88 format_char(lfd,formatstring[i]);
91 else if (directive_type(formatstring+i) ==
's')
96 else if (directive_type(formatstring+i) ==
'l')
98 i+= format_lisp(lfd,formatstring+i,
car(a));
101 else if ((directive_type(formatstring+i) ==
'd') ||
102 (directive_type(formatstring+i) ==
'x'))
104 i += format_int(lfd,formatstring+i,(
int)
get_c_int(
car(a)));
107 else if (directive_type(formatstring+i) ==
'f')
112 else if (directive_type(formatstring+i) ==
'g')
117 else if (directive_type(formatstring+i) ==
'c')
125 cerr <<
"SIOD format: unsupported format directive %" 126 << directive_type(formatstring+i) << endl;
137 static int format_string(LISP fd,
const char *formatstr,
const char *str)
141 char *directive = get_directive(formatstr);
142 int width = get_field_width(directive);
145 if (width > (
signed)strlen(str))
146 buff =
walloc(
char,width+10);
148 buff =
walloc(
char,strlen(str)+1);
150 sprintf(buff,directive,str);
152 output_string(fd,buff);
153 width = strlen(directive)-1;
160 static int format_lisp(LISP fd,
const char *formatstr, LISP a)
164 char *directive = get_directive(formatstr);
165 int width = get_field_width(directive);
169 err(
"format: width in %l not supported",
NIL);
173 output_string(fd,buff);
174 width = strlen(directive)-1;
180 static int format_int(LISP fd,
const char *formatstr,
int i)
184 char *directive = get_directive(formatstr);
185 int width = get_field_width(directive);
189 buff =
walloc(
char,width+10);
193 sprintf(buff,directive,i);
195 output_string(fd,buff);
196 width = strlen(directive)-1;
203 static int format_float(LISP fd,
const char *formatstr,
float f)
207 char *directive = get_directive(formatstr);
208 int width = get_field_width(directive);
212 buff =
walloc(
char,width+10);
216 sprintf(buff,directive,f);
218 output_string(fd,buff);
219 width = strlen(directive)-1;
226 static int format_double(LISP fd,
const char *formatstr,
double d)
230 char *directive = get_directive(formatstr);
231 int width = get_field_width(directive);
235 buff =
walloc(
char,width+10);
239 sprintf(buff,directive,d);
241 output_string(fd,buff);
242 width = strlen(directive)-1;
249 static int format_char(LISP fd,
char c)
255 sprintf(buff,
"%c",c);
257 output_string(fd,buff);
262 static int get_field_width(
const char *directive)
266 if (strlen(directive) == 2)
271 nums = nums.
at(1,strlen(directive)-2);
278 return atoi(n1) + atoi(n2);
282 cerr <<
"SIOD format: can't find width in directive " 283 << directive << endl;
290 static char *get_directive(
const char *fstr)
295 for (i=0; fstr[i] !=
'\0'; i++)
296 if ((fstr[i] >=
'a') &&
300 err(
"format: premature end of format structure",
NIL);
301 char *direct =
walloc(
char,i+2);
302 memmove(direct,fstr,i+1);
307 static char directive_type(
const char *fstr)
313 for (i=0; fstr[i] !=
'\0'; i++)
314 if ((fstr[i] >=
'a') &&
320 err(
"SIOD format: premature end of format structure",
NIL);
325 static void output_string(LISP fd,
const char *str)
329 else if (fd ==
truth)
330 fprintf(stdout,
"%s",str);
334 err(
"format: not a file",fd);
337 static int count_arg_places(
const char *formatstring)
342 for (c=i=0; formatstring[i] !=
'\0'; i++)
343 if (formatstring[i] ==
'%')
345 if (formatstring[i+1] ==
'%')
357 "(format FD FORMATSTRING ARG0 ARG1 ...)\n\ 358 Output ARGs to FD using FROMATSTRING. FORMATSTRING is like a printf\n\ 359 formatstrng. FD may be a filedescriptor, or t (standard output) or\n\ 360 nil (return as a string). Note not all printf format directive are\n\ 361 supported. %l is additionally support for Lisp objects.\n\
#define walloc(TYPE, SIZE)
double get_c_double(LISP x)
int contains(const char *s, ssize_t pos=-1) const
Does it contain this substring?
A Regular expression class to go with the CSTR EST_String class.
long int get_c_int(LISP x)
int siod_llength(LISP list)
LISP strintern(const char *data)
FILE * get_c_file(LISP p, FILE *deflt)
void err(const char *message, LISP x) EST_NORETURN
const char * get_c_string(LISP x)
EST_String siod_sprint(LISP exp)
int matches(const char *e, ssize_t pos=0) const
Exactly match this string?
void init_lsubr(const char *name, LISP(*fcn)(LISP), const char *doc)
EST_String after(int pos, int len=1) const
Part after pos+len.
EST_String before(int pos, int len=0) const
Part before position.
EST_String at(int from, int len=0) const
Return part at position.