Skip to content
This repository
Browse code

kkrunchy Initial checkin

  • Loading branch information...
commit 1d13324af2596c47c38ad0a9f02244c22c854e6e 1 parent ebd813b
Fabian 'ryg' Giesen authored April 10, 2012
14  kkrunchy/_config.hpp
... ...
@@ -0,0 +1,14 @@
  1
+
  2
+
  3
+/****************************************************************************/
  4
+/***                                                                      ***/
  5
+/***   System Configuration                                               ***/
  6
+/***                                                                      ***/
  7
+/****************************************************************************/
  8
+
  9
+// Compile Options
  10
+
  11
+#define sINTRO                  0 // compile for small size
  12
+#define sDEBUG                  1 // include debug code even in release build
  13
+#define sAPPNAME    "kkrunchy"    // application name for window title
  14
+#define sVERSION    "0.23 alpha"  // version number string
264  kkrunchy/_startconsole.cpp
... ...
@@ -0,0 +1,264 @@
  1
+#include "_types.hpp"
  2
+#include "_startconsole.hpp"
  3
+#define WIN32_LEAN_AND_MEAN
  4
+#include <windows.h>
  5
+#include <crtdbg.h>
  6
+#include <malloc.h>
  7
+
  8
+/****************************************************************************/
  9
+/***                                                                      ***/
  10
+/***   System Initialisation                                              ***/
  11
+/***                                                                      ***/
  12
+/****************************************************************************/
  13
+
  14
+sSystem_ *sSystem;
  15
+class sBroker_ *sBroker;
  16
+
  17
+/****************************************************************************/
  18
+/****************************************************************************/
  19
+
  20
+sInt MemoryUsedCount;
  21
+
  22
+#if _DEBUG
  23
+#if !sINTRO
  24
+#undef new
  25
+void * __cdecl operator new(unsigned int size,const char *file,int line)
  26
+{
  27
+  void *p;
  28
+  p = _malloc_dbg(size,_NORMAL_BLOCK,file,line);
  29
+  MemoryUsedCount+=_msize(p); 
  30
+  return p;
  31
+}
  32
+
  33
+void operator delete(void *p)
  34
+{
  35
+	if(p)
  36
+	{
  37
+		MemoryUsedCount-=_msize(p); 
  38
+		_free_dbg(p,_NORMAL_BLOCK);
  39
+	}
  40
+}
  41
+
  42
+#define new new(__FILE__,__LINE__)
  43
+#endif
  44
+#endif
  45
+
  46
+#if sINTRO
  47
+#if !_DEBUG
  48
+void * __cdecl malloc(unsigned int size)
  49
+{
  50
+	return HeapAlloc(GetProcessHeap(),HEAP_NO_SERIALIZE,size);
  51
+}
  52
+
  53
+void __cdecl free(void *ptr)
  54
+{
  55
+	HeapFree(GetProcessHeap(),HEAP_NO_SERIALIZE,ptr);
  56
+}
  57
+#endif
  58
+
  59
+void * __cdecl operator new(unsigned int size)
  60
+{
  61
+	return malloc(size);
  62
+}
  63
+
  64
+void * __cdecl operator new[](unsigned int size)
  65
+{
  66
+	return malloc(size);
  67
+}
  68
+
  69
+void __cdecl operator delete(void *ptr)
  70
+{
  71
+	if(ptr)
  72
+		free(ptr);
  73
+}
  74
+
  75
+void __cdecl operator delete[](void *ptr)
  76
+{
  77
+	if(ptr)
  78
+		free(ptr);
  79
+}
  80
+
  81
+int __cdecl _purecall()
  82
+{
  83
+	return 0;
  84
+}
  85
+
  86
+#if !_DEBUG
  87
+extern "C" int _fltused;
  88
+int _fltused;
  89
+#endif
  90
+
  91
+#endif
  92
+
  93
+/****************************************************************************/
  94
+
  95
+sInt main(sInt argc,sChar **argv)
  96
+{
  97
+  sInt ret;
  98
+
  99
+  sSystem = new sSystem_;
  100
+  ret = sAppMain(argc,argv);
  101
+  delete sSystem;
  102
+
  103
+  return ret;
  104
+}
  105
+
  106
+/****************************************************************************/
  107
+/***                                                                      ***/
  108
+/***   Init/Exit/Debug                                                    ***/
  109
+/***                                                                      ***/
  110
+/****************************************************************************/
  111
+
  112
+void sSystem_::Log(sChar *s)
  113
+{
  114
+  OutputDebugString(s);
  115
+}
  116
+
  117
+/****************************************************************************/
  118
+
  119
+sNORETURN void sSystem_::Abort(sChar *msg)
  120
+{
  121
+  _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG)&~(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF));
  122
