/src/readstat/src/txt/readstat_copy.c
Line | Count | Source |
1 | | #include <stdlib.h> |
2 | | #include <string.h> |
3 | | #include <ctype.h> |
4 | | |
5 | 44.2k | void readstat_copy(char *buf, size_t buf_len, const char *str_start, size_t str_len) { |
6 | 44.2k | size_t this_len = str_len; |
7 | 44.2k | if (this_len >= buf_len) { |
8 | 5.09k | this_len = buf_len - 1; |
9 | 5.09k | } |
10 | 44.2k | memcpy(buf, str_start, this_len); |
11 | 44.2k | buf[this_len] = '\0'; |
12 | 44.2k | } |
13 | | |
14 | 3.37k | void readstat_copy_lower(char *buf, size_t buf_len, const char *str_start, size_t str_len) { |
15 | 3.37k | int i; |
16 | 3.37k | readstat_copy(buf, buf_len, str_start, str_len); |
17 | 17.0k | for (i=0; i<buf_len && buf[i]; i++) |
18 | 13.7k | buf[i] = tolower(buf[i]); |
19 | 3.37k | } |
20 | | |
21 | 6.32k | void readstat_copy_quoted(char *buf, size_t buf_len, const char *str_start, size_t str_len) { |
22 | 6.32k | size_t this_len = str_len; |
23 | 6.32k | if (this_len >= buf_len) { |
24 | 294 | this_len = buf_len - 1; |
25 | 294 | } |
26 | 6.32k | size_t i=0; |
27 | 6.32k | size_t j=0; |
28 | 6.32k | int slash = 0; |
29 | 51.6k | for (i=0; i<this_len; i++) { |
30 | 45.3k | if (slash) { |
31 | 1.74k | if (str_start[i] == 't') { |
32 | 466 | buf[j++] = '\t'; |
33 | 1.28k | } else { |
34 | 1.28k | buf[j++] = str_start[i]; |
35 | 1.28k | } |
36 | 1.74k | slash = 0; |
37 | 43.5k | } else { |
38 | 43.5k | if (str_start[i] == '\\') { |
39 | 1.78k | slash = 1; |
40 | 41.8k | } else { |
41 | 41.8k | buf[j++] = str_start[i]; |
42 | 41.8k | } |
43 | 43.5k | } |
44 | 45.3k | } |
45 | 6.32k | buf[j] = '\0'; |
46 | 6.32k | } |
47 | | |