fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
1/*****************************************************************************
2 * libini *
3 *****************************************************************************/
4
5/*****************************************************************************
6 * File name: src/libini/strings.c *
7 * Created: 2010-09-13 by Hampa Hug <hampa@hampa.ch> *
8 * Copyright: (C) 2010 Hampa Hug <hampa@hampa.ch> *
9 *****************************************************************************/
10
11/*****************************************************************************
12 * This program is free software. You can redistribute it and / or modify it *
13 * under the terms of the GNU General Public License version 2 as published *
14 * by the Free Software Foundation. *
15 * *
16 * This program is distributed in the hope that it will be useful, but *
17 * WITHOUT ANY WARRANTY, without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
19 * Public License for more details. *
20 *****************************************************************************/
21
22
23#include <config.h>
24
25#include <stdlib.h>
26#include <string.h>
27
28#include <libini/libini.h>
29
30
31void ini_str_init (ini_strings_t *is)
32{
33 is->cnt = 0;
34 is->max = 0;
35 is->str = NULL;
36}
37
38void ini_str_free (ini_strings_t *is)
39{
40 free (is->str);
41}
42
43int ini_str_add (ini_strings_t *is, const char *s1, const char *s2, const char *s3)
44{
45 unsigned long n1, n2, n3;
46 unsigned long cnt, max;
47 char *tmp;
48
49 n1 = (s1 != NULL) ? strlen (s1) : 0;
50 n2 = (s2 != NULL) ? strlen (s2) : 0;
51 n3 = (s3 != NULL) ? strlen (s3) : 0;
52
53 cnt = is->cnt + n1 + n2 + n3 + 1;
54
55 if (cnt >= is->max) {
56 max = (is->max == 0) ? 256 : is->max;
57
58 while (cnt >= max) {
59 max *= 2;
60 }
61
62 tmp = realloc (is->str, max);
63
64 if (tmp == NULL) {
65 return (1);
66 }
67
68 is->max = max;
69 is->str = tmp;
70 }
71
72 if (s1 != NULL) {
73 memcpy (is->str + is->cnt, s1, n1);
74 }
75
76 if (s2 != NULL) {
77 memcpy (is->str + is->cnt + n1, s2, n2);
78 }
79
80 if (s3 != NULL) {
81 memcpy (is->str + is->cnt + n1 + n2, s3, n3);
82 }
83
84 is->cnt += n1 + n2 + n3;
85
86 is->str[is->cnt] = 0;
87
88 return (0);
89}
90
91int ini_str_eval (ini_strings_t *is, ini_sct_t *sct, int free)
92{
93 int r;
94
95 r = ini_read_str (sct, is->str);
96
97 if (free) {
98 ini_str_free (is);
99 }
100
101 return (r);
102}