home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vos2-121.zip / v / srcos2 / vutil.cpp < prev    next >
C/C++ Source or Header  |  1999-02-22  |  5KB  |  177 lines

  1. //===============================================================
  2. // General Purpose V Utilities
  3. //
  4. // Copyright (C) 1995,1996,1997,1998  Bruce E. Wampler
  5. //
  6. // This file is part of the V C++ GUI Framework, and is covered
  7. // under the terms of the GNU Library General Public License,
  8. // Version 2. This library has NO WARRANTY. See the source file
  9. // vapp.cxx for more complete information about license terms.
  10. //===============================================================
  11. #include <v/vos2.h>           // for OS/2 stuff
  12. #include <v/v_defs.h>
  13. #include <v/vutil.h>
  14. #define __USELOCALES__
  15. #include <time.h>
  16. //#include <sys/time.h>
  17.  
  18. //=============================>>> vTextLen  <<<============================
  19.   int vTextLen(const char *str, int& numLines)
  20.   {
  21.     // calculate length of multiline text string
  22.     int l, maxl;
  23.     maxl = 0;           // no length so far
  24.     numLines = 1;
  25.     for (l = 0 ; *str; str++)
  26.       {
  27.         if (*str != '\n')
  28.           {
  29.             ++l;
  30.           }
  31.         else
  32.           {
  33.             ++numLines;
  34.             if (l > maxl)
  35.                 maxl = l;
  36.             l = 0;
  37.           }
  38.       }
  39.     if (l > maxl)
  40.         maxl = l;
  41.     return maxl;
  42.   }
  43. //=============================>>> ByteToStr   <<<============================
  44.   void ByteToStr(unsigned char intg, char* str)
  45.   {  // convert byte intg to char string in str
  46.  
  47.     int i;
  48.     int d, intval, j;
  49.     char k;
  50.     static char digits[] = "0123456789ABCDEF";
  51.  
  52.     intval = intg;
  53.     str[0] = '\0';
  54.     i = 0;
  55.     do
  56.       {                         // generate digits
  57.         i++;
  58.         d = intval % 16;        // mod 10
  59.         str[i] = digits[d];
  60.         intval = intval / 16;
  61.       }
  62.     while (intval != 0);
  63.     for (j = 0 ; j < i ; j++ )
  64.       {                         // then reverse
  65.         k = str[i];
  66.         str[i--] = str[j];
  67.         str[j] = k;
  68.       }
  69.     if (str[1] == 0)            // one char only
  70.       {
  71.         str[1] = str[0]; str[2] = 0; str[0] = '0';
  72.       }
  73.   }
  74. //============================>>> vLblLen  <<<============================
  75.   int vLblLen(const char *str)
  76.   {
  77.     int l;
  78.     for (l = 0 ; *str; str++)
  79.       if (*str != '&')
  80.           ++l;
  81.     return l;
  82.   }
  83. //===========================>>> StrToLong <<<============================
  84.   long StrToLong(VCONST char* str)
  85.   {  // convert int intg to char string in str
  86.     long val = 0;
  87.     while (*str)
  88.     if (*str >= '0' && *str <= '9')
  89.         val = (val*10) + (*str++ - '0');
  90.     else
  91.         break;
  92.     return val;
  93.   }
  94. //=============================>>> l_to_a   <<<============================
  95.   void IntToStr(int intg, char* str)
  96.   {  // convert int intg to char string in str
  97.     LongToStr((long)intg, str);
  98.   }
  99. //=============================>>> l_to_a   <<<============================
  100.   void LongToStr(long intg, char* str)
  101.   {  // convert long intg to char string in str
  102.  
  103.     long i;
  104.     long d, intval, j;
  105.     char k;
  106.     static char digits[] = "0123456789";
  107.  
  108.     intval = intg >= 0L ? intg : (-intg);
  109.     str[0] = '\0';
  110.     i = 0;
  111.     do
  112.     {                         // generate digits
  113.       i++;
  114.       d = intval % 10L;       // mod 10
  115.       str[i] = digits[d];
  116.       intval = intval / 10L;
  117.     }
  118.     while (intval != 0L);
  119.  
  120.     if (intg < 0L)
  121.     {                         // then sign
  122.       str[++i] = '-';
  123.     }
  124.  
  125.     for (j = 0 ; j < i ; j++ )
  126.     {                         // then reverse
  127.       k = str[i];
  128.       str[i--] = str[j];
  129.       str[j] = k;
  130.     }
  131.   }
  132.  
  133. //=============================>>> vGetLocalTime <<<==========================
  134.   void vGetLocalTime(char* tm)
  135.   {
  136.     char buff[20];
  137.     time_t t;
  138.     t = time(0);
  139.     strftime(buff, 20, "%X", localtime(&t));
  140.     strcpy(tm,buff);
  141.   }
  142. //=============================>>> vGetLocalDate <<<==========================
  143.   void vGetLocalDate(char* tm)
  144.   {
  145.     char buff[20];
  146.     time_t t;
  147.     t = time(0);
  148.     strftime(buff, 20, "%d %b %Y", localtime(&t));
  149.     strcpy(tm,buff);
  150.   }
  151.  
  152. //=============================>>> vBeep <<<==========================
  153.   void vBeep()
  154.   {
  155.     WinAlarm (HWND_DESKTOP, WA_WARNING);
  156.   }
  157.  
  158. //==================================================================
  159. //
  160. //    Some special case utils for working with CommandObjects
  161. //
  162.  
  163. //=============================>>> vGetcmdIdIndex <<<==========================
  164.   int vGetcmdIdIndex(ItemVal cmdId, CommandObject *cmdObj)
  165.   {
  166.     //    Return the index to the CommandObject array for the
  167.     //    index which has the matching cmdId.
  168.     for (int i = 0 ; cmdObj[i].cmdType != C_EndOfList ; ++i)
  169.     {
  170.       if (cmdObj[i].cmdId == cmdId)
  171.     return i;
  172.     }
  173.     return -1;
  174.   }
  175.  
  176.  
  177.