Permalink
Browse files

kkrunchy_k7 Initial checkin

  • Loading branch information...
1 parent 0a3d41c commit ac7742d9480ae4123173a571fd46cc7bf62dcaeb @rygorous rygorous committed Apr 11, 2012
@@ -0,0 +1,14 @@
+
+
+/****************************************************************************/
+/*** ***/
+/*** System Configuration ***/
+/*** ***/
+/****************************************************************************/
+
+// Compile Options
+
+#define sINTRO 0 // compile for small size
+#define sDEBUG 1 // include debug code even in release build
+#define sAPPNAME "kkrunchy" // application name for window title
+#define sVERSION "0.23 alpha 3" // version number string
@@ -0,0 +1,264 @@
+#include "_types.hpp"
+#include "_startconsole.hpp"
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#include <crtdbg.h>
+#include <malloc.h>
+
+/****************************************************************************/
+/*** ***/
+/*** System Initialisation ***/
+/*** ***/
+/****************************************************************************/
+
+sSystem_ *sSystem;
+class sBroker_ *sBroker;
+
+/****************************************************************************/
+/****************************************************************************/
+
+sInt MemoryUsedCount;
+
+#if _DEBUG
+#if !sINTRO
+#undef new
+void * __cdecl operator new(unsigned int size,const char *file,int line)
+{
+ void *p;
+ p = _malloc_dbg(size,_NORMAL_BLOCK,file,line);
+ MemoryUsedCount+=_msize(p);
+ return p;
+}
+
+void operator delete(void *p)
+{
+ if(p)
+ {
+ MemoryUsedCount-=_msize(p);
+ _free_dbg(p,_NORMAL_BLOCK);
+ }
+}
+
+#define new new(__FILE__,__LINE__)
+#endif
+#endif
+
+#if sINTRO
+#if !_DEBUG
+void * __cdecl malloc(unsigned int size)
+{
+ return HeapAlloc(GetProcessHeap(),HEAP_NO_SERIALIZE,size);
+}
+
+void __cdecl free(void *ptr)
+{
+ HeapFree(GetProcessHeap(),HEAP_NO_SERIALIZE,ptr);
+}
+#endif
+
+void * __cdecl operator new(unsigned int size)
+{
+ return malloc(size);
+}
+
+void * __cdecl operator new[](unsigned int size)
+{
+ return malloc(size);
+}
+
+void __cdecl operator delete(void *ptr)
+{
+ if(ptr)
+ free(ptr);
+}
+
+void __cdecl operator delete[](void *ptr)
+{
+ if(ptr)
+ free(ptr);
+}
+
+int __cdecl _purecall()
+{
+ return 0;
+}
+
+#if !_DEBUG
+extern "C" int _fltused;
+int _fltused;
+#endif
+
+#endif
+
+/****************************************************************************/
+
+sInt main(sInt argc,sChar **argv)
+{
+ sInt ret;
+
+ sSystem = new sSystem_;
+ ret = sAppMain(argc,argv);
+ delete sSystem;
+
+ return ret;
+}
+
+/****************************************************************************/
+/*** ***/
+/*** Init/Exit/Debug ***/
+/*** ***/
+/****************************************************************************/
+
+void sSystem_::Log(sChar *s)
+{
+ OutputDebugString(s);
+}
+
+/****************************************************************************/
+
+sNORETURN void sSystem_::Abort(sChar *msg)
+{
+ _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG)&~(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF));
+ if(msg)
+ PrintF("\afatal error: %s\n",msg);
+ ExitProcess(0);
+}
+
+/****************************************************************************/
+
+void sSystem_::Tag()
+{
+}
+
+/****************************************************************************/
+/*** ***/
+/*** Console IO ***/
+/*** B ***/
+/****************************************************************************/
+
+void sSystem_::PrintF(sChar *format,...)
+{
+ sChar buffer[2048];
+ DWORD written;
+
+ sFormatString(buffer,2048,format,&format);
+ WriteFile(GetStdHandle(STD_OUTPUT_HANDLE),buffer,
+ sGetStringLen(buffer),&written,0);
+}
+
+/****************************************************************************/
+/*** ***/
+/*** File ***/
+/*** ***/
+/****************************************************************************/
+
+sU8 *sSystem_::LoadFile(sChar *name,sInt &size)
+{
+ sInt result;
+ HANDLE handle;
+ DWORD test;
+ sU8 *mem;
+
+ mem = 0;
+ result = sFALSE;
+ handle = CreateFile(name,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);
+ if(handle != INVALID_HANDLE_VALUE)
+ {
+ size = GetFileSize(handle,&test);
+ if(test==0)
+ {
+ mem = new sU8[size];
+ if(ReadFile(handle,mem,size,&test,0))
+ result = sTRUE;
+ if(size!=(sInt)test)
+ result = sFALSE;
+ }
+ CloseHandle(handle);
+ }
+
+ if(!result)
+ {
+ if(mem)
+ delete[] mem;
+ mem = 0;
+ }
+
+ return mem;
+}
+
+/****************************************************************************/
+
+sU8 *sSystem_::LoadFile(sChar *name)
+{
+ sInt dummy;
+ return LoadFile(name,dummy);
+}
+
+/****************************************************************************/
+
+sChar *sSystem_::LoadText(sChar *name)
+{
+ sInt result;
+ HANDLE handle;
+ DWORD test;
+ sU8 *mem;
+ sInt size;
+
+ mem = 0;
+ result = sFALSE;
+ handle = CreateFile(name,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);
+ if(handle != INVALID_HANDLE_VALUE)
+ {
+ size = GetFileSize(handle,&test);
+ if(test==0)
+ {
+ mem = new sU8[size+1];
+ if(ReadFile(handle,mem,size,&test,0))
+ result = sTRUE;
+ if(size!=(sInt)test)
+ result = sFALSE;
+ mem[size]=0;
+ }
+ CloseHandle(handle);
+ }
+
+ if(!result)
+ {
+ delete[] mem;
+ mem = 0;
+ }
+
+ return (sChar *)mem;}
+
+/****************************************************************************/
+
+sBool sSystem_::SaveFile(sChar *name,sU8 *data,sInt size)
+{
+ sInt result;
+ HANDLE handle;
+ DWORD test;
+
+ result = sFALSE;
+ handle = CreateFile(name,GENERIC_WRITE,FILE_SHARE_WRITE,0,CREATE_NEW,0,0);
+ if(handle == INVALID_HANDLE_VALUE)
+ handle = CreateFile(name,GENERIC_WRITE,FILE_SHARE_WRITE,0,TRUNCATE_EXISTING,0,0);
+ if(handle != INVALID_HANDLE_VALUE)
+ {
+ if(WriteFile(handle,data,size,&test,0))
+ result = sTRUE;
+ if(size!=(sInt)test)
+ result = sFALSE;
+ CloseHandle(handle);
+ }
+
+ return result;
+}
+
+/****************************************************************************/
+
+sInt sSystem_::GetTime()
+{
+ return GetTickCount();
+}
+
+/****************************************************************************/
@@ -0,0 +1,47 @@
+#ifndef __STARTCONSOLE_HPP__
+#define __STARTCONSOLE_HPP__
+
+/****************************************************************************/
+/*** ***/
+/*** Forwards ***/
+/*** ***/
+/****************************************************************************/
+
+/****************************************************************************/
+/*** ***/
+/*** Main Program ***/
+/*** ***/
+/****************************************************************************/
+
+// implement this to write a program
+
+sInt sAppMain(sInt argc,sChar **argv);
+
+/****************************************************************************/
+
+struct sSystem_
+{
+// init/exit/debug
+
+ void Log(sChar *); // print out debug string
+ sNORETURN void Abort(sChar *msg); // terminate now
+ void Tag(); // called by broker (in a direct hackish fashion)
+
+// console io
+
+ void PrintF(sChar *format,...);
+
+// file
+
+ sU8 *LoadFile(sChar *name,sInt &size); // load file entirely, return size
+ sU8 *LoadFile(sChar *name); // load file entirely
+ sChar *LoadText(sChar *name); // load file entirely and add trailing zero
+ sBool SaveFile(sChar *name,sU8 *data,sInt size); // save file entirely
+
+// misc
+ sInt GetTime();
+};
+
+extern sSystem_ *sSystem;
+
+#endif
Oops, something went wrong.

0 comments on commit ac7742d

Please sign in to comment.