home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 Secrets / Secrets2.iso / Audio / WAV / MaplayP / _SETUP.1 / str_lib.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-21  |  3.3 KB  |  183 lines

  1. /* str_lib.cpp
  2.  
  3.    String manipulation routines for Win32 interface. Not needed
  4.    for other operating systems. Bad programming by Jeff Tsay.
  5.  
  6.    Last modified : 06/21/97 */
  7.  
  8. #include <windows.h>
  9. #include "str_lib.h"
  10.  
  11. str_type my_itoa(int value, str_type str, int radix)
  12. // Negative numbers not supported, or radices bigger than 10
  13. {
  14.    char temp[256];
  15.  
  16.    if (value == 0) {
  17.        str[1] = '\0';
  18.       str[0] = '0';
  19.  
  20.     } else {
  21.  
  22.        temp[255]='\0';
  23.       int last_index;
  24.  
  25.        for(int i=254; i>=0, value>0; i--) {
  26.           int remainder = value % radix;
  27.           value /= radix;
  28.           temp[i] = (char) (remainder + '0');
  29.           last_index = i;
  30.        }
  31.  
  32.        lstrcpy(str, temp + last_index);
  33.    }
  34.  
  35.    return(str);
  36. }
  37.  
  38. int my_atoi(str_type str)
  39. // Negative numbers not supported
  40. {
  41.     int length = lstrlen(str);
  42.    int base = 1;
  43.    int result = 0;
  44.  
  45.    for(int i=length-1; i>=0; i--)
  46.    {
  47.         result += (str[i] - '0') * base;
  48.       base = (base << 3) + (base << 1);  // base*=10;
  49.    }
  50.  
  51.    return(result);
  52. }
  53.  
  54.  
  55. str_type strcpy_2n(str_type dest, str_type src)
  56. // Copies until 2 '\0' characters are reached
  57. {
  58.     int last_zero = 0;
  59.    str_type temp_src = src;
  60.  
  61.    while (1) {
  62.        if ((*dest++ = *temp_src++) == '\0') {
  63.           if (last_zero) {
  64.              return(dest);
  65.          } else {
  66.              last_zero = 1;
  67.          }
  68.       } else {
  69.           last_zero = 0;
  70.       }
  71.    }
  72. }
  73.  
  74. str_type strcat_2n(str_type dest, str_type src)
  75. // Appends at the 2nd '\0' character
  76. {
  77.     int last_zero = 0;
  78.    int found = 0;
  79.    str_type temp_dest = dest;
  80.  
  81.    while (!found) {
  82.       if (*temp_dest == '\0') {
  83.           if (last_zero) {
  84.              found = 1;
  85.          } else {
  86.              last_zero = 1;
  87.             temp_dest++;
  88.          }
  89.        } else {
  90.           last_zero = 0;
  91.          temp_dest++;
  92.       }
  93.    }
  94.  
  95.    strcpy_2n(temp_dest, src);
  96.  
  97.    return(dest);
  98. }
  99.  
  100. bool b_strcmpi(str_type s1, str_type s2)
  101. // Case insensitive string compare that returns true if
  102. // the strings match.
  103. {
  104.     return ((bool) (lstrcmpi(s1, s2) == 0));
  105. }
  106.  
  107. str_type time_string(int ms, str_type dest)
  108. {
  109.     int i, j, nmlength;
  110.     int seconds, minutes;
  111.     char second_str[4];
  112.     char minute_str[4];
  113.  
  114.     minutes = ms / 60000;
  115.     seconds = ms / 1000 - minutes * 60;
  116.  
  117.     my_itoa(minutes, minute_str, 10);
  118.     my_itoa(seconds, second_str, 10);
  119.  
  120.     nmlength = 3 - lstrlen(minute_str);
  121.  
  122.     for (i=0; i < nmlength ; i++)
  123.         dest[i] = '0';
  124.  
  125.     for (i = nmlength, j=0; i<3; i++, j++)
  126.         dest[i] = minute_str[j];
  127.  
  128.     dest[3] = ':';
  129.  
  130.     nmlength = 6 - lstrlen(second_str);
  131.  
  132.     for (i=4; i < nmlength ; i++)
  133.         dest[i] = '0';
  134.  
  135.     for (i=nmlength, j=0; i<6; i++, j++)
  136.         dest[i] = second_str[j];
  137.  
  138.     dest[6]='\0';
  139.  
  140.     return (dest);
  141. }
  142.  
  143. str_type Proper_Filename(str_type s)
  144. {
  145.     int length = lstrlen(s);
  146.  
  147.     for(int i=length; i>=0; --i)
  148.         if (s[i]=='\\')
  149.             return (s+i+1);
  150.     return(s);
  151. }
  152.  
  153. int CDTrack_number(str_type current, str_type next, str_type src)
  154. {
  155.     int ret_val;
  156.     int length = lstrlen(src);
  157.    int chars_to_copy = 2;
  158.    int i = length - 5;
  159.    int next_track;
  160.  
  161.    while ((src[i]!='k') && (src[i]!='K') && (i>0)) {
  162.        chars_to_copy++;
  163.       i--;
  164.    };
  165.  
  166.    do {
  167.        i++;
  168.       chars_to_copy--;
  169.    } while (src[i]=='0');
  170.  
  171.    lstrcpyn(current, src + i, chars_to_copy);
  172.  
  173.    ret_val = my_atoi(current);
  174.    next_track = ret_val + 1;
  175.  
  176.    my_itoa(next_track, next, 10);
  177.  
  178.    return(ret_val);
  179. }
  180.  
  181.  
  182.  
  183.