+  if(msg)
  123
+    PrintF("\afatal error: %s\n",msg);
  124
+  ExitProcess(0);
  125
+}
  126
+
  127
+/****************************************************************************/
  128
+
  129
+void sSystem_::Tag()
  130
+{
  131
+}
  132
+
  133
+/****************************************************************************/
  134
+/***                                                                      ***/
  135
+/***   Console IO                                                         ***/
  136
+/***                                                                      ***/
  137
+/****************************************************************************/
  138
+
  139
+void sSystem_::PrintF(sChar *format,...)
  140
+{
  141
+  sChar buffer[2048];
  142
+  DWORD written;
  143
+
  144
+  sFormatString(buffer,2048,format,&format);
  145
+  WriteFile(GetStdHandle(STD_OUTPUT_HANDLE),buffer,
  146
+    sGetStringLen(buffer),&written,0);
  147
+}
  148
+
  149
+/****************************************************************************/
  150
+/***                                                                      ***/
  151
+/***   File                                                               ***/
  152
+/***                                                                      ***/
  153
+/****************************************************************************/
  154
+
  155
+sU8 *sSystem_::LoadFile(sChar *name,sInt &size)
  156
+{
  157
+  sInt result;
  158
+  HANDLE handle;
  159
+  DWORD test;
  160
+  sU8 *mem;
  161
+   
  162
+  mem = 0;
  163
+  result = sFALSE;
  164
+  handle = CreateFile(name,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);
  165
+  if(handle != INVALID_HANDLE_VALUE)
  166
+  {
  167
+    size = GetFileSize(handle,&test);
  168
+    if(test==0)
  169
+    {
  170
+      mem = new sU8[size];
  171
+      if(ReadFile(handle,mem,size,&test,0))
  172
+        result = sTRUE;
  173
+      if(size!=(sInt)test)
  174
+        result = sFALSE;
  175
+    }
  176
+    CloseHandle(handle);
  177
+  }
  178
+
  179
+  if(!result)
  180
+  {
  181
+    if(mem)
  182
+      delete[] mem;
  183
+    mem = 0;
  184
+  }
  185
+
  186
+  return mem;
  187
+}
  188
+
  189
+/****************************************************************************/
  190
+
  191
+sU8 *sSystem_::LoadFile(sChar *name)
  192
+{
  193
+  sInt dummy;
  194
+  return LoadFile(name,dummy);
  195
+}
  196
+
  197
+/****************************************************************************/
  198
+
  199
+sChar *sSystem_::LoadText(sChar *name)
  200
+{
  201
+  sInt result;
  202
+  HANDLE handle;
  203
+  DWORD test;
  204
+  sU8 *mem;
  205
+  sInt size;
  206
+   
  207
+  mem = 0;
  208
+  result = sFALSE;
  209
+  handle = CreateFile(name,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);
  210
+  if(handle != INVALID_HANDLE_VALUE)
  211
+  {
  212
+    size = GetFileSize(handle,&test);
  213
+    if(test==0)
  214
+    {
  215
+      mem = new sU8[size+1];
  216
+      if(ReadFile(handle,mem,size,&test,0))
  217
+        result = sTRUE;
  218
+      if(size!=(sInt)test)
  219
+        result = sFALSE;
  220
+      mem[size]=0;
  221
+    }
  222
+    CloseHandle(handle);
  223
+  }
  224
+
  225
+  if(!result)
  226
+  {
  227
+    delete[] mem;
  228
+    mem = 0;
  229
+  }
  230
+
  231
+  return (sChar *)mem;}
  232
+
  233
+/****************************************************************************/
  234
+
  235
+sBool sSystem_::SaveFile(sChar *name,sU8 *data,sInt size)
  236
+{
  237
+  sInt result;
  238
+  HANDLE handle;
  239
+  DWORD test;
  240
+
  241
+  result = sFALSE;
  242
+  handle = CreateFile(name,GENERIC_WRITE,FILE_SHARE_WRITE,0,CREATE_NEW,0,0);  
  243
+  if(handle == INVALID_HANDLE_VALUE)
  244
+    handle = CreateFile(name,GENERIC_WRITE,FILE_SHARE_WRITE,0,TRUNCATE_EXISTING,0,0);  
  245
+  if(handle != INVALID_HANDLE_VALUE)
  246
+  {
  247
+    if(WriteFile(handle,data,size,&test,0))
  248
+      result = sTRUE;
  249
+    if(size!=(sInt)test)
  250
+      result = sFALSE;
  251
+    CloseHandle(handle);
  252
+  }
  253
+
  254
+  return result;
  255
+}
  256
+
  257
+/****************************************************************************/
  258
+
  259
+sInt sSystem_::GetTime()
  260
