Added encoding
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@21 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
2cfa99195a
commit
f621311ac3
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 *
|
DOH *
|
||||||
NewFile(char *filename, char *mode)
|
NewFile(DOH *fn, char *mode)
|
||||||
{
|
{
|
||||||
File *f;
|
File *f;
|
||||||
FILE *file;
|
FILE *file;
|
||||||
|
char *filename;
|
||||||
|
|
||||||
|
filename = Char(fn);
|
||||||
file = fopen(filename,mode);
|
file = fopen(filename,mode);
|
||||||
if (!file) return 0;
|
if (!file) return 0;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,34 @@ static int Writen(DOH *out, void *buffer, int len) {
|
||||||
return 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)
|
* DohvPrintf(DOH *so, char *format, va_list ap)
|
||||||
*
|
*
|
||||||
|
|
@ -60,6 +88,7 @@ DohvPrintf(DOH *so, char *format, va_list ap)
|
||||||
void *pvalue;
|
void *pvalue;
|
||||||
char *stemp;
|
char *stemp;
|
||||||
int nbytes = 0;
|
int nbytes = 0;
|
||||||
|
char encoder[128], *ec;
|
||||||
|
|
||||||
while (*p) {
|
while (*p) {
|
||||||
switch(state) {
|
switch(state) {
|
||||||
|
|
@ -72,6 +101,7 @@ DohvPrintf(DOH *so, char *format, va_list ap)
|
||||||
widthval = 0;
|
widthval = 0;
|
||||||
precval = 0;
|
precval = 0;
|
||||||
*(fmt++) = *p;
|
*(fmt++) = *p;
|
||||||
|
encoder[0] = 0;
|
||||||
state = 10;
|
state = 10;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
@ -98,6 +128,9 @@ DohvPrintf(DOH *so, char *format, va_list ap)
|
||||||
fmt = newformat;
|
fmt = newformat;
|
||||||
nbytes++;
|
nbytes++;
|
||||||
state = 0;
|
state = 0;
|
||||||
|
} else if (*p == '(') {
|
||||||
|
ec = encoder;
|
||||||
|
state = 60;
|
||||||
} else {
|
} else {
|
||||||
*(fmt++) = *p;
|
*(fmt++) = *p;
|
||||||
}
|
}
|
||||||
|
|
@ -193,6 +226,17 @@ DohvPrintf(DOH *so, char *format, va_list ap)
|
||||||
*(fmt++) = *p;
|
*(fmt++) = *p;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
/* Got an encoding header */
|
||||||
|
case 60:
|
||||||
|
if (*p == ')') {
|
||||||
|
*ec = 0;
|
||||||
|
state = 10;
|
||||||
|
} else {
|
||||||
|
*ec = *p;
|
||||||
|
ec++;
|
||||||
|
}
|
||||||
|
break;
|
||||||
case 100:
|
case 100:
|
||||||
/* Got a formatting code */
|
/* Got a formatting code */
|
||||||
if (widthval < precval) maxwidth = precval;
|
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 */
|
if ((*p == 's') || (*p == 'S')) { /* Null-Terminated string */
|
||||||
DOH *doh;
|
DOH *doh;
|
||||||
DOH *Sval;
|
DOH *Sval;
|
||||||
|
DOH *enc = 0;
|
||||||
doh = va_arg(ap, DOH *);
|
doh = va_arg(ap, DOH *);
|
||||||
if (DohCheck(doh)) {
|
if (DohCheck(doh)) {
|
||||||
/* Is a DOH object. */
|
/* Is a DOH object. */
|
||||||
|
|
@ -208,7 +253,12 @@ DohvPrintf(DOH *so, char *format, va_list ap)
|
||||||
} else {
|
} else {
|
||||||
Sval = Str(doh);
|
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++) = 's';
|
||||||
*fmt = 0;
|
*fmt = 0;
|
||||||
if ((maxwidth + 1) < OBUFLEN) {
|
if ((maxwidth + 1) < OBUFLEN) {
|
||||||
|
|
@ -216,11 +266,16 @@ DohvPrintf(DOH *so, char *format, va_list ap)
|
||||||
} else {
|
} else {
|
||||||
stemp = (char *) DohMalloc(maxwidth+1);
|
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 (Writen(so,stemp,strlen(stemp)) < 0) return -1;
|
||||||
if ((DOH *) Sval != doh) {
|
if ((DOH *) Sval != doh) {
|
||||||
Delete(Sval);
|
Delete(Sval);
|
||||||
}
|
}
|
||||||
|
if (enc) Delete(enc);
|
||||||
if (*p == 'S') {
|
if (*p == 'S') {
|
||||||
Delete(doh);
|
Delete(doh);
|
||||||
}
|
}
|
||||||
|
|
@ -228,6 +283,7 @@ DohvPrintf(DOH *so, char *format, va_list ap)
|
||||||
DohFree(stemp);
|
DohFree(stemp);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if (!doh) doh = "";
|
||||||
maxwidth = maxwidth+strlen(newformat)+strlen((char *) doh);
|
maxwidth = maxwidth+strlen(newformat)+strlen((char *) doh);
|
||||||
*(fmt++) = 's';
|
*(fmt++) = 's';
|
||||||
*fmt = 0;
|
*fmt = 0;
|
||||||
|
|
@ -365,3 +421,18 @@ DOH *DohSplit(DOH *in, char *chs, int nsplits) {
|
||||||
}
|
}
|
||||||
return list;
|
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
|
* Chicago, IL 60637
|
||||||
* beazley@cs.uchicago.edu
|
* 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.
|
* 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);
|
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 DohPutc(int ch, DOH *obj);
|
||||||
extern int DohUngetc(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 DohPrintf(DOH *obj, char *format, ...);
|
||||||
extern int DohvPrintf(DOH *obj, char *format, va_list ap);
|
extern int DohvPrintf(DOH *obj, char *format, va_list ap);
|
||||||
/* extern int DohScanf(DOH *obj, char *format, ...);
|
/* extern int DohScanf(DOH *obj, char *format, ...);
|
||||||
extern int DohvScanf(DOH *obj, char *format, va_list ap); */
|
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
|
/* Macros to invoke the above functions. Includes the location of
|
||||||
the caller to simplify debugging if something goes wrong */
|
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 SetDouble DohSetDouble
|
||||||
#define Firstitem DohFirstitem
|
#define Firstitem DohFirstitem
|
||||||
#define Nextitem DohNextitem
|
#define Nextitem DohNextitem
|
||||||
|
#define Readline DohReadline
|
||||||
|
|
||||||
/* #define Scanf DohScanf
|
/* #define Scanf DohScanf
|
||||||
#define vScanf DohvScanf*/
|
#define vScanf DohvScanf*/
|
||||||
|
|
@ -270,7 +273,7 @@ extern void String_replace(DOH *s, DOH *token, DOH *rep, int flags);
|
||||||
* Files
|
* Files
|
||||||
* ----------------------------------------------------------------------------- */
|
* ----------------------------------------------------------------------------- */
|
||||||
|
|
||||||
extern DOH *NewFile(char *file, char *mode);
|
extern DOH *NewFile(DOH *file, char *mode);
|
||||||
extern DOH *NewFileFromFile(FILE *f);
|
extern DOH *NewFileFromFile(FILE *f);
|
||||||
extern DOH *NewFileFromFd(int fd);
|
extern DOH *NewFileFromFd(int fd);
|
||||||
extern int DohCopyto(DOH *input, DOH *output);
|
extern int DohCopyto(DOH *input, DOH *output);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue