Added scanf and sscanf to stdio

This commit is contained in:
drmortalwombat 2021-12-24 14:39:03 +01:00
parent 0dd6a0655c
commit 720e532d22
7 changed files with 623 additions and 9 deletions

View File

@ -1293,10 +1293,16 @@ int bm_transform(Bitmap * dbm, ClipRect * clip, int dx, int dy, int w, int h, Bi
{
long lsx = (long)sx << 16, lsy = (long)sy << 16;
char * ldp = dbm->data + dbm->cwidth * (dy & ~7) + (dx & ~7) + (dy & 7);
int dstride = dbm->cwidth * 8 - 8;
for(int y=0; y<h; y++)
{
long rsx = lsx, rsy = lsy;
char * dp = ldp;
char m = 0x80 >> (dx & 7);
for(int x=0; x<w; x++)
{
int ix = (int)(rsx >> 16);
@ -1304,13 +1310,26 @@ int bm_transform(Bitmap * dbm, ClipRect * clip, int dx, int dy, int w, int h, Bi
if (ix >= 0 && iy >= 0 && ix < sbm->width && iy < 8 * sbm->cheight)
{
bm_put(dbm, dx + x, dy + y, bm_get(sbm, ix, iy));
if (bm_get(sbm, ix, iy))
*dp |= m;
else
*dp &= ~m;
}
m >>= 1;
if (!m)
{
m = 0x80;
dp += 8;
}
rsx += (long)dxx << 8;
rsy += (long)dyx << 8;
}
ldp++;
if (((int)ldp & 7) == 0)
ldp += dstride;
lsx += (long)dxy << 8;
lsy += (long)dyy << 8;
}

View File

@ -555,3 +555,424 @@ int sprintf(char * str, const char * fmt, ...)
}
static inline bool isspace(char c)
{
return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
}
static int hexch(char cs)
{
if (cs >= '0' && cs <= '9')
return cs - '0';
else if (cs >= 'a' && cs <= 'f')
return cs - 'a' + 10;
else if (cs >= 'A' && cs <= 'F')
return cs - 'A' + 10;
else
return -1;
}
static const char * scanpat(const char * fmt, char * mask)
{
for(char i=0; i<32; i++)
mask[i] = 0;
bool negated = false;
char fc = *fmt++;
if (fc == '^')
{
negated = true;
fc = *fmt++;
}
do
{
char fd = fc;
char nc = *fmt++;
if (nc == '-')
{
nc = *fmt;
if (nc != ']')
{
fmt++;
fd = nc;
nc = *fmt++;
}
}
while (fc <= fd)
{
mask[fc >> 3] |= 0x01 << (fc & 7);
fc++;
}
fc = nc;
} while (fc && fc != ']');
if (fc == ']')
fmt++;
if (negated)
{
for(char i=4; i<32; i++)
mask[i] = ~mask[i];
}
return fmt;
}
int fpscanf(const char * fmt, int (* ffunc)(void * p), void * fparam, void ** params)
{
char fc, cs;
int nv = 0;
unsigned nch = 0
cs = ffunc(fparam);
nch++;
while (cs > 0 && (fc = *fmt++))
{
switch (fc)
{
case ' ':
while (cs > 0 && isspace(cs))
{
cs = ffunc(fparam);
nch++;
}
break;
case '%':
{
int width = 0x7fff;
bool issigned = true;
bool ignore = false;
bool islong = false;
unsigned base = 10;
fc = *fmt++;
if (fc == '*')
{
ignore = true;
fc = *fmt++;
}
if (fc >= '0' && fc <= '9')
{
width = (int)(fc - '0');
fc = *fmt++;
while (fc >= '0' && fc <= '9')
{
width = width * 10 + (int)(fc - '0');
fc = *fmt++;
}
}
if (fc == 'l')
{
islong = true;
fc = *fmt++;
}
switch (fc)
{
case 'n':
*(unsigned *)*params = nch;
params++;
nv++;
break;
case 'x':
base = 16;
case 'u':
issigned = false;
case 'i':
case 'd':
{
bool sign = false;
if (cs == '-')
{
sign = true;
cs = ffunc(fparam);
nch++;
}
else if (cs == '+')
{
cs = ffunc(fparam);
nch++;
}
int cv;
if ((cv = hexch(cs)) >= 0)
{
cs = ffunc(fparam);
nch++;
if (cv == 0 && cs == 'x')
{
base = 16;
cs = ffunc(fparam);
nch++;
}
#ifndef NOLONG
if (islong)
{
unsigned long vi = (unsigned long)cv;
while ((cv = hexch(cs)) >= 0)
{
vi = vi * base + (unsigned long)cv;
cs = ffunc(fparam);
nch++;
}
if (!ignore)
{
if (sign && issigned)
*(long *)*params = -(long)vi;
else
*(unsigned long *)*params = vi;
params++;
nv++;
}
}
else
#endif
{
unsigned vi = (unsigned)cv;
while ((cv = hexch(cs)) >= 0)
{
vi = vi * base + (unsigned)cv;
cs = ffunc(fparam);
nch++;
}
if (!ignore)
{
if (sign && issigned)
*(int *)*params = -(int)vi;
else
*(unsigned *)*params = vi;
params++;
nv++;
}
}
}
else
return nv;
} break;
#ifndef NOFLOAT
case 'f':
case 'e':
case 'g':
{
bool sign = false;
if (cs == '-')
{
sign = true;
cs = ffunc(fparam);
nch++;
}
else if (cs == '+')
{
cs = ffunc(fparam);
nch++;
}
if (cs >= '0' && cs <= '9' || cs == '.')
{
float vf = 0;
while (cs >= '0' && cs <= '9')
{
vf = vf * 10 + (int)(cs - '0');
cs = ffunc(fparam);
nch++;
}
if (cs == '.')
{
float digits = 1.0;
cs = ffunc(fparam);
while (cs >= '0' && cs <= '9')
{
vf = vf * 10 + (int)(cs - '0');
digits *= 10;
cs = ffunc(fparam);
nch++;
}
vf /= digits;
}
char e = 0;
bool eneg = false;
if (cs == 'e' || cs == 'E')
{
cs = ffunc(fparam);
nch++;
if (cs == '-')
{
eneg = true;
cs = ffunc(fparam);
nch++;
}
else if (cs == '+')
{
cs = ffunc(fparam);
nch++;
}
while (cs >= '0' && cs <= '9')
{
e = e * 10 + cs - '0';
cs = ffunc(fparam);
nch++;
}
}
if (!ignore)
{
if (e)
{
if (eneg)
{
while (e > 6)
{
vf /= 1000000.0;
e -= 6;
}
vf /= tpow10[e];
}
else
{
while (e > 6)
{
vf *= 1000000.0;
e -= 6;
}
vf *= tpow10[e];
}
}
if (sign)
*(float *)*params = -vf;
else
*(float *)*params = vf;
params++;
nv++;
}
}
else
return nv;
} break;
#endif
case 's':
{
char * pch = (char *)*params;
while (width > 0 && cs > 0 && !isspace(cs))
{
if (!ignore)
*pch++ = cs;
cs = ffunc(fparam);
nch++;
width--;
}
if (!ignore)
{
*pch = 0;
params++;
nv++;
}
} break;
case '[':
{
char pat[32];
fmt = scanpat(fmt, pat);
char * pch = (char *)*params;
while (width > 0 && (pat[cs >> 3] & (1 << (cs & 7))))
{
if (!ignore)
*pch++ = cs;
cs = ffunc(fparam);
nch++;
width--;
}
if (!ignore)
{
*pch = 0;
params++;
nv++;
}
} break;
case 'c':
{
char * pch = (char *)*params;
if (!ignore)
{
*pch = cs;
params++;
nv++;
}
cs = ffunc(fparam);
nch++;
} break;
}
} break;
default:
if (cs == fc)
{
cs = ffunc(fparam);
nch++;
}
else
return nv;
break;
}
}
return nv;
}
int sscanf_func(void * fparam)
{
char ** cp = (char **)fparam;
char c = **cp;
if (c)
(*cp)++;
return c;
}
int scanf_func(void * fparam)
{
return getchar();
}
int sscanf(const char * fmt, const char * str, ...)
{
return fpscanf(fmt, sscanf_func, &str, (void **)((&str) + 1));
}
int scanf(const char * fmt, ...)
{
return fpscanf(fmt, scanf_func, nullptr, (void **)((&fmt) + 1));
}

View File

@ -16,6 +16,10 @@ void printf(const char * fmt, ...);
int sprintf(char * str, const char * fmt, ...);
int scanf(const char * fmt, ...);
int sscanf(const char * fmt, const char * str, ...);
#pragma compile("stdio.c")
#endif

View File

@ -1,6 +1,9 @@
#ifndef STDLIB_H
#define STDLIB_H
extern const float tpow10[7];
void itoa(int n, char * s, unsigned radix);
void utoa(unsigned int n, char * s, unsigned radix);

View File

@ -75,7 +75,7 @@ int main(int argc, const char** argv)
DWORD length = ::GetModuleFileNameA(NULL, basePath, sizeof(basePath));
#else
printf("Starting oscar64 1.1.49\n");
printf("Starting oscar64 1.1.50\n");
#ifdef __APPLE__
uint32_t length = sizeof(basePath);

View File

@ -25,8 +25,8 @@ LANGUAGE LANG_ENGLISH, SUBLANG_NEUTRAL
//
VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,1,49,0
PRODUCTVERSION 1,1,49,0
FILEVERSION 1,1,50,0
PRODUCTVERSION 1,1,50,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
@ -43,12 +43,12 @@ BEGIN
BEGIN
VALUE "CompanyName", "oscar64"
VALUE "FileDescription", "oscar64 compiler"
VALUE "FileVersion", "1.1.49.0"
VALUE "FileVersion", "1.1.50.0"
VALUE "InternalName", "oscar64.exe"
VALUE "LegalCopyright", "Copyright (C) 2021"
VALUE "OriginalFilename", "oscar64.exe"
VALUE "ProductName", "oscar64"
VALUE "ProductVersion", "1.1.49.0"
VALUE "ProductVersion", "1.1.50.0"
END
END
BLOCK "VarFileInfo"

View File

@ -22,6 +22,12 @@
}
"Entry"
{
"MsmKey" = "8:_0819C3D33D0942EF9E319C754E6C43D8"
"OwnerKey" = "8:_UNDEFINED"
"MsmSig" = "8:_UNDEFINED"
}
"Entry"
{
"MsmKey" = "8:_0D8B657E4A954DBFAF14055CDFFB384C"
"OwnerKey" = "8:_UNDEFINED"
"MsmSig" = "8:_UNDEFINED"
@ -88,6 +94,12 @@
}
"Entry"
{
"MsmKey" = "8:_5ACC41BF427E47EBB9A4A66BD117FF80"
"OwnerKey" = "8:_UNDEFINED"
"MsmSig" = "8:_UNDEFINED"
}
"Entry"
{
"MsmKey" = "8:_636E67C8267143AA9FC128EB0AF85FC9"
"OwnerKey" = "8:_UNDEFINED"
"MsmSig" = "8:_UNDEFINED"
@ -124,6 +136,12 @@
}
"Entry"
{
"MsmKey" = "8:_97FBAA1E30F140628447208D520BC191"
"OwnerKey" = "8:_UNDEFINED"
"MsmSig" = "8:_UNDEFINED"
}
"Entry"
{
"MsmKey" = "8:_9D0D7A63D6C848CD85489D6E7C43AFAD"
"OwnerKey" = "8:_UNDEFINED"
"MsmSig" = "8:_UNDEFINED"
@ -202,6 +220,18 @@
}
"Entry"
{
"MsmKey" = "8:_D16004065A954CC7A91F72185792B436"
"OwnerKey" = "8:_UNDEFINED"
"MsmSig" = "8:_UNDEFINED"
}
"Entry"
{
"MsmKey" = "8:_D1EF53E7B029443F8EE2E8174F8201ED"
"OwnerKey" = "8:_UNDEFINED"
"MsmSig" = "8:_UNDEFINED"
}
"Entry"
{
"MsmKey" = "8:_DA28A07E7836459C99161100D7C102B8"
"OwnerKey" = "8:_UNDEFINED"
"MsmSig" = "8:_UNDEFINED"
@ -266,6 +296,12 @@
"OwnerKey" = "8:_UNDEFINED"
"MsmSig" = "8:_UNDEFINED"
}
"Entry"
{
"MsmKey" = "8:_FDDF38BFE6BA48C88ED8CB7EADD94159"
"OwnerKey" = "8:_UNDEFINED"
"MsmSig" = "8:_UNDEFINED"
}
}
"Configurations"
{
@ -373,6 +409,26 @@
"IsDependency" = "11:FALSE"
"IsolateTo" = "8:"
}
"{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_0819C3D33D0942EF9E319C754E6C43D8"
{
"SourcePath" = "8:..\\include\\gfx\\bitmap.h"
"TargetName" = "8:bitmap.h"
"Tag" = "8:"
"Folder" = "8:_E0C7A8689A4144D0842BCEDA85A07B5D"
"Condition" = "8:"
"Transitive" = "11:FALSE"
"Vital" = "11:TRUE"
"ReadOnly" = "11:FALSE"
"Hidden" = "11:FALSE"
"System" = "11:FALSE"
"Permanent" = "11:FALSE"
"SharedLegacy" = "11:FALSE"
"PackageAs" = "3:1"
"Register" = "3:1"
"Exclude" = "11:FALSE"
"IsDependency" = "11:FALSE"
"IsolateTo" = "8:"
}
"{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_0D8B657E4A954DBFAF14055CDFFB384C"
{
"SourcePath" = "8:..\\include\\crt.c"
@ -593,6 +649,26 @@
"IsDependency" = "11:FALSE"
"IsolateTo" = "8:"
}
"{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_5ACC41BF427E47EBB9A4A66BD117FF80"
{
"SourcePath" = "8:..\\include\\gfx\\vector3d.h"
"TargetName" = "8:vector3d.h"
"Tag" = "8:"
"Folder" = "8:_E0C7A8689A4144D0842BCEDA85A07B5D"
"Condition" = "8:"
"Transitive" = "11:FALSE"
"Vital" = "11:TRUE"
"ReadOnly" = "11:FALSE"
"Hidden" = "11:FALSE"
"System" = "11:FALSE"
"Permanent" = "11:FALSE"
"SharedLegacy" = "11:FALSE"
"PackageAs" = "3:1"
"Register" = "3:1"
"Exclude" = "11:FALSE"
"IsDependency" = "11:FALSE"
"IsolateTo" = "8:"
}
"{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_636E67C8267143AA9FC128EB0AF85FC9"
{
"SourcePath" = "8:..\\include\\c64\\kernalio.h"
@ -713,6 +789,26 @@
"IsDependency" = "11:FALSE"
"IsolateTo" = "8:"
}
"{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_97FBAA1E30F140628447208D520BC191"
{
"SourcePath" = "8:..\\include\\gfx\\tinyfont.h"
"TargetName" = "8:tinyfont.h"
"Tag" = "8:"
"Folder" = "8:_E0C7A8689A4144D0842BCEDA85A07B5D"
"Condition" = "8:"
"Transitive" = "11:FALSE"
"Vital" = "11:TRUE"
"ReadOnly" = "11:FALSE"
"Hidden" = "11:FALSE"
"System" = "11:FALSE"
"Permanent" = "11:FALSE"
"SharedLegacy" = "11:FALSE"
"PackageAs" = "3:1"
"Register" = "3:1"
"Exclude" = "11:FALSE"
"IsDependency" = "11:FALSE"
"IsolateTo" = "8:"
}
"{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_9D0D7A63D6C848CD85489D6E7C43AFAD"
{
"SourcePath" = "8:..\\include\\stdio.h"
@ -973,6 +1069,46 @@
"IsDependency" = "11:FALSE"
"IsolateTo" = "8:"
}
"{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_D16004065A954CC7A91F72185792B436"
{
"SourcePath" = "8:..\\include\\gfx\\vector3d.c"
"TargetName" = "8:vector3d.c"
"Tag" = "8:"
"Folder" = "8:_E0C7A8689A4144D0842BCEDA85A07B5D"
"Condition" = "8:"
"Transitive" = "11:FALSE"
"Vital" = "11:TRUE"
"ReadOnly" = "11:FALSE"
"Hidden" = "11:FALSE"
"System" = "11:FALSE"
"Permanent" = "11:FALSE"
"SharedLegacy" = "11:FALSE"
"PackageAs" = "3:1"
"Register" = "3:1"
"Exclude" = "11:FALSE"
"IsDependency" = "11:FALSE"
"IsolateTo" = "8:"
}
"{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_D1EF53E7B029443F8EE2E8174F8201ED"
{
"SourcePath" = "8:..\\include\\gfx\\tinyfont.c"
"TargetName" = "8:tinyfont.c"
"Tag" = "8:"
"Folder" = "8:_E0C7A8689A4144D0842BCEDA85A07B5D"
"Condition" = "8:"
"Transitive" = "11:FALSE"
"Vital" = "11:TRUE"
"ReadOnly" = "11:FALSE"
"Hidden" = "11:FALSE"
"System" = "11:FALSE"
"Permanent" = "11:FALSE"
"SharedLegacy" = "11:FALSE"
"PackageAs" = "3:1"
"Register" = "3:1"
"Exclude" = "11:FALSE"
"IsDependency" = "11:FALSE"
"IsolateTo" = "8:"
}
"{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_DA28A07E7836459C99161100D7C102B8"
{
"SourcePath" = "8:..\\include\\c64\\memmap.h"
@ -1173,6 +1309,26 @@
"IsDependency" = "11:FALSE"
"IsolateTo" = "8:"
}
"{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_FDDF38BFE6BA48C88ED8CB7EADD94159"
{
"SourcePath" = "8:..\\include\\gfx\\bitmap.c"
"TargetName" = "8:bitmap.c"
"Tag" = "8:"
"Folder" = "8:_E0C7A8689A4144D0842BCEDA85A07B5D"
"Condition" = "8:"
"Transitive" = "11:FALSE"
"Vital" = "11:TRUE"
"ReadOnly" = "11:FALSE"
"Hidden" = "11:FALSE"
"System" = "11:FALSE"
"Permanent" = "11:FALSE"
"SharedLegacy" = "11:FALSE"
"PackageAs" = "3:1"
"Register" = "3:1"
"Exclude" = "11:FALSE"
"IsDependency" = "11:FALSE"
"IsolateTo" = "8:"
}
}
"FileType"
{
@ -1242,6 +1398,17 @@
{
}
}
"{9EF0B969-E518-4E46-987F-47570745A589}:_E0C7A8689A4144D0842BCEDA85A07B5D"
{
"Name" = "8:gfx"
"AlwaysCreate" = "11:FALSE"
"Condition" = "8:"
"Transitive" = "11:FALSE"
"Property" = "8:_1113C161CA574BEF91886C572F021DFA"
"Folders"
{
}
}
}
}
}
@ -1262,15 +1429,15 @@
{
"Name" = "8:Microsoft Visual Studio"
"ProductName" = "8:oscar64"
"ProductCode" = "8:{DB2F9B75-4B42-43FE-8332-FBA90B563EED}"
"PackageCode" = "8:{BEE247BE-4092-4F85-8315-85AB43FA7450}"
"ProductCode" = "8:{05496EAE-CB95-43D8-B470-9BAFABAEFDC8}"
"PackageCode" = "8:{E0B052C3-44C4-4A66-8BC9-4120C4A81063}"
"UpgradeCode" = "8:{9AB61EFF-ACAC-4079-9950-8D96615CD4EF}"
"AspNetVersion" = "8:2.0.50727.0"
"RestartWWWService" = "11:FALSE"
"RemovePreviousVersions" = "11:TRUE"
"DetectNewerInstalledVersion" = "11:TRUE"
"InstallAllUsers" = "11:FALSE"
"ProductVersion" = "8:1.1.49"
"ProductVersion" = "8:1.1.50"
"Manufacturer" = "8:oscar64"
"ARPHELPTELEPHONE" = "8:"
"ARPHELPLINK" = "8:"