+{
  261
+  return GetTickCount();
  262
+}
  263
+
  264
+/****************************************************************************/
47  kkrunchy/_startconsole.hpp
... ...
@@ -0,0 +1,47 @@
  1
+#ifndef __STARTCONSOLE_HPP__
  2
+#define __STARTCONSOLE_HPP__
  3
+
  4
+/****************************************************************************/
  5
+/***                                                                      ***/
  6
+/***   Forwards                                                           ***/
  7
+/***                                                                      ***/
  8
+/****************************************************************************/
  9
+
  10
+/****************************************************************************/
  11
+/***                                                                      ***/
  12
+/***   Main Program                                                       ***/
  13
+/***                                                                      ***/
  14
+/****************************************************************************/
  15
+
  16
+// implement this to write a program
  17
+
  18
+sInt sAppMain(sInt argc,sChar **argv);
  19
+
  20
+/****************************************************************************/
  21
+
  22
+struct sSystem_
  23
+{
  24
+// init/exit/debug
  25
+
  26
+  void Log(sChar *);                                      // print out debug string
  27
+  sNORETURN void Abort(sChar *msg);                       // terminate now
  28
+  void Tag();                                             // called by broker (in a direct hackish fashion)
  29
+
  30
+// console io
  31
+
  32
+  void PrintF(sChar *format,...);
  33
+
  34
+// file
  35
+
  36
+  sU8 *LoadFile(sChar *name,sInt &size);                  // load file entirely, return size
  37
+  sU8 *LoadFile(sChar *name);                             // load file entirely
  38
+  sChar *LoadText(sChar *name);                           // load file entirely and add trailing zero
  39
+  sBool SaveFile(sChar *name,sU8 *data,sInt size);        // save file entirely
  40
+
  41
+// misc
  42
+  sInt GetTime();
  43
+};
  44
+
  45
+extern sSystem_ *sSystem;
  46
+
  47
+#endif
2,363  kkrunchy/_types.cpp
... ...
@@ -0,0 +1,2363 @@
  1
+#include "_types.hpp"
  2
+#include "_startconsole.hpp"
  3
+
  4
+/****************************************************************************/
  5
+/***                                                                      ***/
  6
+/***   Compiler                                                           ***/
  7
+/***                                                                      ***/
  8
+/****************************************************************************/
  9
+
  10
+/****************************************************************************/
  11
+/***                                                                      ***/
  12
+/***   Init/Exit                                                          ***/
  13
+/***                                                                      ***/
  14
+/****************************************************************************/
  15
+
  16
+void sDLLSYSTEM sInitTypes();
  17
+void sDLLSYSTEM sExitTypes();
  18
+#if sINTRO
  19
+extern class ScriptRuntime *SR__;
  20
+#endif
  21
+/****************************************************************************/
  22
+/***                                                                      ***/
  23
+/***   Forwards                                                           ***/
  24
+/***                                                                      ***/
  25
+/****************************************************************************/
  26
+
  27
+static sU32 sRandomSeed=0x74382381;
  28
+
  29
+sU32 sGetRnd()
  30
+{
  31
+  sU32 i;
  32
+
  33
+  _asm
  34
+  {
  35
+    mov eax,sRandomSeed
  36
+
  37
+    imul eax,eax,343fdh
  38
+    add eax,269ec3h
  39
+    mov ebx,eax
  40
+
  41
+    imul eax,eax,343fdh
  42
+    add eax,269ec3h
  43
+
  44
+    mov sRandomSeed,eax
  45
+
  46
+    sar eax,10
  47
+    and eax,00000ffffh
  48
+    shl ebx,6
  49
+    and ebx,0ffff0000h
  50
+    or eax,ebx
  51
+    mov i,eax
  52
+  }
  53
+
  54
+  return i;
  55
+}
  56
+
  57
+/****************************************************************************/
  58
+
  59
+sU32 sGetRnd(sU32 max)
  60
+{
  61
+  return sGetRnd()%max;
  62
+}
  63
+
  64
+/****************************************************************************/
  65
+
  66
+sF32 sFGetRnd()
  67
+{
  68
+  return ((sGetRnd()&0x3fffffff)*1.0f)/0x40000000;
  69
+}
  70
+
  71
+/****************************************************************************/
  72
+
  73
+sF32 sFGetRnd(sF32 max)
  74
+{
  75
+  return ((sGetRnd()&0x3fffffff)*max)/0x40000000;
  76
+}
  77
+
  78
+/****************************************************************************/
  79
+
  80
+void sSetRndSeed(sInt seed)
  81
