/cdex1/branches/cdex_130/cdexos/MP3Dec/str_lib.cpp

# · C++ · 255 lines · 182 code · 61 blank · 12 comment · 45 complexity · 22a868d399611a2f5555e92c4ac1c24f MD5 · raw file

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