blob: fe4f8887aa8eed2c9f5db9c8ed2da73dcc131f01 (
plain)
1 // Cini is a command line tool to parse ini files
2 // Copyright (C) 2018 Aaron Ball <nullspoon@oper.io>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 #include "str.h"
18 void trim(char* str, char* out) {
19 int start = 0;
20 int end = 0;
21 int i = 0;
22
23 // Read backwards
24 for(i = strlen(str); i >= 0; i--) {
25 if(str[i] != ' '
26 && str[i] != '\0'
27 && str[i] != '\r'
28 && str[i] != '\n'
29 && str[i] != '\t') {
30 // Once we find the first non-whitespace char, insert \0 after
31 end = i;
32 break;
33 }
34 }
35
36 // Read forwards to find first non-whitespace char
37 for(i = 0;i<strlen(str);i++) {
38 if(str[i] != ' '
39 && str[i] != '\0'
40 && str[i] != '\r'
41 && str[i] != '\n'
42 && str[i] != '\t') {
43 start = i;
44 break;
45 }
46 }
47
48 if(end == start) {
49 out[0] = '\0';
50 } else {
51 strncpy(out, &str[start], end - start + 2);
52 str[end + 1] = '\0';
53 }
54 }
55
56 void split(char* str, char delim, int reqcol, char* buf) {
57 int i = 0;
58 int col = 0;
59 int bufi = 0;
60
61 while(i < strlen(str)) {
62 if(str[i] == delim) {
63 col++;
64 i++;
65 continue;
66 } else {
67 if(col == reqcol) {
68 buf[bufi] = str[i];
69 bufi++;
70 }
71 }
72 i++;
73 }
74
75 buf[bufi] = '\0';
76 }
|