+{
  82
+  sRandomSeed = seed+seed*17+seed*121+(seed*121/17);
  83
+  sGetRnd();  
  84
+  sRandomSeed ^= seed+seed*17+seed*121+(seed*121/17);
  85
+  sGetRnd();  
  86
+  sRandomSeed ^= seed+seed*17+seed*121+(seed*121/17);
  87
+  sGetRnd();  
  88
+  sRandomSeed ^= seed+seed*17+seed*121+(seed*121/17);
  89
+  sGetRnd();
  90
+}
  91
+
  92
+/****************************************************************************/
  93
+
  94
+sInt sMakePower2(sInt val)
  95
+{
  96
+  sInt p;
  97
+
  98
+  p = 1;
  99
+
  100
+  while(p<val)
  101
+    p = p*2;
  102
+  return p;
  103
+}
  104
+
  105
+/****************************************************************************/
  106
+
  107
+sInt sGetPower2(sInt val)
  108
+{
  109
+  sInt p;
  110
+
  111
+  p = 1;
  112
+
  113
+  while((1<<p)<val)
  114
+    p++;
  115
+  return p;
  116
+}
  117
+
  118
+/****************************************************************************/
  119
+
  120
+#if !sINTRO // this is inlined in spline class! 
  121
+void sHermite(sF32 *d,sF32 *p0,sF32 *p1,sF32 *p2,sF32 *p3,sInt count,sF32 fade,sF32 t,sF32 c,sF32 b)
  122
+{
  123
+  sF32 f1,f2,f3,f4;
  124
+  sF32 t0,t1,t2,t3;
  125
+  sF32 a1,b1,a2,b2;
  126
+  sInt i;
  127
+
  128
+  a1 = (1-t)*(1-c)*(1+b)/2;
  129
+  b1 = (1-t)*(1+c)*(1-b)/2;
  130
+  a2 = (1-t)*(1+c)*(1+b)/2;
  131
+  b2 = (1-t)*(1-c)*(1-b)/2;
  132
+
  133
+  t1 = p1[-1];
  134
+  t2 = p2[-1];
  135
+
  136
+  if(p0==0)
  137
+  {
  138
+    p0 = p2;
  139
+    a1 = -a1;
  140
+    t0 = t1+(t1-t2);
  141
+  }
  142
+  else
  143
+  {
  144
+    t0 = p0[-1];
  145
+  }
  146
+  if(p3==0)
  147
+  {
  148
+    p3 = p1;
  149
+    b2 = -b2;
  150
+    t3 = t2+(t2-t1);
  151
+  }
  152
+  else
  153
+  {
  154
+    t3 = p3[-1];
  155
+  }
  156
+
  157
+  f1 =  2*fade*fade*fade - 3*fade*fade + 1;
  158
+  f2 = -2*fade*fade*fade + 3*fade*fade;
  159
+  f3 =    fade*fade*fade - 2*fade*fade + fade;
  160
+  f4 =    fade*fade*fade -   fade*fade;
  161
+ 
  162
+  t0 = t1-t0;
  163
+  t1 = t2-t1;
  164
+  t2 = t3-t2;
  165
+  f3 = f3 * 2*t1/(t1+t0);
  166
+  f4 = f4 * 2*t1/(t1+t2);
  167
+
  168
+  sVERIFY(count<=9);
  169
+
  170
+  for(i=0;i<count;i++)
  171
+  {
  172
+    d[i] = f1 * p1[i]
  173
+         + f2 * p2[i]
  174
+         + f3 * (a1*(p1[i]-p0[i]) + b1*(p2[i]-p1[i]))
  175
+         + f4 * (a2*(p2[i]-p1[i]) + b2*(p3[i]-p2[i]));
  176
+  }
  177
+}
  178
+#endif
  179
+/*
  180
+sInt sRangeInt(sInt a,sInt b,sInt c)
  181
+{
  182
+  return sRange(a,b,c);
  183
+}
  184
+sF32 sRangeF32(sF32 a,sF32 b,sF32 c)
  185
+{
  186
+  return sRange(a,b,c);
  187
+}
  188
+*/
  189
+#if sINTRO
  190
+
  191
+/****************************************************************************/
  192
+
  193
+sF64 sFPow(sF64 a,sF64 b)
  194
+{
  195
+	__asm
  196
+	{
  197
+		fld		qword ptr [a];
  198
+		fld		qword ptr [b];
  199
+
  200
+		fxch	st(1);
  201
+		ftst;
  202
+		fstsw	ax;
  203
+		sahf;
  204
+		jz		zero;
  205
+
  206
+		fyl2x;
  207
+		fld1;
  208
+		fld		st(1);
  209
+		fprem;
  210
+		f2xm1;
  211
+		faddp	st(1), st;
  212
+		fscale;
  213
+
  214
+zero:
  215
+		fstp	st(1);
  216
+		fstp	qword ptr [a];
  217
+	}
  218
+	
  219
+	return a;
  220
+}
  221
