home *** CD-ROM | disk | FTP | other *** search
/ PC Administrator / spravce.iso / TaskModule / src / smlcrt.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-07  |  5.4 KB  |  212 lines

  1. /*
  2. Copyright 2001 Anish Mistry. All rights reserved.
  3.  
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6.  
  7.    1. Redistributions of source code must retain the above copyright notice, 
  8.    this list of conditions and the following disclaimer.
  9.    2. Redistributions in binary form must reproduce the above copyright notice,
  10.    this list of conditions and the following disclaimer in the documentation 
  11.    and/or other materials provided with the distribution.
  12.  
  13. THIS SOFTWARE IS PROVIDED BY ANISH MISTRY ``AS IS'' AND ANY EXPRESS OR IMPLIED 
  14. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
  15. AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS 
  16. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  17. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  18. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  19. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  20. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22.  
  23. The views and conclusions contained in the software and documentation are those
  24. of the authors and should not be interpreted as representing official policies,
  25. either expressed or implied, of Anish Mistry or AM Productions.
  26.  
  27. * Variation of the FreeBSD License. http://www.freebsd.org/copyright/freebsd-license.html
  28. */
  29.  
  30. #include "stdafx.h"
  31. #include "smlcrt.h"
  32. /*
  33. void* __cdecl operator new(unsigned int cb)
  34. {
  35.   return HeapAlloc(GetProcessHeap(),0,cb);
  36. }
  37.  
  38. void __cdecl operator delete(void* pv)
  39. {
  40.   if(pv)
  41.     HeapFree(GetProcessHeap(),0,pv);
  42. }
  43. */
  44. extern "C" int _cdecl _purecall(void)
  45. {
  46.   return 0;
  47. }
  48.  
  49. inline unsigned int Pow(int num,int power)
  50. {
  51.     if(power == 0)
  52.         return 1;
  53.     int tempNum = num;
  54. //    int tempPower = power;
  55.     while(power != 1)
  56.     {
  57.         tempNum *= num;
  58.         power--;
  59.     }
  60.     return tempNum;
  61. }
  62. /*
  63. // function definitions
  64. int __CxxFrameHandler(void )
  65. {
  66.     return 1;
  67. }
  68.  
  69. int atexit( void ( __cdecl *func )( void ) )
  70. {
  71.     return true;
  72.  
  73. }*/
  74.  
  75. void __cdecl WinMainCRTStartup( void )
  76. {
  77.  
  78.     int mainret;
  79.     char *lpszCommandLine;
  80.     STARTUPINFO StartupInfo;
  81.  
  82.     lpszCommandLine = GetCommandLine();
  83.  
  84.     // Skip past program name (first token in command line).
  85.  
  86.     if ( *lpszCommandLine == '"' )  //Check for and handle quoted program name
  87.     {
  88.         // Scan, and skip over, subsequent characters until  another
  89.         // double-quote or a null is encountered
  90.  
  91.         while( *lpszCommandLine && (*lpszCommandLine != '"') )
  92.             lpszCommandLine++;
  93.  
  94.         // If we stopped on a double-quote (usual case), skip over it.
  95.  
  96.         if ( *lpszCommandLine == '"' )
  97.             lpszCommandLine++;
  98.     }
  99.     else    // First token wasn't a quote
  100.     {
  101.         while ( *lpszCommandLine > ' ' )
  102.             lpszCommandLine++;
  103.     }
  104.  
  105.     // Skip past any white space preceeding the second token.
  106.  
  107.     while ( *lpszCommandLine && (*lpszCommandLine <= ' ') )
  108.         lpszCommandLine++;
  109.  
  110.     StartupInfo.dwFlags = 0;
  111.     GetStartupInfo( &StartupInfo );
  112.  
  113.     mainret = WinMain( GetModuleHandle(NULL),
  114.                        NULL,
  115.                        lpszCommandLine,
  116.                        StartupInfo.dwFlags & STARTF_USESHOWWINDOW
  117.                             ? StartupInfo.wShowWindow : SW_SHOWDEFAULT );
  118.  
  119.     ExitProcess(mainret);
  120. }
  121.  
  122.  
  123. char GetAlphaNum(const int &num)
  124. {
  125.     if(num > 9)
  126.         return NULL;
  127.     return char(48+num);
  128. }
  129.  
  130. inline int GetStringLength(const char *str)
  131. {
  132.     for(unsigned int i = 0;str[i] != NULL;i++);
  133.     return i;
  134. }
  135.  
  136. int Atoi(const char *pString)
  137. {
  138.     int nNum = 0;
  139.     int nMagnitude = 1;
  140.     bool bNeg = false;
  141.     int i = 0;
  142.     int nStrLength = GetStringLength(pString)-1;
  143.     if(pString[0] == '-')
  144.     {
  145.         bNeg = true;
  146.         pString++;
  147.         nStrLength--;
  148.     }
  149.     while(i <= nStrLength)
  150.     {
  151.         nNum += nMagnitude*(pString[nStrLength-i++]-48);
  152.         //i++;
  153.         nMagnitude*=10;
  154.     }
  155.     if(bNeg)
  156.     {
  157.         nNum *= -1;
  158.         //return nNum|4294967296;
  159.     }
  160.     return nNum;
  161. }
  162.  
  163. inline int GetNumDigits(int n)
  164. {
  165.     for(int i = 0;n != 0;i++,n/=10);
  166.     return i;
  167. }
  168.  
  169.  
  170. const char *Itoa(int num, char *buffer)
  171. {
  172.     int nNumDigits = GetNumDigits(num);
  173.     bool bNeg = false;
  174.     if(num & 0x80000000)
  175.     {
  176.         bNeg = true;
  177.         num *= -1;
  178.         buffer[0] = '-';
  179.         buffer++;
  180.     }
  181.     for(int i = nNumDigits-1;i >= 0;i--,num/=10)
  182.     {
  183.         buffer[i] = char(48+(int(num%10)));
  184.     }
  185.     if(bNeg)
  186.         buffer--;
  187.     return buffer;
  188. }
  189.  
  190. char * GetFileDirectory(const char *pProgramPath,char *pCurrentDirectory)
  191. {// begin GetFileDirectory
  192.     for(int i = lstrlen(pProgramPath)-1;i >= 0;i--)
  193.         if(pProgramPath[i] == '\\')
  194.         {// begin copy directory name into buffer
  195.             lstrcpyn(pCurrentDirectory,pProgramPath,i+1);
  196.             break;
  197.         }// end copy directory name into buffer
  198.     return pCurrentDirectory;
  199. }// end GetFileDirectory
  200.  
  201. void SetAppDirectoryAsCurrent(void)
  202. {// begin SetAppDirectoryAsCurrent
  203.     // get the directory of the program
  204.     char pCurrentDirectory[MAX_PATH] = {NULL};
  205.     char pProgramPath[MAX_PATH] = {NULL};
  206.     GetModuleFileName(GetModuleHandle(NULL),pProgramPath,MAX_PATH-1);
  207.     // turn into a directory
  208.     GetFileDirectory(pProgramPath,pCurrentDirectory);
  209.     // set the current directory to the one the program is running from
  210.     SetCurrentDirectory(pCurrentDirectory);
  211. }// end SetAppDirectoryAsCurrent
  212.