File: UgString.c

package info (click to toggle)
uget 2.2.3-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,348 kB
  • sloc: ansic: 41,325; sh: 1,444; makefile: 325
file content (401 lines) | stat: -rw-r--r-- 8,496 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 *
 *   Copyright (C) 2012-2020 by C.H. Huang
 *   plushuang.tw@gmail.com
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  ---
 *
 *  In addition, as a special exception, the copyright holders give
 *  permission to link the code of portions of this program with the
 *  OpenSSL library under certain conditions as described in each
 *  individual source file, and distribute linked combinations
 *  including the two.
 *  You must obey the GNU Lesser General Public License in all respects
 *  for all of the code used other than OpenSSL.  If you modify
 *  file(s) with this exception, you may extend this exception to your
 *  version of the file(s), but you are not obligated to do so.  If you
 *  do not wish to do so, delete this exception statement from your
 *  version.  If you delete this exception statement from all source
 *  files in the program, then also delete it here.
 *
 */

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <stdio.h>  // vsnprintf
#include <stdlib.h>
#include <stdarg.h>
#include <UgString.h>

// ----------------------------------------------------------------------------
// String

#if !(defined HAVE_GLIB)
char* ug_strdup_printf (const char* format, ...)
{
	va_list arg_list;
	char*   string;
	int     string_len;

	va_start (arg_list, format);
#ifdef _MSC_VER
	/* for MS C only */
	string_len = _vscprintf (format, arg_list);
#else
	/* for C99 standard */
	string_len = vsnprintf (NULL, 0, format, arg_list);
#endif
	va_end (arg_list);

	string = ug_malloc (string_len + 1);

	va_start (arg_list, format);
	vsprintf (string, format, arg_list);
	va_end (arg_list);

	return string;
}

char*  ug_strndup (const char* string, size_t length)
{
	char*  result;

	result = ug_malloc (length + 1);
	result[length] = 0;
	strncpy (result, string, length);
	return result;
}
#endif // ! (HAVE_GLIB)

// return length of new string
int    ug_str_remove_crlf (const char* src, char* dest)
{
	return  ug_str_remove_chars (src, dest, "\r\n");
}

// return length of new string
int    ug_str_remove_chars (const char* src, char* dest, const char* chars)
{
	const char* cur;
	int         length = 0;

	if (src) {
		for (;  ;  src++) {
			for (cur = chars;  cur[0];  cur++) {
				if (src[0] == cur[0])
					break;
			}
			if (cur[0] != 0)
				continue;

			if (dest)
				*dest++ = *src;
			if (src[0] == 0)
				break;
			length++;
		}
	}

	return length;
}

// return number of characters was replaced by to_char
int    ug_str_replace_chars (char* str, const char* from_chars, int to_char)
{
	int   counts;

	for (counts = 0;  ; counts++) {
		str = strpbrk (str, from_chars);
		if (str == NULL)
			break;
		str[0] = to_char;
	}
	return counts;
}

/*
 * convert double to string
 * If value large than 1024, it will append unit string like "KiB",
 * "MiB", "GiB", "TiB", or "PiB"  to string.
 */
char*  ug_str_from_int_unit (int64_t value, const char* tail)
{
	static const char* unit_array[] = {"", " KiB", " MiB", " GiB", " TiB", " PiB"};
	static const int   unit_array_len = sizeof (unit_array) / sizeof (char*);
	int  index;

	for (index=0;  index < unit_array_len -1;  index++) {
//		if (value < 1024)
		if (value < 10000)
			break;
//		value /= 1024;
		value >>= 10;
	}

	return ug_strdup_printf ("%d%s%s",
			(int)value, unit_array[index], (tail) ? tail : "");
}

/*
 * convert seconds to string (hh:mm:ss)
 */
char*  ug_str_from_seconds (int seconds, int limit_99_99_99)
{
	int  hour, minute;
	int  day, year;

	minute  = seconds / 60;
	hour    = minute / 60;
	minute  = minute % 60;
	seconds = seconds % 60;

	if (hour < 100)
		return ug_strdup_printf ("%.2d:%.2d:%.2d", hour, minute, seconds);
	else if (limit_99_99_99)
		return ug_strdup ("99:99:99");
	else {
		day    = hour / 24;
		year   = day / 365;
		if (year)
			return ug_strdup ("> 1 year");
		else
			return ug_strdup_printf ("%u days", day);
	}
}

/*
 * convert time_t to string
 */
char*  ug_str_from_time (time_t ptt, int date_only)
{
	struct tm*  timem;

	timem = localtime (&ptt);
	if (timem == NULL)
		return NULL;
	if (date_only) {
		return ug_strdup_printf ("%.4d-%.2d-%.2d",
				timem->tm_year + 1900,
				timem->tm_mon  + 1,
				timem->tm_mday);
	}
	else {
		return ug_strdup_printf ("%.4d-%.2d-%.2d" " %.2d:%.2d:%.2d",
				timem->tm_year + 1900,
				timem->tm_mon  + 1,
				timem->tm_mday,
				timem->tm_hour,
				timem->tm_min,
				timem->tm_sec);
	}
}