+
  222
+sF64 sFExp(sF64 f)
  223
+{
  224
+	__asm
  225
+	{
  226
+		fld		qword ptr [f];
  227
+		fldl2e;
  228
+		fmulp	st(1), st;
  229
+
  230
+		fld1;
  231
+		fld		st(1);
  232
+		fprem;
  233
+		f2xm1;
  234
+		faddp	st(1), st;
  235
+		fscale;
  236
+
  237
+    fstp  st(1);
  238
+		fstp	qword ptr [f];
  239
+	}
  240
+
  241
+	return f;
  242
+}
  243
+
  244
+
  245
+#endif
  246
+
  247
+
  248
+/****************************************************************************/
  249
+/***                                                                      ***/
  250
+/***   Memory and String                                                  ***/
  251
+/***                                                                      ***/
  252
+/****************************************************************************/
  253
+
  254
+void sCopyMem4(sU32 *d,sU32 *s,sInt c)
  255
+{
  256
+  __asm
  257
+  {
  258
+    mov esi,[s]
  259
+    mov edi,[d]
  260
+    mov ecx,[c]
  261
+    rep movsd
  262
+  }
  263
+}
  264
+
  265
+#if !sINTRO
  266
+void sCopyMem8(sU64 *d,sU64 *s,sInt c)  
  267
+{
  268
+  __asm
  269
+  {
  270
+    mov esi,[s]
  271
+    mov edi,[d]
  272
+    mov ecx,[c]
  273
+    add ecx,ecx
  274
+    rep movsd
  275
+  }
  276
+}
  277
+#endif
  278
+
  279
+#if !sINTRO
  280
+
  281
+sChar *sDupString(sChar *s,sInt minsize)
  282
+{
  283
+  sInt size;
  284
+  sChar *d;
  285
+
  286
+  if(s==0)
  287
+    return 0;
  288
+
  289
+  size = sGetStringLen(s)+1;
  290
+  if(minsize>size)
  291
+    size = minsize;
  292
+
  293
+  d = new sChar[size];
  294
+  sCopyString(d,s,size);
  295
+  return d;
  296
+}
  297
+
  298
+#endif
  299
+
  300
+/****************************************************************************/
  301
+
  302
+#if !sINTRO || !sRELEASE
  303
+void sCopyString(sChar *d,sChar *s,sInt size)
  304
+{
  305
+  while(size>0 && *s)
  306
+  {
  307
+    size--;
  308
+    *d++ = *s++;
  309
+  }
  310
+  *d = 0;
  311
+}
  312
+#endif
  313
+
  314
+
  315
+/****************************************************************************/
  316
+
  317
+#if !sINTRO
  318
+
  319
+void sAppendString(sChar *d,sChar *s,sInt size)
  320
+{
  321
+  while(size>0 && *d)
  322
+  {
  323
+    size--;
  324
+    d++;
  325
+  }
  326
+  while(size>0 && *s)
  327
+  {
  328
+    size--;
  329
+    *d++ = *s++;
  330
+  }
  331
+  *d = 0;
  332
+}
  333
+
  334
+/****************************************************************************/
  335
+
  336
+void sParentString(sChar *path)
  337
+{
  338
+  sChar *s,*e;
  339
+
  340
+  e = path;
  341
+  s = path;
  342
+  while(s[0] && s[1])
  343
+  {
  344
+    if(*s=='/' && (s!=path || s[-1]!=':'))
  345
+      e = s+1;
  346
+    s++;
  347
+  }
  348
+
  349
+  *e = 0;
  350
+}
  351
+
  352
+/****************************************************************************/
  353
+
  354
+sInt sCmpString(sChar *a,sChar *b)
  355
+{
  356
+  sInt aa,bb;
  357
+  do
  358
+  {
  359
+    aa = *a++;
  360
+    bb = *b++;
  361
+  }
  362
+  while(aa!=0 && aa==bb);
  363
+  return sSign(aa-bb);
  364
+}
  365
+
  366
+sInt sCmpStringI(sChar *a,sChar *b)
  367
+{
  368
+  sInt aa,bb;
  369
+  do
  370
+  {
  371
+    aa = *a++;
  372
+    bb = *b++;
  373
+    if(aa>='A' && aa<='Z') aa=aa-'A'+'a';
  374
+    if(bb>='A' && bb<='Z') bb=bb-'A'+'a';
  375
+  }
  376
+  while(aa!=0 && aa==bb);
  377
+  return sSign(aa-bb);
  378
+}
  379
+
  380
+sChar *sFindString(sChar *s,sChar *f)
  381
