Added encoding
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@21 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
546867ac9d
commit
16ca1b7225
4 changed files with 90 additions and 6 deletions
|
|
@ -78,13 +78,16 @@ DohObjInfo *File_type() {
|
|||
}
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
* NewFile(char *filename, char *mode)
|
||||
* NewFile(DOH *filename, char *mode)
|
||||
* ----------------------------------------------------------------------------- */
|
||||
DOH *
|
||||
NewFile(char *filename, char *mode)
|
||||
NewFile(DOH *fn, char *mode)
|
||||
{
|
||||
File *f;
|
||||
FILE *file;
|
||||
char *filename;
|
||||
|
||||
filename = Char(fn);
|
||||
file = fopen(filename,mode);
|
||||
if (!file) return 0;
|
||||
|
||||
|
|
|
|||
|
|
@ -35,6 +35,34 @@ static int Writen(DOH *out, void *buffer, int len) {
|
|||
return len;
|
||||
}
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
* void DohEncoding(char *name, DOH *(*fn)(DOH *s))
|
||||
*
|
||||
* Register a printf encoding method.
|
||||
* ----------------------------------------------------------------------------- */
|
||||
|
||||
static DOH *encodings = 0;
|
||||
void DohEncoding(char *name, DOH *(*fn)(DOH *s))
|
||||
{
|
||||
if (!encodings) encodings = NewHash();
|
||||
Setattr(encodings,(void *) name, NewVoid((void *)fn,0));
|
||||
}
|
||||
|
||||
static DOH *encode(char *name, DOH *s) {
|
||||
DOH *handle, *ns;
|
||||
DOH *(*fn)(DOH *);
|
||||
long pos;
|
||||
if (!encodings || !(handle = Getattr(encodings,name))) {
|
||||
return Copy(s);
|
||||
}
|
||||
pos = Tell(s);
|
||||
Seek(s,0,SEEK_SET);
|
||||
fn = (DOH *(*)(DOH *)) Data(handle);
|
||||
ns = (*fn)(s);
|
||||
Seek(s,pos,SEEK_SET);
|
||||
return ns;
|
||||
}
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
* DohvPrintf(DOH *so, char *format, va_list ap)
|
||||
*
|
||||
|
|
@ -60,6 +88,7 @@ DohvPrintf(DOH *so, char *format, va_list ap)
|
|||
void *pvalue;
|
||||
char *stemp;
|
||||
int nbytes = 0;
|
||||
char encoder[128], *ec;
|
||||
|
||||
while (*p) {
|
||||
switch(state) {
|
||||
|
|
@ -72,6 +101,7 @@ DohvPrintf(DOH *so, char *format, va_list ap)
|
|||
widthval = 0;
|
||||
precval = 0;
|
||||
*(fmt++) = *p;
|
||||
encoder[0] = 0;
|
||||
state = 10;
|
||||
}
|
||||
break;
|
||||
|
|
@ -98,6 +128,9 @@ DohvPrintf(DOH *so, char *format, va_list ap)
|
|||
fmt = newformat;
|
||||
nbytes++;
|
||||
state = 0;
|
||||
} else if (*p == '(') {
|
||||
ec = encoder;
|
||||
state = 60;
|
||||
} else {
|
||||
*(fmt++) = *p;
|
||||
}
|
||||
|
|
@ -193,6 +226,17 @@ DohvPrintf(DOH *so, char *format, va_list ap)
|
|||
*(fmt++) = *p;
|
||||
}
|
||||
break;
|
||||
|
||||
/* Got an encoding header */
|
||||
case 60:
|
||||
if (*p == ')') {
|
||||
*ec = 0;
|
||||
state = 10;
|
||||
} else {
|
||||
*ec = *p;
|
||||
ec++;
|
||||
}
|
||||
break;
|
||||
case 100:
|
||||
/* Got a formatting code */
|
||||
if (widthval < precval) maxwidth = precval;
|
||||
|
|
@ -200,6 +244,7 @@ DohvPrintf(DOH *so, char *format, va_list ap)
|
|||
if ((*p == 's') || (*p == 'S')) { /* Null-Terminated string */
|
||||
DOH *doh;
|
||||
DOH *Sval;
|
||||
DOH *enc = 0;
|
||||
doh = va_arg(ap, DOH *);
|
||||
if (DohCheck(doh)) {
|
||||
/* Is a DOH object. */
|
||||
|
|
@ -208,7 +253,12 @@ DohvPrintf(DOH *so, char *format, va_list ap)
|
|||
} else {
|
||||
Sval = Str(doh);
|
||||
}
|
||||
maxwidth = maxwidth+strlen(newformat)+Len(Sval);
|
||||
if (strlen(encoder)) {
|
||||
enc = encode(encoder,Sval);
|
||||
maxwidth = maxwidth+strlen(newformat)+Len(enc);
|
||||
} else {
|
||||
maxwidth = maxwidth+strlen(newformat)+Len(Sval);
|
||||
}
|
||||
*(fmt++) = 's';
|
||||
*fmt = 0;
|
||||
if ((maxwidth + 1) < OBUFLEN) {
|
||||
|
|
@ -216,11 +266,16 @@ DohvPrintf(DOH *so, char *format, va_list ap)
|
|||
} else {
|
||||
stemp = (char *) DohMalloc(maxwidth+1);
|
||||
}
|
||||
nbytes+=sprintf(stemp,newformat,Data(Sval));
|
||||
if (enc) {
|
||||
nbytes+=sprintf(stemp,newformat,Data(enc));
|
||||
} else {
|
||||
nbytes+=sprintf(stemp,newformat,Data(Sval));
|
||||
}
|
||||
if (Writen(so,stemp,strlen(stemp)) < 0) return -1;
|
||||
if ((DOH *) Sval != doh) {
|
||||
Delete(Sval);
|
||||
}
|
||||
if (enc) Delete(enc);
|
||||
if (*p == 'S') {
|
||||
Delete(doh);
|
||||
}
|
||||
|
|
@ -228,6 +283,7 @@ DohvPrintf(DOH *so, char *format, va_list ap)
|
|||
DohFree(stemp);
|
||||
}
|
||||
} else {
|
||||
if (!doh) doh = "";
|
||||
maxwidth = maxwidth+strlen(newformat)+strlen((char *) doh);
|
||||
*(fmt++) = 's';
|
||||
*fmt = 0;
|
||||
|
|
@ -365,3 +421,18 @@ DOH *DohSplit(DOH *in, char *chs, int nsplits) {
|
|||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/* Read a single line of text */
|
||||
|
||||
DOH *DohReadline(DOH *in) {
|
||||
char c;
|
||||
DOH *s = NewString("");
|
||||
while (1) {
|
||||
if (Read(in,&c,1) < 0) return s;
|
||||
if (c == '\n') return s;
|
||||
if (c == '\r') continue;
|
||||
Putc(c,s);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
* Chicago, IL 60637
|
||||
* beazley@cs.uchicago.edu
|
||||
*
|
||||
* Please read the file LICENSE for the copyright and terms by which SWIG
|
||||
* Please read the file LICENSE for the copyright and terms by which DOH
|
||||
* can be used and distributed.
|
||||
****************************************************************************/
|
||||
|
||||
|
|
@ -717,3 +717,10 @@ String_replace(DOH *stro, DOH *token, DOH *rep, int flags)
|
|||
replace_internal(str,Char(token),Char(rep),flags,str->str,count);
|
||||
}
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
* void String_trim(DOH *str, char *front, char *back)
|
||||
*
|
||||
* Trim a string.
|
||||
* ----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -174,11 +174,13 @@ extern int DohGetc(DOH *obj);
|
|||
extern int DohPutc(int ch, DOH *obj);
|
||||
extern int DohUngetc(int ch, DOH *obj);
|
||||
|
||||
extern void DohEncoding(char *name, DOH *(*fn)(DOH *s));
|
||||
extern int DohPrintf(DOH *obj, char *format, ...);
|
||||
extern int DohvPrintf(DOH *obj, char *format, va_list ap);
|
||||
/* extern int DohScanf(DOH *obj, char *format, ...);
|
||||
extern int DohvScanf(DOH *obj, char *format, va_list ap); */
|
||||
|
||||
extern DOH *DohReadline(DOH *in);
|
||||
/* Macros to invoke the above functions. Includes the location of
|
||||
the caller to simplify debugging if something goes wrong */
|
||||
|
||||
|
|
@ -226,6 +228,7 @@ extern int DohvPrintf(DOH *obj, char *format, va_list ap);
|
|||
#define SetDouble DohSetDouble
|
||||
#define Firstitem DohFirstitem
|
||||
#define Nextitem DohNextitem
|
||||
#define Readline DohReadline
|
||||
|
||||
/* #define Scanf DohScanf
|
||||
#define vScanf DohvScanf*/
|
||||
|
|
@ -270,7 +273,7 @@ extern void String_replace(DOH *s, DOH *token, DOH *rep, int flags);
|
|||
* Files
|
||||
* ----------------------------------------------------------------------------- */
|
||||
|
||||
extern DOH *NewFile(char *file, char *mode);
|
||||
extern DOH *NewFile(DOH *file, char *mode);
|
||||
extern DOH *NewFileFromFile(FILE *f);
|
||||
extern DOH *NewFileFromFd(int fd);
|
||||
extern int DohCopyto(DOH *input, DOH *output);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue