You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
5.4KB

  1. #include <iostream>
  2. #include <string.h>
  3. #include <stdarg.h>
  4. #include "String.h"
  5. //#include "Exceptions.h"
  6. #ifdef HAVE_CONFIG_H
  7. #include "config.h"
  8. #endif
  9. #if 0
  10. char String::t[BUFSIZ];
  11. String::String(const String & s) : str(strdup(s.str)) { }
  12. String::String(char c) {
  13. char t[2];
  14. sprintf(t, "%c", c);
  15. str = strdup(t);
  16. }
  17. String::String(const char * s) : str(s ? strdup(s) : NULL) { }
  18. String::String(int i) {
  19. char t[20];
  20. sprintf(t, "%i", i);
  21. str = strdup(t);
  22. }
  23. String::String(double d) {
  24. char t[30];
  25. sprintf(t, "%g", d);
  26. str = strdup(t);
  27. }
  28. String::~String() {
  29. free(str);
  30. }
  31. char * String::set(char * s, ...) {
  32. va_list ap;
  33. va_start(ap, s);
  34. vsprintf(t, s, ap);
  35. free(str);
  36. str = strdup(t);
  37. va_end(ap);
  38. return t;
  39. }
  40. char * String::to_charp(size_t from, ssize_t to) const {
  41. if (to < 0) {
  42. strcpy(t, &(str[from]));
  43. } else {
  44. if (to >= strlen()) {
  45. to = strlen() - 1;
  46. }
  47. if (to >= from) {
  48. int i;
  49. for (i = 0; i <= to - from; i++) {
  50. t[i] = str[i + from];
  51. }
  52. t[i] = '\0';
  53. } else {
  54. t[0] = '\0';
  55. }
  56. }
  57. return t;
  58. }
  59. int String::to_int(void) const {
  60. int r;
  61. sscanf(str, "%i", &r);
  62. return r;
  63. }
  64. double String::to_double(void) const {
  65. double r;
  66. sscanf(str, "%lf", &r);
  67. return r;
  68. }
  69. string String::to_string(void) const {
  70. return string(str);
  71. }
  72. String & String::operator=(const String & s) {
  73. if (str != s.str) {
  74. // On évite l'autodestruction...
  75. free(str);
  76. str = strdup(s.str);
  77. }
  78. return *this;
  79. }
  80. String String::operator+(const String & s) const {
  81. strcpy(t, str);
  82. strcat(t, s.str);
  83. return String(t);
  84. }
  85. String & String::operator+=(const String & s) {
  86. strcpy(t, str);
  87. strcat(t, s.str);
  88. free(str);
  89. str = strdup(t);
  90. return (*this);
  91. }
  92. ostream & operator<<(ostream & os, const String & s) {
  93. return (os << s.to_charp());
  94. }
  95. istream & operator>>(istream & is, String & s) {
  96. char c = 0;
  97. s.set("");
  98. while (!is.eof() && (c != '\n') && (c != '\r')) {
  99. is >> c;
  100. s += c;
  101. }
  102. return is;
  103. }
  104. bool String::operator!=(const String & s) const {
  105. return (strcmp(str, s.str) != 0);
  106. }
  107. bool String::operator==(const String & s) const {
  108. return (strcmp(str, s.str) == 0);
  109. }
  110. bool String::operator<=(const String & s) const {
  111. return (strcmp(str, s.str) <= 0);
  112. }
  113. bool String::operator>=(const String & s) const {
  114. return (strcmp(str, s.str) >= 0);
  115. }
  116. bool String::operator<(const String & s) const {
  117. return (strcmp(str, s.str) < 0);
  118. }
  119. bool String::operator>(const String & s) const {
  120. return (strcmp(str, s.str) > 0);
  121. }
  122. size_t String::strlen() const {
  123. return (str ? ::strlen(str) : 0);
  124. }
  125. char String::operator[](size_t i) const {
  126. if (i >= strlen()) {
  127. return 0;
  128. } else {
  129. return str[i];
  130. }
  131. }
  132. ssize_t String::strchr(char c, size_t from) const {
  133. size_t s = strlen();
  134. for (size_t i = from; i < s; i++) {
  135. if (str[i] == c) return i;
  136. }
  137. return -1;
  138. }
  139. ssize_t String::strrchr(char c) const {
  140. size_t s = strlen();
  141. for (size_t i = s - 1; i >= 0; i--) {
  142. if (str[i] == c) return i;
  143. }
  144. return -1;
  145. }
  146. ssize_t String::strstr(const String & s) const {
  147. char * p = ::strstr(str, s.str);
  148. if (p) {
  149. return p - str;
  150. } else {
  151. return -1;
  152. }
  153. }
  154. int String::strchrcnt(char c) const {
  155. size_t i, cnt = 0;
  156. size_t s = strlen();
  157. for (i = 0; i < s; i++) {
  158. if (str[i] == c) cnt++;
  159. }
  160. return cnt;
  161. }
  162. String String::to_sqldate(void) const {
  163. /* DD/MM/YYYY ==> YYYYMMMDD */
  164. return (is_date() ? String(to_charp(6, 9)) + to_charp(3, 4) + to_charp(0, 1) : "");
  165. }
  166. String String::to_sqltime(void) const {
  167. /* h:m ==> h * 60 + m */
  168. int p = strchr(':');
  169. return (is_time() ? String(String(to_charp(0, p - 1)).to_int() * 60 + String(to_charp(p + 1)).to_int()) : "");
  170. }
  171. String String::from_sqldate(void) const {
  172. /* YYYYMMDD ==> DD/MM/YYYY */
  173. return ((strlen() == 8) && is_number() ? String(to_charp(6, 7)) + '/' + to_charp(4, 5) + '/' + to_charp(0, 3) : "");
  174. }
  175. String String::from_sqltime(void) const {
  176. /* t ==> (t / 60):(t % 60) */
  177. int t = to_int();
  178. return (is_number() ? String((int) (t / 60)) + ':' + (t % 60) : "");
  179. }
  180. bool String::is_date(void) const {
  181. /* 'DD/MM/YYYY'
  182. 0123456789 */
  183. if (strlen() != 10) return false;
  184. if ((str[2] != '/') || (str[5] != '/') ||
  185. (!String(to_charp(0, 1)).is_number()) ||
  186. (!String(to_charp(3, 4)).is_number()) ||
  187. (!String(to_charp(6, 9)).is_number())) {
  188. return false;
  189. }
  190. return true;
  191. }
  192. bool String::is_number(void) const {
  193. size_t s = strlen();
  194. for (size_t i = ((str[i] == '-') ? 1 : 0); i < s; i++) {
  195. if ((str[i] > '9') || (str[i] < '0')) return false;
  196. }
  197. return true;
  198. }
  199. bool String::is_float(void) const {
  200. size_t s = strlen();
  201. bool seendot = false;
  202. for (size_t i = ((str[i] == '-') ? 1 : 0); i < s; i++) {
  203. if ((str[i] > '9') || (str[i] < '0')) {
  204. if ((str[i] == '.') && !seendot) {
  205. seendot = true;
  206. } else {
  207. return false;
  208. }
  209. }
  210. }
  211. return true;
  212. }
  213. bool String::is_time(void) const {
  214. int p = strchr(':');
  215. if (p == -1) return false;
  216. // On accepte les heures sous le format xxxxxx:yy pour pouvoir indiquer des durées.
  217. if ((!String(to_charp(0, p - 1)).is_number()) || (!String(to_charp(p + 1)).is_number()))
  218. return false;
  219. return (String(to_charp(p + 1)).to_int() < 60) ? true : false;
  220. }
  221. #endif