static const char* month_string[] =
{
	"Jan", "Feb", "Mar", "Apr",
	"May", "Jun", "Jul", "Aug",
	"Sep", "Oct", "Nov", "Dec"
};

time_t  ug_str_rfc822_to_time (const char* string)
{
	struct tm   timem;
	const char* cur;
	const char* end;
	int         idx;

	end = string + strlen (string);

	// day
	if ((cur = string + 5) >= end)
		return -1;
	timem.tm_mday = strtoul (cur, NULL, 10);

	// month
	if ((cur += 3) >= end)
		return -1;
	for (idx = 0;  idx < 12;  idx++) {
		if (strncmp (cur, month_string[idx], 3) == 0)
			break;
	}
	if (idx == 12)
		return -1;
	timem.tm_mon = idx;

	// year
	if ((cur += 4) >= end)
		return -1;
	timem.tm_year = strtoul (cur, NULL, 10);
//	if (timem.tm_year < 50)
//		timem.tm_year += 100;
//	else if (timem.tm_year > 1000)
		timem.tm_year -= 1900;

	if ((cur = strchr (cur, ' ')) == NULL)
		return -1;
	cur++;

	// hour
	timem.tm_hour = strtoul (cur, NULL, 10);

	// minute
	if ((cur += 3) >= end)
		return -1;
	timem.tm_min = strtoul (cur, NULL, 10);

	// second
	if ((cur += 3) >= end)
		return -1;
	timem.tm_sec = strtoul (cur, NULL, 10);

	// zone
//	if ((cur += 3) >= end)
//		return -1;

	// other
	timem.tm_wday = 0;
	timem.tm_yday = 0;
	timem.tm_isdst = -1;

	return mktime (&timem);
}

time_t  ug_str_rfc3339_to_time (const char* string)
{
	struct tm   timem;
	const char* cur;
	const char* end;

	end = string + strlen (string);

	// year
	timem.tm_year = strtoul (string, NULL, 10);
	timem.tm_year -= 1900;

	if ((cur = strchr (string, '-')) == NULL)
		return -1;
	cur++;

	// month
	timem.tm_mon = strtoul (cur, NULL, 10);
	if (timem.tm_mon > 0)
		timem.tm_mon -= 1;

	// day
	if ((cur += 3) >= end)
		return -1;
	timem.tm_mday = strtoul (cur, NULL, 10);

	// hour
	if ((cur += 3) >= end)
		return -1;
	timem.tm_hour = strtoul (cur, NULL, 10);

	// minute
	if ((cur += 3) >= end)
		return -1;
	timem.tm_min = strtoul (cur, NULL, 10);

	// second
	if ((cur += 3) >= end)
		return -1;
	timem.tm_sec = strtoul (cur, NULL, 10);

	// zone
	if ((cur = strpbrk (cur, "+-")) == NULL)
		return -1;

	// other
	timem.tm_wday = 0;
	timem.tm_yday = 0;
	timem.tm_isdst = -1;

	return mktime (&timem);
}

// ------------------------------------
// command-line

char** ug_argv_from_cmd (const char* cmd, int* argc, int reserve_len)
{
	int    argn;
	char** argv;
	char*  buf;
	char*  beg;
	char*  cur;
	char*  end;
	char*  quote = NULL;   // pointer to '\"'

	for (argn = 0, cur = (char*)cmd;  ;  argn++) {
		if (cur[0] == 0) {
			end = cur;
			break;
		}
		while (cur[0] != ' ' && cur[0])
			cur++;
		while (cur[0] == ' ')
			cur++;
	}

	// malloc size include buffer and null-terminated
	argv = ug_malloc (sizeof (char*) * (argn+2+reserve_len));
	buf  = ug_strdup (cmd);
	end  = buf + (end - cmd);
	// argv[-1] == buf
	*argv++ = buf;

	for (argn = reserve_len, beg = buf, cur = buf;  cur[0];  ) {
		switch (cur[0]) {
		default:
			cur++;
			break;

		case '\"':
			// overwrite '\"', must include null-terminated
			memmove (cur, cur+1, end - cur);
			if (quote == NULL)
				quote = cur;
			else
				quote = NULL;
			break;

		case ' ':
			if (quote == NULL) {
				argv[argn++] = beg;
				while (cur[0] == ' ')
					*cur++ = 0;
				beg = cur;
			}
			break;
		}
	}

	if (beg[0])
		argv[argn++] = beg;
	if (argc)
		argc[0] = argn;
	argv[argn++] = NULL;  // null-termainate
	return argv;
}

void   ug_argv_free (char** ug_argv)
{
	ug_free (ug_argv[-1]);
	ug_free (ug_argv-1);
}