campo-sirio/gfm/scanfx.c
luca 3d88151f4d Patch level :2.1 nopatch
Files correlati     :gfm.dll
Ricompilazione Demo : [ ]
Commento            :riportate sulla 2.1 le correzioni sulla gfm.dll della 2.0


git-svn-id: svn://10.65.10.50/trunk@11815 c028cbd2-c16b-5b4b-a496-9718f37d4682
2004-03-09 09:49:17 +00:00

334 lines
7.0 KiB
C
Executable File

/* int _ScanfAux(wOpt, sBuffer, sFmt, arg,)
*
* ARGUMENT
* int wOpt;
* char *sFmt, *sBuffer;
* (?) arg;
*
* DESCRIPTION
* Auxiliary function called by ScanDecimal, StringScanDecimal,
* and FileScanDecimal, which does all the work. According to the format
* string sFmt, the arguments are read and stored in the appropriate
* locations, with DEC's read as strings and then converted to DEC's.
* wOpt tells the source of input: 1 for stdio, 2 for a string,
* and 3 for a file. sBuffer is the source string for StringScanDecimal,
* while sBuffer is a (char *) cast of the file pointer for file input.
*
* SIDE EFFECTS
* Each argument is set to the correct value.
*
* RETURNS
* The number of fields scanned, the return from scanf/fscanf/sscanf.
*
* POSSIBLE ERRORS
* GM_NULLPOINTER
* GM_NULLSTRING
* GM_INVALIDID
* GM_CNVRW
* GM_CNVRE
* GM_NAN
* GM_EOF
*
* AUTHOR
* Jared Levy
* Copyright (C) 1987-1990 Greenleaf Software Inc. All rights reserved.
*
* MODIFICATIONS
*
*
*/
#include <stdio.h>
#include <malloc.h>
#include "gm.h"
#include "gmsystem.h"
int _ScanfAux(int wOpt,char * sBuffer, char * sFmt, ELLIPSES)
{
char sNewFmt[256], cType, *pStrArr[21], *pTemp;
int *pInVal, pOutVal[21];
int wInArg, wOutArg, i, j, wPrec=-1, wPrecArr[21], wNumD=0,wTot=0;
int wValid;
mbool wfStar, wfFlagR=FALSE, wfFlagT=FALSE, wfRoundArr[21];
mbool wfRegChar, wfDecChar;
#ifndef LATTICE
#ifndef _LDATA
mbool wfFar;
#endif
#endif
DEC *pDecArr[21], *pRes, *pDec;
#ifdef _LDATA
long *pLong;
int *pLow;
#endif
if (wOpt>1 && !sBuffer) {
_MacErr(GM_NULLSTRING);
return(0);
}
/* create new variable list */
pInVal = (int *) &sFmt;
wInArg = sizeof(char *) / sizeof(int);
wOutArg = 0;
j=0;
i=0;
/* trace through format string */
while (sFmt[i]!='\0') {
sNewFmt[j]=sFmt[i]; /* copy character */
i++;
j++;
if (sFmt[i-1]=='%') { /* copy specification */
#ifndef LATTICE
#ifndef _LDATA
wfFar=FALSE;
#endif
#endif
if (sFmt[i]=='*') { /* star prefix */
sNewFmt[j]=sFmt[i];
i++;
j++;
wfStar=TRUE;
}
else
wfStar=FALSE;
while (sFmt[i]>='0'&&sFmt[i]<='9') { /* width */
sNewFmt[j]=sFmt[i];
i++;
j++;
}
if (sFmt[i]=='.') { /* precision for DEC */
i++;
wPrec=0;
while (sFmt[i]>='0'&&sFmt[i]<='9') {
wPrec=wPrec*10+sFmt[i]-'0';
i++;
}
}
#ifndef LATTICE
if (sFmt[i]=='F') { /* far pointers */
sNewFmt[j]=sFmt[i];
i++;
j++;
#ifndef _LDATA
wfFar=TRUE;
#endif
}
else if (sFmt[i]=='N') { /* near pointers */
sNewFmt[j]=sFmt[i];
i++;
j++;
}
#endif
if (sFmt[i]=='l') { /* long types */
sNewFmt[j]=sFmt[i];
i++;
j++;
}
#ifndef LATTICE
else if (sFmt[i]=='h') { /* short types */
sNewFmt[j]=sFmt[i];
i++;
j++;
}
#endif
while (sFmt[i]==cGMFmtRnd || sFmt[i]==cGMFmtTrunc) {
if (sFmt[i]==cGMFmtRnd)
wfFlagR=TRUE;
if (sFmt[i]==cGMFmtTrunc)
wfFlagT=TRUE;
i++;
}
cType=sFmt[i]; /* find type */
wfRegChar = (cType=='d'||cType=='o'||cType=='x'||
#ifdef TURBOC
cType=='F'||
#else
cType=='g'||
#endif
cType=='u'||cType=='n'||
#ifndef LATTICE
cType=='D'||cType=='O'||cType=='X'||cType=='i'||
cType=='I'||cType=='U'||cType=='E'||cType=='p'||
cType=='['||
#endif
#ifdef _MSC
cType=='G'||
#endif
cType=='f'||cType=='e'||cType=='c'||cType=='s');
wfDecChar = (cType==cGMFmt||cType==cGMFmtComma||
cType==cGMFmtSci||cType==cGMFmtSciE||
cType==cGMFmtEng||cType==cGMFmtEngE);
if (wfRegChar) { /* regular C type */
/* copy format string */
if (cType=='[')
while (sFmt[i]!=']'&&sFmt[i]!='\0') {
sNewFmt[j]=sFmt[i];
i++;
j++;
}
else {
sNewFmt[j]=cType;
i++;
j++;
}
/* copy pointer */
if (!wfStar) {
pOutVal[wOutArg]=pInVal[wInArg];
wOutArg++;
wInArg++;
#ifndef LATTICE
#ifndef _LDATA
if (wfFar) {
pOutVal[wOutArg]=pInVal[wInArg];
wInArg++;
wOutArg++;
}
#else
pOutVal[wOutArg]=pInVal[wInArg];
wInArg++;
wOutArg++;
#endif
#else
#ifdef _LDATA
pOutVal[wOutArg]=pInVal[wInArg];
wInArg++;
wOutArg++;
#endif
#endif
/* n's and *'s don't count towards total values */
if (cType!='n')
wTot++;
}
}
if (wfDecChar) { /* DEC type */
sNewFmt[j]='8'; /* limit string length to */
j++; /* 80 characters (buffer) */
sNewFmt[j]='0';
j++;
sNewFmt[j]='s'; /* put s in format string */
i++;
j++;
if (!wfStar) {
/* store DEC pointer */
#ifdef _LDATA
pLong = (long *) &pInVal[wInArg];
pDec = (DEC *) *pLong;
#else
pDec = (DEC *) pInVal[wInArg];
#endif
pDecArr[wNumD]=pDec;
/* create & store string pointer */
wInArg++;
pTemp=calloc(81,sizeof(char));
pStrArr[wNumD]=pTemp;
pTemp[0]='\0';
#ifdef _LDATA
pLow=(int *) &pTemp;
pOutVal[wOutArg]=*pLow;
wOutArg++;
pOutVal[wOutArg]=*(pLow+1);
wInArg++;
#else
pOutVal[wOutArg]=(int) pStrArr[wNumD];
#endif
wOutArg++;
/* store other information */
wPrecArr[wNumD]=wPrec;
wfRoundArr[wNumD]=wfFlagR || !wfFlagT;
wNumD++;
wTot++;
}
}
if (!wfRegChar && !wfDecChar) { /* neither */
sNewFmt[j]=sFmt[i];
i++;
j++;
}
wPrec=-1; /* reset flags */
wfFlagR=FALSE;
wfFlagT=FALSE;
}
}
sNewFmt[j]='\0';
/* scan input */
/* stdio input */
if (wOpt==1)
i=scanf(sNewFmt,
pOutVal[0], pOutVal[1], pOutVal[2], pOutVal[3], pOutVal[4],
pOutVal[5], pOutVal[6], pOutVal[7], pOutVal[8], pOutVal[9]
,pOutVal[10], pOutVal[11], pOutVal[12], pOutVal[13], pOutVal[14],
pOutVal[15], pOutVal[16], pOutVal[17], pOutVal[18], pOutVal[19]
);
/* string input */
if (wOpt==2)
i=sscanf(sBuffer, sNewFmt,
pOutVal[0], pOutVal[1], pOutVal[2], pOutVal[3], pOutVal[4],
pOutVal[5], pOutVal[6], pOutVal[7], pOutVal[8], pOutVal[9]
,pOutVal[10], pOutVal[11], pOutVal[12], pOutVal[13], pOutVal[14],
pOutVal[15], pOutVal[16], pOutVal[17], pOutVal[18], pOutVal[19]
);
/* file input */
if (wOpt==3)
i=fscanf((FILE *) sBuffer, sNewFmt,
pOutVal[0], pOutVal[1], pOutVal[2], pOutVal[3], pOutVal[4],
pOutVal[5], pOutVal[6], pOutVal[7], pOutVal[8], pOutVal[9]
,pOutVal[10], pOutVal[11], pOutVal[12], pOutVal[13], pOutVal[14],
pOutVal[15], pOutVal[16], pOutVal[17], pOutVal[18], pOutVal[19]
);
wValid=i;
/* convert DEC's from ascii & store */
for (j=0;j<wNumD;j++) {
if (!pDecArr[j])
{_MacErr(GM_NULLPOINTER);}
else if (pStrArr[j][0]!='\0') {
/* use appropriate rounding & precision specs */
wfGMRound=wfRoundArr[j];
wPrec=wPrecArr[j];
/* convert with appropriate routine */
if (wPrec==-1)
pRes = ConvAsciiToDecimal(pDecArr[j],
pStrArr[j]);
else
pRes = ConvAsciiToDecimalRound(
pDecArr[j], pStrArr[j], wPrec);
/* if pRes is null, don't count as valid input */
if (!pRes && i!=EOF)
wValid--;
}
/* free allocated string memory */
#ifdef LATTICE
free(pStrArr[j]);
#else
free(pStrArr[j]);
#endif
}
wfGMRound=TRUE;
if (i<wTot)
_MacErr(GM_EOF);
return(wValid);
}