+{
  382
+  sInt len,i,slen;
  383
+
  384
+  len = sGetStringLen(f);
  385
+  slen = sGetStringLen(s);
  386
+  for(i=0;i<=slen-len;i++)
  387
+  {
  388
+    if(sCmpMem(s+i,f,len)==0)
  389
+      return s+i;
  390
+  }
  391
+  return 0;
  392
+}
  393
+/*
  394
+sChar *sFindStringI(sChar *s,sChar *f)
  395
+{
  396
+  sInt len,i,slen;
  397
+
  398
+  len = sGetStringLen(f);
  399
+  slen = sGetStringLen(s);
  400
+  for(i=0;i<=slen-len;i++)
  401
+  {
  402
+    if(sCmpMemI(s+i,f,len)==0)
  403
+      return s+i;
  404
+  }
  405
+  return 0;
  406
+}
  407
+*/
  408
+#endif
  409
+/****************************************************************************/
  410
+/****************************************************************************/
  411
+
  412
+#if !sINTRO
  413
+sInt sScanInt(sChar *&scan)
  414
+{
  415
+  sInt val; 
  416
+  sInt sign;
  417
+
  418
+  sign = 1;
  419
+  val = 0;
  420
+
  421
+  if(*scan=='\'')
  422
+  {
  423
+    if(scan[1]!=0 && scan[2]=='\'')
  424
+    {
  425
+      val = scan[1];
  426
+      scan+=3;
  427
+      return val;
  428
+    }
  429
+    return 0;
  430
+  }
  431
+
  432
+  if(*scan=='+')
  433
+    scan++;
  434
+  else if(*scan=='-')
  435
+  {
  436
+    sign = -1;
  437
+    scan++;
  438
+  }
  439
+
  440
+  if(*scan=='$' || *scan=='#')
  441
+  {
  442
+    scan++;
  443
+    for(;;)
  444
+    {
  445
+      if(*scan>='0' && *scan<='9')
  446
+        val = val*16+(*scan)-'0';
  447
+      else if(*scan>='a' && *scan<='f')
  448
+        val = val*16+(*scan)-'a'+10;
  449
+      else if(*scan>='A' && *scan<='F')
  450
+        val = val*16+(*scan)-'A'+10;
  451
+      else
  452
+        break;
  453
+      scan++;
  454
+    }
  455
+  }
  456
+  else
  457
+  {
  458
+    while(*scan>='0' && *scan<='9')
  459
+      val = val*10+(*scan++)-'0';
  460
+  }
  461
+
  462
+  return val*sign;
  463
+}
  464
+
  465
+
  466
+sInt sScanHex(sChar *&scan)
  467
+{
  468
+  sInt val; 
  469
+  sInt sign;
  470
+
  471
+  sign = 1;
  472
+  val = 0;
  473
+
  474
+  if(*scan=='+')
  475
+    scan++;
  476
+  else if(*scan=='-')
  477
+  {
  478
+    sign = -1;
  479
+    scan++;
  480
+  }
  481
+
  482
+  for(;;)
  483
+  {
  484
+    if(*scan>='0' && *scan<='9')
  485
+      val = val*16+(*scan)-'0';
  486
+    else if(*scan>='a' && *scan<='f')
  487
+      val = val*16+(*scan)-'a'+10;
  488
+    else if(*scan>='A' && *scan<='F')
  489
+      val = val*16+(*scan)-'A'+10;
  490
+    else
  491
+      break;
  492
+    scan++;
  493
+  }
  494
+
  495
+  return val*sign;
  496
+}
  497
+
  498
+/****************************************************************************/
  499
+
  500
+sF32 sScanFloat(sChar *&scan)
  501
+{
  502
+  sF64 val;
  503
+  sInt sign;
  504
+  sF64 dec;
  505
+
  506
+  sign = 1;
  507
+  val = 0;
  508
+
  509
+  if(*scan=='+')
  510
+    scan++;
  511
+  else if(*scan=='-')
  512
+  {
  513
+    sign = -1;
  514
+    scan++;
  515
+  }
  516
+
  517
+  while(*scan>='0' && *scan<='9')
  518
+    val = val*10+(*scan++)-'0';
  519
+  if(*scan=='.')
  520
+  {
  521
+    scan++;
  522
+    dec = 1.0;
  523
+    while(*scan>='0' && *scan<='9')
  524
+    {
  525
+      dec = dec/10.0;
  526
+      val = val + ((*scan++)-'0')*dec;
  527
+    }
  528
+  }
  529
+
  530
+  return val*sign;
  531
+}
  532
+
  533
+/****************************************************************************/
  534
+
  535
+sBool sScanString(sChar *&scan,sChar *buffer,sInt size)
  536
