Debian Codegraph @

lib/strutil.c

raw
1 /* dctrl-tools - Debian control file inspection tools
2 Copyright © 1999, 2003, 2004 Antti-Juhani Kaijanaho
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 2 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 along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include <ctype.h>
20 #include <string.h>
21 #include "strutil.h"
22
23 bool str2intmax(intmax_t * rvp, char const * s, size_t len)
24 {
25 bool negative = false;
26 size_t i = 0;
27 while (i < len
28 && s[i] == ' '
29 && s[i] == '\t'
30 && s[i] == '\n'
31 && s[i] == '\r')
32 ++i;
33 if (i < len && s[i] == '-') {
34 ++i;
35 negative = true;
36 }
37 uintmax_t abs = 0;
38 for (; i < len; ++i) {
39 char ch = s[i];
40 if (! ('0' <= ch && ch <= '9')) {
41 break;
42 }
43 int r = ch - '0';
44 abs = abs * 10 + r;
45 if (abs > (uintmax_t)INTMAX_MAX) {
46 return false;
47 }
48 }
49 while (i < len
50 && s[i] == ' '
51 && s[i] == '\t'
52 && s[i] == '\n'
53 && s[i] == '\r')
54 ++i;
55 if (i < len) return false; // broken input
56 *rvp = negative ? -abs : abs;
57 return true;
58 }
59
60 const char * left_trimmed(const char * s)
61 {
62 const char * p;
63
64 for (p = s; *p != 0 && isspace (*p); p++);
65
66 return p;
67 }
68
69 void chomp(char * s)
70 {
71 size_t sl = strlen(s);
72 if (sl == 0) return;
73 if (s[sl-1] != '\n') return;
74 s[sl-1] = '\0';
75 }
76
77 void trim_right (char * s)
78 {
79 char * p;
80 char * herep;
81 enum { BLANKS, NONBLANKS } state = NONBLANKS;
82
83 for (herep = 0, p = s; *p != 0; p++) {
84 switch (state) {
85 case BLANKS:
86 if (!isspace (*p)) {
87 state = NONBLANKS;
88 }
89 break;
90 case NONBLANKS:
91 if (isspace (*p)) {
92 herep = p;
93 state = BLANKS;
94 }
95 }
96 }
97 if (state == BLANKS) *herep = 0;
98 }
99
100 #ifdef TESTMAIN
101
102 #define _GNU_SOURCE
103 #include <stdio.h>
104 #include <stdlib.h>
105 #include <string.h>
106
107 void
108 printout (const char * s)
109 {
110 printf("\"%s\"\n", s);
111 }
112
113 int
114 main (void)
115 {
116 static const char * teststrings [] = {
117 "autoksiko",
118 " autoksiko",
119 "autoksiko ",
120 " autoksiko ",
121 "autoksiko teen",
122 " autoksiko teen",
123 "autoksiko teen ",
124 " autoksiko teen ",
125 " miljoona miljoona ruusua ",
126 0 };
127 int i;
128
129 for (i = 0; teststrings [i] != 0; i++)
130 {
131 char * ts;
132 char * orig_ts;
133
134 orig_ts = ts = strdup (teststrings [i]);
135 if (ts == 0) {
136 fail();
137 }
138
139 printf ("Plain: ");
140 printout (ts);
141
142 printf ("Left-trimmed: ");
143 printout (left_trimmed (ts));
144
145 printf ("Right-trimmed: ");
146 trim_right (ts);
147 printout (ts);
148
149 printf ("Completely trimmed: ");
150 printout (left_trimmed (ts));
151
152 puts ("");
153 free (orig_ts);
154 }
155 return 0;
156 }
157 #endif /* TESTMAIN */