home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lxapi32.zip / Lib32 / lxsprintf.c < prev    next >
C/C++ Source or Header  |  2002-04-26  |  3KB  |  146 lines

  1. /* $Id: lxsprintf.c,v 1.2 2002/04/26 23:09:25 smilcke Exp $ */
  2.  
  3. /*
  4.  * sprintf.c
  5.  * Autor:               Stefan Milcke
  6.  * Erstellt am:         05.11.2001
  7.  * Letzte Aenderung am: 01.01.2002
  8.  *
  9. */
  10. #pragma code_seg ("_inittext");
  11. #pragma data_seg ("_initdata","endds");
  12.  
  13. #define INCL_NOPMAPI
  14. #define INCL_DOSMISC
  15.  
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #include <os2.h>
  20.  
  21. #include <ldefos2.h>
  22.  
  23. #define CR 0x0d
  24. #define LF 0x0a
  25.  
  26.  
  27. #define LEADING_ZEROES          0x8000
  28. #define SIGNIFICANT_FIELD       0x0007
  29.  
  30. char *HexLongToASCII(char *StrPtr, unsigned long wHexVal, unsigned short Option,unsigned short nlz);
  31. char *DecLongToASCII(char *StrPtr, unsigned long lDecVal, unsigned short Option,unsigned short nlz);
  32.  
  33. //---------------------------------- sprintf -----------------------------------
  34. int sprintf (char *buffer, const char *format, ...)
  35. {
  36.  char *BuildPtr=buffer;
  37.  char *pStr = (char *) format;
  38.  char *SubStr;
  39.  int numLeadingZeroes;
  40.  union
  41.  {
  42.   void   *VoidPtr;
  43.   unsigned short *WordPtr;
  44.   unsigned long  *LongPtr;
  45. //#ifdef KEE
  46.   unsigned long  *StringPtr;
  47. //#else
  48. //  double *StringPtr;
  49. //#endif
  50.  } Parm;
  51.  int wBuildOption;
  52.  Parm.VoidPtr=(void *) &format;
  53.  Parm.StringPtr++;                         // skip size of string pointer
  54.  while (*pStr)
  55.  {
  56.   switch (*pStr)
  57.   {
  58.    case '%':
  59.     wBuildOption=0;
  60.     pStr++;
  61.     if(*pStr=='0')
  62.     {
  63.      wBuildOption|=LEADING_ZEROES;
  64.      pStr++;
  65.      if(*pStr>='0' && *pStr<='9')
  66.      {
  67.       numLeadingZeroes=((int)(*pStr))-((int)'0');
  68.       pStr++;
  69.      }
  70.      else
  71.       numLeadingZeroes=0;
  72.     }
  73.     if(*pStr=='u')                      // always unsigned
  74.      pStr++;
  75.     switch(*pStr)
  76.     {
  77.      case 'x':
  78.      case 'X':
  79.       BuildPtr=HexLongToASCII(BuildPtr,*Parm.LongPtr++,wBuildOption,numLeadingZeroes);
  80.       pStr++;
  81.       continue;
  82.      case 'd':
  83.       BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption,numLeadingZeroes);
  84.       pStr++;
  85.       continue;
  86. //#ifdef KEE
  87.      case 's':
  88. #ifndef KEE
  89.       DebugInt3();
  90. #endif
  91.       SubStr=(char *)(*Parm.StringPtr);
  92.       if(SubStr && (*SubStr))
  93.       {
  94.        while((*BuildPtr++ = *SubStr++));
  95.        BuildPtr--;                      // remove the \0
  96.       }
  97.       Parm.StringPtr++;
  98.       pStr++;
  99.       continue;
  100. //#endif
  101.      case 'l':
  102.       pStr++;
  103.       switch (*pStr)
  104.       {
  105.        case 'x':
  106.        case 'X':
  107.         BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption,numLeadingZeroes);
  108.         pStr++;
  109.         continue;
  110.        case 'd':
  111.         BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption,numLeadingZeroes);
  112.         pStr++;
  113.         continue;
  114.       } // end switch
  115.       continue;                        // dunno what he wants
  116.      case 0:
  117.       continue;
  118.     } // end switch
  119.     break;
  120.    case '\\':
  121.     pStr++;
  122.     switch(*pStr)
  123.     {
  124.      case 'n':
  125.       *BuildPtr++=LF;
  126.       pStr++;
  127.       continue;
  128.      case 'r':
  129.       *BuildPtr++=CR;
  130.       pStr++;
  131.       continue;
  132.      case 0:
  133.       continue;
  134.     } // end switch
  135.    break;
  136.   } // end switch
  137.   *BuildPtr++=*pStr++;
  138.  } // end while
  139.  *BuildPtr=0;  // cauterize the string
  140.  return 1; //not correct
  141. }
  142.  
  143. #ifdef __cplusplus
  144. }
  145. #endif
  146.