+{
  537
+  if(*scan!='"')
  538
+    return sFALSE;
  539
+  scan++;
  540
+  while(*scan!='"' && *scan!=0 && *scan!='\r' && *scan!='\n' && size>1)
  541
+  {
  542
+    *buffer++ = *scan++;
  543
+    size--;
  544
+  }
  545
+  *buffer++ = 0;
  546
+  if(*scan=='"')
  547
+  {
  548
+    scan++;
  549
+    return sTRUE;
  550
+  }
  551
+  else
  552
+    return sFALSE;
  553
+}
  554
+
  555
+/****************************************************************************/
  556
+
  557
+sBool sScanName(sChar *&scan,sChar *buffer,sInt size)
  558
+{
  559
+  sBool done;
  560
+
  561
+  done = sFALSE;
  562
+
  563
+  if(!((*scan>='a' && *scan<='z') || 
  564
+       (*scan>='A' && *scan<='Z') || *scan=='_' )) 
  565
+    return sFALSE;
  566
+
  567
+  *buffer++ = *scan++;
  568
+  size--;
  569
+
  570
+  while(size>1)
  571
+  {
  572
+    if(!((*scan>='a' && *scan<='z') || 
  573
+         (*scan>='A' && *scan<='Z') || 
  574
+         (*scan>='0' && *scan<='9') || *scan=='_' )) 
  575
+    {
  576
+      *buffer++=0;
  577
+      return sTRUE;
  578
+    }
  579
+    *buffer++ = *scan++;
  580
+    size--;
  581
+  }
  582
+
  583
+  *buffer++=0;
  584
+  return sFALSE;
  585
+}
  586
+
  587
+/****************************************************************************/
  588
+
  589
+void sScanSpace(sChar *&scan)
  590
+{
  591
+  while(*scan==' ' || *scan=='\t' || *scan=='\r' || *scan=='\n')
  592
+    scan++;
  593
+}
  594
+
  595
+/****************************************************************************/
  596
+
  597
+sBool sScanCycle(sChar *s,sInt index,sInt &start,sInt &len)
  598
+{
  599
+  start = 0;
  600
+  len = 0;
  601
+  while(index>0)
  602
+  {
  603
+    while(s[start]!='|' && s[start]!=0)
  604
+      start++;
  605
+    if(s[start]!='|')
  606
+      return sFALSE;
  607
+    start++;
  608
+    index--;
  609
+  }
  610
+  while(s[start+len]!='|' && s[start+len]!=0)
  611
+    len++;
  612
+  return sTRUE;
  613
+}
  614
+
  615
+#endif
  616
+
  617
+/****************************************************************************/
  618
+/***                                                                      ***/
  619
+/***   Formatted Printing                                                 ***/
  620
+/***                                                                      ***/
  621
+/****************************************************************************/
  622
+
  623
+#if !sINTRO || _DEBUG
  624
+
  625
+extern "C" char * __cdecl _fcvt( double value, int count, int *dec, int *sign );
  626
+
  627
+void sFormatString(sChar *d,sInt left,sChar *s,sChar **fp)
  628
+{
  629
+  sInt c;
  630
+  sInt field0;
  631
+  sInt field1;
  632
+  sInt minus;
  633
+  sInt null;
  634
+  sInt len;
  635
+  sChar buffer[64];
  636
+  sChar *string;
  637
+  sInt val;
  638
+  sInt arg;
  639
+  sInt sign;
  640
+  sInt i;
  641
+  sF64 fval;
  642
+  static sChar hex[17] = "0123456789abcdef";
  643
+  static sChar HEX[17] = "0123456789ABCDEF";
  644
+
  645
+  arg = 0;
  646
+  left--;
  647
+
  648
+  c = *s++;
  649
+  while(c)
  650
+  {
  651
+    if(c=='%')
  652
+    {
  653
+      c = *s++;
  654
+
  655
+      minus = 0;
  656
+      null = 0;
  657
+      field0 = 0;
  658
+      field1 = 4;
  659
+
  660
+      if(c=='-')
  661
+      {
  662
+        minus = 1;
  663
+        c = *s++;
  664
+      }
  665
+      if(c=='0')
  666
+      {
  667
+        null = 1;
  668
+      }
  669
+      while(c>='0' && c<='9')
  670
+      {
  671
+        field0 = field0*10 + c - '0';
  672
+        c = *s++;
  673
+      }
  674
+      if(c=='.')
  675
+      {
  676
+        field1=0;
  677
+        c = *s++;
  678
+        while(c>='0' && c<='9')
  679
+        {
  680
+          field1 = field1*10 + c - '0';
  681
+          c = *s++;
  682
+        }
  683
+      }
  684
+
  685
+      if(c=='%')
  686
+      {
  687
+        c = *s++;
  688
+        if(left>0)
  689
+        {
  690
+          *d++ = '%';
  691
+          left--;
  692
+        }
  693
+      }
  694
+      else if(c=='d' || c=='x' || c=='X' || c=='i' || c=='f')
  695
+      {
  696
+        len = 0;
  697
+        sign = 0;
  698
+        if(c=='f')
  699
+        {
  700
+          fval = sVARARGF(fp,arg);arg+=2;
  701
+        }
  702
+        else
  703
+        {
  704
+          val = sVARARG(fp,arg);arg++;
  705
+        }
  706
+        if(c=='f')        // this is preliminary!!!!!!!
  707
+        {
  708
+#if sINTRO
  709
+          string = "???";
  710
+#else
  711
+          if(fval<0)
  712
+            field1++;
  713
+          string = _fcvt(fval,field1,&i,&sign);
  714
+#endif
  715
+          if(i<0)
  716
+          {
  717
+            buffer[len++]='.';
  718
+            while(i<=-1)
  719
+            {
  720
+              i++;
  721
+              buffer[len++]='0';
  722
+            }
  723
+            i=-1;
  724
+          }
  725
+          while(*string)
  726
+          {
  727
+            if(i==0)
  728
+              buffer[len++]='.';
  729
+            i--;
  730
+            buffer[len++]=*string++;
  731
+          }
  732
+        }
  733
+        else if(c=='d' || c=='i')
  734
+        {          
  735
+          if(val==0x80000000)
  736
+          {
  737
+            val = val/10;
  738
+            buffer[len++] = '8';
  739
+          }
  740
+          if(val<0)
  741
+          {
  742
+            sign = 1;
  743
+            val = -val;
  744
+          }
  745
+          do
  746
+          {
  747
+            buffer[len++] = val%10+'0';
  748
+            val = val/10;
  749
+          }
  750
+          while(val!=0);
  751
+          if(sign)
  752
+            len++;
  753
+
  754
+        }
  755
+        else if(c=='x' || c=='X')
  756
+        {
  757
+          do
  758
+          {
  759
+            if(c=='x')
  760
+              buffer[len] = hex[val&15];
  761
+            else
  762
+              buffer[len] = HEX[val&15];
  763
+            val = (val>>4)&0x0fffffff;
  764
+            len++;
  765
+          }
  766
+          while(val!=0);
  767
+        }
  768
+
  769
+        if(!minus && !null)
  770
+        {
  771
+          while(field0>len && left>0)
  772
+          {
  773
+            *d++ = ' ';
  774
+            left--;
  775
+            field0--;
  776
+          }
  777
+        }
  778
+        if(sign && left>0)
  779
+        {
  780
+          *d++ = '-';
  781
+          left--;
  782
+          field0--;
  783
+          len--;
  784
+        }
  785
+        if(!minus && null)
  786
+        {
  787
+          while(field0>len && left>0)
  788
+          {
  789
+            *d++ = '0';
  790
+            left--;
  791
+            field0--;
  792
+          }
  793
+        }
  794
+        i = 0;
  795
+        while(len>0 && left>0)
  796
+        {
  797
+          len--;
  798
+          if(c=='f')
  799
+            *d++ = buffer[i++];
  800
+          else
  801
+            *d++ = buffer[len];
  802
+          left--;
  803
+          field0--;
  804
+        }
  805
+        if(!minus)
  806
+        {
  807
+          while(field0>len && left>0)
  808
+          {
  809
+            *d++ = ' ';
  810
+            left--;
  811
+            field0--;
  812
+          }
  813
+        }
  814
+        c = *s++;
  815
+      }
  816
+      else if(c=='c')
  817
+      {
  818
+        val = (sInt)sVARARG(fp,arg);arg++;
  819
+        if(left>0)
  820
+        {
  821
+          *d++ = val;
  822
+          left--;
  823
+        }
  824
+        c = *s++;
  825
+      }
  826
+      else if(c=='s')
  827
+      {
  828
+        string = (sChar *)sVARARG(fp,arg);arg++;
  829
+        len = sGetStringLen(string);
  830
+        if(field0<=len)
  831
+          field0=len;
  832
+        if(!minus)
  833
+        {
  834
+          while(field0>len && left>0)
  835
+          {
  836
+            *d++ = ' ';
  837
+            left--;
  838
+            field0--;
  839
+          }
  840
+        }
  841
+        while(*string && left>0)
  842
+        {
  843
+          *d++=*string++;
  844
+          left--;
  845
+        }
  846
+        if(minus)
  847
+        {
  848
+          while(field0>len && left>0)
  849
+          {
  850
+            *d++ = ' ';
  851
+            left--;
  852
+            field0--;
  853
+          }
  854
+        }
  855
+        c = *s++;
  856
+      }
  857
+      else if(c=='h' || c=='H') 
  858
+      {
  859
+        val = sVARARG(fp,arg);arg++;
  860