文字列のバッファ処理を変更
@@ -1,57 +1,62 @@ | ||
1 | 1 | #include "global.h" |
2 | 2 | |
3 | -int npl_mbstr_init(npl_mbstr_t *str, const char *s) | |
3 | +struct npl_mbstr_t { | |
4 | + npl_object_t object; | |
5 | + char *str; | |
6 | +}; | |
7 | + | |
8 | +npl_mbstr_t* npl_mbstr_new(const char *s) | |
4 | 9 | { |
5 | - assert(str); | |
6 | - assert(s); | |
10 | + size_t obj_size; | |
11 | + size_t len, str_size; | |
12 | + npl_mbstr_t *str; | |
7 | 13 | |
8 | - str->str = strdup(s); | |
9 | - if (str->str == NULL) | |
10 | - return 1; // error | |
11 | - | |
12 | - return 0; | |
13 | -} | |
14 | - | |
15 | -int npl_mbstr_init_from_wcs(npl_mbstr_t *str, const wchar_t *s) | |
16 | -{ | |
17 | - assert(str); | |
18 | 14 | assert(s); |
19 | 15 | |
20 | - str->str = npl_convert_to_mbs(s); | |
21 | - if (str->str == NULL) | |
22 | - return 1; // error | |
16 | + // 文字列サイズを計算 | |
17 | + len = strlen(s); | |
18 | + len += 1; | |
19 | + if (len < 1) | |
20 | + return NULL; | |
21 | + if (len > (SIZE_MAX / sizeof(char))) | |
22 | + return NULL; | |
23 | + str_size = len * sizeof(char); | |
23 | 24 | |
24 | - return 0; | |
25 | -} | |
25 | + // オブジェクトサイズを計算 | |
26 | + obj_size = str_size + sizeof(npl_mbstr_t); | |
27 | + if (obj_size < sizeof(npl_mbstr_t)) | |
28 | + return NULL; | |
26 | 29 | |
27 | -npl_mbstr_t* npl_mbstr_new(const char *s) | |
28 | -{ | |
29 | - npl_object_t *object; | |
30 | - | |
31 | - object = npl_object_new(npl_mbstr_type); | |
32 | - if (object == NULL) | |
30 | + // オブジェクト初期化 | |
31 | + str = malloc(obj_size); | |
32 | + if (str == NULL) | |
33 | 33 | return NULL; |
34 | - if (npl_mbstr_init(NPL_MBSTR(object), s)) { | |
35 | - npl_object_unref(object); | |
34 | + memset(str, 0, obj_size); | |
35 | + if (npl_object_init(&str->object, npl_mbstr_type)) { | |
36 | + free(str); | |
36 | 37 | return NULL; |
37 | 38 | } |
39 | + str->str = (void*)(str + 1); | |
38 | 40 | |
39 | - return NPL_MBSTR(object); | |
41 | + // 文字列コピー | |
42 | + strcpy(str->str, s); | |
43 | + | |
44 | + return str; | |
45 | + | |
40 | 46 | } |
41 | 47 | |
42 | 48 | npl_mbstr_t* npl_mbstr_new_from_wcs(const wchar_t *s) |
43 | 49 | { |
44 | - npl_object_t *object; | |
50 | + npl_mbstr_t *str; | |
51 | + char *mbs; | |
45 | 52 | |
46 | - object = npl_object_new(npl_mbstr_type); | |
47 | - if (object == NULL) | |
53 | + mbs = npl_convert_to_mbs(s); | |
54 | + if (mbs == NULL) | |
48 | 55 | return NULL; |
49 | - if (npl_mbstr_init_from_wcs(NPL_MBSTR(object), s)) { | |
50 | - npl_object_unref(object); | |
51 | - return NULL; | |
52 | - } | |
53 | 56 | |
54 | - return NPL_MBSTR(object); | |
57 | + str = npl_mbstr_new(mbs); | |
58 | + free(mbs); | |
59 | + return str; | |
55 | 60 | } |
56 | 61 | |
57 | 62 | char* npl_mbstr_get(npl_mbstr_t *str) |
@@ -60,16 +65,6 @@ | ||
60 | 65 | return str->str; |
61 | 66 | } |
62 | 67 | |
63 | -static void _destructor(npl_object_t *object) | |
64 | -{ | |
65 | - npl_mbstr_t *s = NPL_MBSTR(object); | |
66 | - | |
67 | - if (s->str != NULL) { | |
68 | - free(s->str); | |
69 | - s->str = NULL; | |
70 | - } | |
71 | -} | |
72 | - | |
73 | 68 | npl_type_t _npl_mbstr_type = { |
74 | 69 | .object = { |
75 | 70 | .type = &_npl_type_type, |
@@ -77,9 +72,9 @@ | ||
77 | 72 | .static_flag = 1, |
78 | 73 | }, |
79 | 74 | .name = L"mbstr", |
80 | - .destructor = _destructor, | |
75 | + .destructor = NULL, | |
81 | 76 | .parent = &_npl_object_type, |
82 | - .size = sizeof(npl_mbstr_t), | |
77 | + .size = 0, | |
83 | 78 | }; |
84 | 79 | |
85 | 80 | npl_type_t *npl_mbstr_type = &_npl_mbstr_type; |
@@ -52,6 +52,7 @@ | ||
52 | 52 | int static_flag; |
53 | 53 | }; |
54 | 54 | |
55 | +int npl_object_init(npl_object_t *object, npl_type_t *type); | |
55 | 56 | npl_object_t* npl_object_new(npl_type_t *type); |
56 | 57 | void npl_object_ref(npl_object_t *object); |
57 | 58 | void npl_object_unref(npl_object_t *object); |
@@ -86,13 +87,6 @@ | ||
86 | 87 | |
87 | 88 | // str class |
88 | 89 | // str はワイド文字の文字列。 |
89 | -struct npl_str_t { | |
90 | - npl_object_t object; | |
91 | - wchar_t *str; | |
92 | -}; | |
93 | - | |
94 | -int npl_str_init(npl_str_t *str, const wchar_t *s); | |
95 | -int npl_str_init_from_mbs(npl_str_t *str, const char *s); | |
96 | 90 | npl_str_t* npl_str_new(const wchar_t *s); |
97 | 91 | npl_str_t* npl_str_new_from_mbs(const char *s); |
98 | 92 | wchar_t* npl_str_get(npl_str_t *s); |
@@ -99,13 +93,6 @@ | ||
99 | 93 | |
100 | 94 | // mbstr class |
101 | 95 | // mbstr はマルチバイト文字の文字列。 |
102 | -struct npl_mbstr_t { | |
103 | - npl_object_t object; | |
104 | - char *str; | |
105 | -}; | |
106 | - | |
107 | -int npl_mbstr_init(npl_mbstr_t *str, const char *s); | |
108 | -int npl_mbstr_init_from_wcs(npl_mbstr_t *str, const wchar_t *s); | |
109 | 96 | npl_mbstr_t* npl_mbstr_new(const char *s); |
110 | 97 | npl_mbstr_t* npl_mbstr_new_from_wcs(const wchar_t *s); |
111 | 98 | char* npl_mbstr_get(npl_mbstr_t *str); |
@@ -112,10 +99,24 @@ | ||
112 | 99 | |
113 | 100 | // sbuf class |
114 | 101 | // StringBuffer |
102 | +struct npl_sbuf_t { | |
103 | + npl_object_t object; | |
104 | + | |
105 | + // buf のワイド文字数(バイト数でなく) | |
106 | + size_t bufsize; | |
107 | + | |
108 | + // ワイド文字列バッファへのポインタ | |
109 | + wchar_t *buf; | |
110 | +}; | |
111 | + | |
112 | +int npl_sbuf_init(npl_sbuf_t *sb); | |
115 | 113 | npl_sbuf_t* npl_sbuf_new(void); |
116 | 114 | int npl_sbuf_add(npl_sbuf_t *sb, const wchar_t *s); |
117 | 115 | npl_str_t* npl_sbuf_get(npl_sbuf_t *sb); |
118 | 116 | void npl_sbuf_clear(npl_sbuf_t *sb); |
117 | +wchar_t* npl_sbuf_get_buf(npl_sbuf_t *sb); | |
118 | +size_t npl_sbuf_get_bufsize(npl_sbuf_t *sb); | |
119 | +int npl_sbuf_change_bufsize(npl_sbuf_t *sb, size_t bufsize); | |
119 | 120 | |
120 | 121 | // stream class |
121 | 122 | struct npl_stream_t { |
@@ -9,18 +9,34 @@ | ||
9 | 9 | call_destructor(object, type->parent); |
10 | 10 | } |
11 | 11 | |
12 | +int npl_object_init(npl_object_t *object, npl_type_t *type) | |
13 | +{ | |
14 | + assert(object); | |
15 | + assert(type); | |
16 | + | |
17 | + object->type = type; | |
18 | + object->ref_count = 0; | |
19 | + object->static_flag = 0; | |
20 | + npl_object_ref(object); | |
21 | + | |
22 | + return 0; | |
23 | +} | |
24 | + | |
12 | 25 | npl_object_t* npl_object_new(npl_type_t *type) |
13 | 26 | { |
14 | 27 | npl_object_t *object; |
15 | 28 | |
16 | 29 | assert(type); |
30 | + assert(type->size != 0); | |
17 | 31 | |
18 | 32 | object = malloc(type->size); |
19 | 33 | if (object == NULL) |
20 | 34 | return NULL; // error |
21 | 35 | memset(object, 0, type->size); |
22 | - object->type = type; | |
23 | - npl_object_ref(object); | |
36 | + if (npl_object_init(object, type)) { | |
37 | + free(object); | |
38 | + return NULL; // error; | |
39 | + } | |
24 | 40 | |
25 | 41 | return object; |
26 | 42 | } |
@@ -1,44 +1,16 @@ | ||
1 | 1 | #include "global.h" |
2 | 2 | |
3 | -#define BUFSIZE 256 | |
3 | +#define DEFAULT_BUFSIZE 256 | |
4 | 4 | |
5 | -struct BufList { | |
6 | - struct BufList *next; | |
7 | - struct BufList *prev; | |
8 | - wchar_t buf[BUFSIZE]; | |
9 | -}; | |
10 | - | |
11 | -struct npl_sbuf_t { | |
12 | - npl_object_t object; | |
13 | - struct BufList root; | |
14 | - struct BufList *head; | |
15 | - struct BufList *tail; | |
16 | -}; | |
17 | - | |
18 | -static struct BufList* add_buf_list(npl_sbuf_t *buf) | |
5 | +int npl_sbuf_init(npl_sbuf_t *sb) | |
19 | 6 | { |
20 | - struct BufList *b; | |
7 | + assert(sb); | |
21 | 8 | |
22 | - b = malloc(sizeof(struct BufList)); | |
23 | - if (b == NULL) | |
24 | - return NULL; // error | |
25 | - memset(b, 0, sizeof(struct BufList)); | |
9 | + sb->buf = malloc(DEFAULT_BUFSIZE * sizeof(wchar_t)); | |
10 | + if (sb->buf == NULL) | |
11 | + return 1; // error | |
12 | + sb->bufsize = DEFAULT_BUFSIZE; | |
26 | 13 | |
27 | - b->prev = buf->tail; | |
28 | - buf->tail->next = b; | |
29 | - buf->tail = b; | |
30 | - | |
31 | - return b; | |
32 | -} | |
33 | - | |
34 | -static int _npl_sbuf_init(npl_sbuf_t *sbuf) | |
35 | -{ | |
36 | - assert(sbuf); | |
37 | - | |
38 | - memset(&sbuf->root, 0, sizeof(struct BufList)); | |
39 | - sbuf->head = &sbuf->root; | |
40 | - sbuf->tail = &sbuf->root; | |
41 | - | |
42 | 14 | return 0; |
43 | 15 | } |
44 | 16 |
@@ -49,7 +21,7 @@ | ||
49 | 21 | object = npl_object_new(npl_sbuf_type); |
50 | 22 | if (object == NULL) |
51 | 23 | return NULL; |
52 | - if (_npl_sbuf_init(NPL_SBUF(object))) { | |
24 | + if (npl_sbuf_init(NPL_SBUF(object))) { | |
53 | 25 | npl_object_unref(object); |
54 | 26 | return NULL; |
55 | 27 | } |
@@ -59,102 +31,97 @@ | ||
59 | 31 | |
60 | 32 | int npl_sbuf_add(npl_sbuf_t *sb, const wchar_t *s) |
61 | 33 | { |
62 | - struct BufList *buf; | |
63 | - size_t len, rest; | |
64 | - size_t ss, p; | |
34 | + size_t n, size; | |
35 | + int res; | |
65 | 36 | |
66 | 37 | assert(sb); |
67 | 38 | assert(s); |
68 | 39 | |
69 | - ss = wcslen(s); | |
70 | - p = 0; | |
71 | - buf = sb->tail; | |
72 | - while (ss != p) { | |
73 | - len = wcsnlen(buf->buf, BUFSIZE); | |
74 | - rest = BUFSIZE - len; | |
75 | - if (rest == 0) { | |
76 | - buf = add_buf_list(sb); | |
77 | - if (buf == NULL) | |
78 | - return 1; // error | |
79 | - continue; | |
80 | - } | |
81 | - wmemcpy(&buf->buf[len], &s[p], rest); | |
82 | - p += wcsnlen(&s[p], rest); | |
40 | + // バッファの必要サイズを計算し、 | |
41 | + // 足りなければ増やす。 | |
42 | + size = wcslen(sb->buf); | |
43 | + n = wcslen(s); | |
44 | + size += n; | |
45 | + if (size < n) | |
46 | + return 1; // error | |
47 | + size += 1; | |
48 | + if (size < 1) | |
49 | + return 1; // error | |
50 | + if (sb->bufsize < size) { | |
51 | + res = npl_sbuf_change_bufsize(sb, size); | |
52 | + if (res) | |
53 | + return 1; // error | |
83 | 54 | } |
84 | 55 | |
56 | + // バッファへ追加 | |
57 | + wcscat(sb->buf, s); | |
58 | + | |
85 | 59 | return 0; |
86 | 60 | } |
87 | 61 | |
88 | 62 | npl_str_t* npl_sbuf_get(npl_sbuf_t *sb) |
89 | 63 | { |
90 | - struct BufList *buf; | |
91 | - size_t len = 0, n; | |
92 | - size_t bytes; | |
93 | - wchar_t *ws; | |
94 | - npl_str_t *str; | |
64 | + assert(sb); | |
65 | + return npl_str_new(sb->buf);; | |
66 | +} | |
95 | 67 | |
68 | +void npl_sbuf_clear(npl_sbuf_t *sb) | |
69 | +{ | |
96 | 70 | assert(sb); |
71 | + wmemset(sb->buf, L'\0', sb->bufsize); | |
72 | +} | |
97 | 73 | |
98 | - // 文字列の長さを調べる | |
99 | - buf = sb->head; | |
100 | - while (buf != NULL) { | |
101 | - n = wcsnlen(buf->buf, BUFSIZE); | |
102 | - len += n; | |
103 | - if (len < n) | |
104 | - return NULL; // error | |
105 | - buf = buf->next; | |
106 | - } | |
74 | +wchar_t* npl_sbuf_get_buf(npl_sbuf_t *sb) | |
75 | +{ | |
76 | + assert(sb); | |
77 | + return sb->buf; | |
78 | +} | |
107 | 79 | |
108 | - // NUL文字の分 | |
109 | - len++; | |
110 | - if (len == 0) | |
111 | - return NULL; // error | |
80 | +size_t npl_sbuf_get_bufsize(npl_sbuf_t *sb) | |
81 | +{ | |
82 | + assert(sb); | |
83 | + return sb->bufsize; | |
84 | +} | |
112 | 85 | |
113 | - // バイト換算 | |
114 | - if (len > (SIZE_MAX / sizeof(wchar_t))) | |
115 | - return NULL; // error | |
116 | - bytes = len * sizeof(wchar_t); | |
86 | +int npl_sbuf_change_bufsize(npl_sbuf_t *sb, size_t bufsize) | |
87 | +{ | |
88 | + size_t n, rest; | |
89 | + size_t bytes; | |
90 | + void *res; | |
117 | 91 | |
118 | - // メモリ確保 | |
119 | - ws = malloc(bytes); | |
120 | - if (ws == NULL) | |
121 | - return NULL; // error | |
122 | - wmemset(ws, L'\0', len); | |
92 | + assert(sb); | |
93 | + assert(bufsize >= 1); | |
123 | 94 | |
124 | - // 文字をコピー | |
125 | - buf = sb->head; | |
126 | - while (buf != NULL) { | |
127 | - wcsncat(ws, buf->buf, BUFSIZE); | |
128 | - buf = buf->next; | |
129 | - } | |
95 | + // バッファのバイト数を計算 | |
96 | + if (bufsize > (SIZE_MAX / sizeof(wchar_t))) | |
97 | + return 1; // error | |
98 | + bytes = bufsize * sizeof(wchar_t); | |
130 | 99 | |
131 | - // npl_str_t* にする | |
132 | - str = npl_str_new(ws); | |
133 | - free(ws); | |
100 | + // n, rest | |
101 | + n = sb->bufsize; | |
102 | + if (n < bufsize) | |
103 | + rest = bufsize - n; | |
104 | + else | |
105 | + rest = 0; | |
134 | 106 | |
135 | - return str; | |
136 | -} | |
107 | + // バッファ割り当て | |
108 | + res = realloc(sb->buf, bytes); | |
109 | + if (res == NULL) | |
110 | + return 1; // error | |
111 | + sb->buf = res; | |
112 | + sb->bufsize = bufsize; | |
137 | 113 | |
138 | -void npl_sbuf_clear(npl_sbuf_t *sb) | |
139 | -{ | |
140 | - struct BufList *b1, *b2; | |
114 | + // 増えた分のバッファをクリア | |
115 | + if (rest > 0) | |
116 | + wmemset(&sb->buf[n], L'\0', rest); | |
141 | 117 | |
142 | - assert(sb); | |
143 | - | |
144 | - b1 = sb->head->next; | |
145 | - while (b1 != NULL) { | |
146 | - b2 = b1->next; | |
147 | - free(b1); | |
148 | - b1 = b2; | |
149 | - } | |
150 | - _npl_sbuf_init(sb); | |
118 | + return 0; | |
151 | 119 | } |
152 | 120 | |
153 | 121 | static void _destructor(npl_object_t *object) |
154 | 122 | { |
155 | 123 | npl_sbuf_t *sb = NPL_SBUF(object); |
156 | - | |
157 | - npl_sbuf_clear(sb); | |
124 | + free(sb->buf); | |
158 | 125 | } |
159 | 126 | |
160 | 127 | static npl_type_t _npl_sbuf_type = { |
@@ -1,57 +1,61 @@ | ||
1 | 1 | #include "global.h" |
2 | 2 | |
3 | -int npl_str_init(npl_str_t *str, const wchar_t *s) | |
3 | +struct npl_str_t { | |
4 | + npl_object_t object; | |
5 | + wchar_t *str; | |
6 | +}; | |
7 | + | |
8 | +npl_str_t* npl_str_new(const wchar_t *s) | |
4 | 9 | { |
5 | - assert(str); | |
6 | - assert(s); | |
10 | + size_t obj_size; | |
11 | + size_t len, str_size; | |
12 | + npl_str_t *str; | |
7 | 13 | |
8 | - str->str = wcsdup(s); | |
9 | - if (str->str == NULL) | |
10 | - return 1; // error | |
11 | - | |
12 | - return 0; | |
13 | -} | |
14 | - | |
15 | -int npl_str_init_from_mbs(npl_str_t *str, const char *s) | |
16 | -{ | |
17 | - assert(str); | |
18 | 14 | assert(s); |
19 | 15 | |
20 | - str->str = npl_convert_to_wcs(s); | |
21 | - if (str->str == NULL) | |
22 | - return 1; // error | |
16 | + // 文字列サイズを計算 | |
17 | + len = wcslen(s); | |
18 | + len += 1; | |
19 | + if (len < 1) | |
20 | + return NULL; | |
21 | + if (len > (SIZE_MAX / sizeof(wchar_t))) | |
22 | + return NULL; | |
23 | + str_size = len * sizeof(wchar_t); | |
23 | 24 | |
24 | - return 0; | |
25 | -} | |
25 | + // オブジェクトサイズを計算 | |
26 | + obj_size = str_size + sizeof(npl_str_t); | |
27 | + if (obj_size < sizeof(npl_str_t)) | |
28 | + return NULL; | |
26 | 29 | |
27 | -npl_str_t* npl_str_new(const wchar_t *s) | |
28 | -{ | |
29 | - npl_object_t *object; | |
30 | - | |
31 | - object = npl_object_new(npl_str_type); | |
32 | - if (object == NULL) | |
30 | + // オブジェクト初期化 | |
31 | + str = malloc(obj_size); | |
32 | + if (str == NULL) | |
33 | 33 | return NULL; |
34 | - if (npl_str_init(NPL_STR(object), s)) { | |
35 | - npl_object_unref(object); | |
34 | + memset(str, 0, obj_size); | |
35 | + if (npl_object_init(&str->object, npl_str_type)) { | |
36 | + free(str); | |
36 | 37 | return NULL; |
37 | 38 | } |
39 | + str->str = (void*)(str + 1); | |
38 | 40 | |
39 | - return NPL_STR(object); | |
41 | + // 文字列コピー | |
42 | + wcscpy(str->str, s); | |
43 | + | |
44 | + return str; | |
40 | 45 | } |
41 | 46 | |
42 | 47 | npl_str_t* npl_str_new_from_mbs(const char *s) |
43 | 48 | { |
44 | - npl_object_t *object; | |
49 | + npl_str_t *str; | |
50 | + wchar_t *ws; | |
45 | 51 | |
46 | - object = npl_object_new(npl_str_type); | |
47 | - if (object == NULL) | |
52 | + ws = npl_convert_to_wcs(s); | |
53 | + if (ws == NULL) | |
48 | 54 | return NULL; |
49 | - if (npl_str_init_from_mbs(NPL_STR(object), s)) { | |
50 | - npl_object_unref(object); | |
51 | - return NULL; | |
52 | - } | |
53 | 55 | |
54 | - return NPL_STR(object); | |
56 | + str = npl_str_new(ws); | |
57 | + free(ws); | |
58 | + return str; | |
55 | 59 | } |
56 | 60 | |
57 | 61 | wchar_t* npl_str_get(npl_str_t *str) |
@@ -60,16 +64,6 @@ | ||
60 | 64 | return str->str; |
61 | 65 | } |
62 | 66 | |
63 | -static void _destructor(npl_object_t *object) | |
64 | -{ | |
65 | - npl_str_t *s = NPL_STR(object); | |
66 | - | |
67 | - if (s->str != NULL) { | |
68 | - free(s->str); | |
69 | - s->str = NULL; | |
70 | - } | |
71 | -} | |
72 | - | |
73 | 67 | npl_type_t _npl_str_type = { |
74 | 68 | .object = { |
75 | 69 | .type = &_npl_type_type, |
@@ -77,9 +71,9 @@ | ||
77 | 71 | .static_flag = 1, |
78 | 72 | }, |
79 | 73 | .name = L"str", |
80 | - .destructor = _destructor, | |
74 | + .destructor = NULL, | |
81 | 75 | .parent = &_npl_object_type, |
82 | - .size = sizeof(npl_str_t), | |
76 | + .size = 0, | |
83 | 77 | }; |
84 | 78 | |
85 | 79 | npl_type_t *npl_str_type = &_npl_str_type; |
@@ -1,64 +0,0 @@ | ||
1 | -/* mysbuf.c | |
2 | - * | |
3 | - * StringBufferを使い文字列を作る。 | |
4 | - */ | |
5 | - | |
6 | -#include <stdio.h> | |
7 | -#include <stdlib.h> | |
8 | -#include "../npl.h" | |
9 | - | |
10 | -int main() | |
11 | -{ | |
12 | - npl_sbuf_t *sbuf; | |
13 | - npl_str_t *str; | |
14 | - | |
15 | - // NPLライブラリ初期化 | |
16 | - if (npl_init()) { | |
17 | - fprintf(stderr, "npl_init() failed.\n"); | |
18 | - exit(1); | |
19 | - } | |
20 | - | |
21 | - // sbuf生成 | |
22 | - sbuf = npl_sbuf_new(); | |
23 | - if (sbuf == NULL) { | |
24 | - fprintf(stderr, "npl_sbuf_new() failed.\n"); | |
25 | - exit(1); | |
26 | - } | |
27 | - | |
28 | - // sbufに文字を追加する | |
29 | - if (npl_sbuf_add(sbuf, L"あいうえお")) { | |
30 | - fprintf(stderr, "npl_sbuf_add() failed.\n"); | |
31 | - exit(1); | |
32 | - } | |
33 | - if (npl_sbuf_add(sbuf, L"123")) { | |
34 | - fprintf(stderr, "npl_sbuf_add() failed.\n"); | |
35 | - exit(1); | |
36 | - } | |
37 | - if (npl_sbuf_add(sbuf, L"ABC")) { | |
38 | - fprintf(stderr, "npl_sbuf_add() failed.\n"); | |
39 | - exit(1); | |
40 | - } | |
41 | - | |
42 | - // sbufからstrを得る | |
43 | - str = npl_sbuf_get(sbuf); | |
44 | - if (str == NULL) { | |
45 | - fprintf(stderr, "npl_sbuf_get() failed.\n"); | |
46 | - exit(1); | |
47 | - } | |
48 | - | |
49 | - // 文字列の内容を表示する | |
50 | - printf("\"%S\"\n", npl_str_get(str)); | |
51 | - | |
52 | - // sbuf, str の使用を終える | |
53 | - npl_object_unref(NPL_OBJECT(str)); | |
54 | - npl_object_unref(NPL_OBJECT(sbuf)); | |
55 | - | |
56 | - // NPLライブラリ終了処理 | |
57 | - if (npl_final()) { | |
58 | - fprintf(stderr, "npl_final() failed.\n"); | |
59 | - exit(1); | |
60 | - } | |
61 | - | |
62 | - return 0; | |
63 | -} | |
64 | - |
@@ -1,4 +1,4 @@ | ||
1 | -all: init hello mysbuf echo | |
1 | +all: init hello strbuf echo string | |
2 | 2 | |
3 | 3 | init: init.c |
4 | 4 | gcc -oinit init.c ../libnpl.a |
@@ -6,15 +6,19 @@ | ||
6 | 6 | hello: hello.c |
7 | 7 | gcc -ohello hello.c ../libnpl.a |
8 | 8 | |
9 | -mysbuf: mysbuf.c | |
10 | - gcc -omysbuf mysbuf.c ../libnpl.a | |
9 | +strbuf: strbuf.c | |
10 | + gcc -ostrbuf strbuf.c ../libnpl.a | |
11 | 11 | |
12 | 12 | echo: echo.c |
13 | 13 | gcc -oecho echo.c ../libnpl.a |
14 | 14 | |
15 | +string: string.c | |
16 | + gcc -ostring string.c ../libnpl.a | |
17 | + | |
15 | 18 | clean: |
16 | 19 | rm -f init |
17 | 20 | rm -f hello |
18 | - rm -f mysbuf | |
21 | + rm -f strbuf | |
19 | 22 | rm -f echo |
23 | + rm -f string | |
20 | 24 |
@@ -0,0 +1,64 @@ | ||
1 | +/* strbuf.c | |
2 | + * | |
3 | + * StringBufferを使い文字列を作る。 | |
4 | + */ | |
5 | + | |
6 | +#include <stdio.h> | |
7 | +#include <stdlib.h> | |
8 | +#include "../npl.h" | |
9 | + | |
10 | +int main() | |
11 | +{ | |
12 | + npl_sbuf_t *sbuf; | |
13 | + npl_str_t *str; | |
14 | + | |
15 | + // NPLライブラリ初期化 | |
16 | + if (npl_init()) { | |
17 | + fprintf(stderr, "npl_init() failed.\n"); | |
18 | + exit(1); | |
19 | + } | |
20 | + | |
21 | + // sbuf生成 | |
22 | + sbuf = npl_sbuf_new(); | |
23 | + if (sbuf == NULL) { | |
24 | + fprintf(stderr, "npl_sbuf_new() failed.\n"); | |
25 | + exit(1); | |
26 | + } | |
27 | + | |
28 | + // sbufに文字を追加する | |
29 | + if (npl_sbuf_add(sbuf, L"あいうえお")) { | |
30 | + fprintf(stderr, "npl_sbuf_add() failed.\n"); | |
31 | + exit(1); | |
32 | + } | |
33 | + if (npl_sbuf_add(sbuf, L"123")) { | |
34 | + fprintf(stderr, "npl_sbuf_add() failed.\n"); | |
35 | + exit(1); | |
36 | + } | |
37 | + if (npl_sbuf_add(sbuf, L"ABC")) { | |
38 | + fprintf(stderr, "npl_sbuf_add() failed.\n"); | |
39 | + exit(1); | |
40 | + } | |
41 | + | |
42 | + // sbufからstrを得る | |
43 | + str = npl_sbuf_get(sbuf); | |
44 | + if (str == NULL) { | |
45 | + fprintf(stderr, "npl_sbuf_get() failed.\n"); | |
46 | + exit(1); | |
47 | + } | |
48 | + | |
49 | + // 文字列の内容を表示する | |
50 | + printf("\"%S\"\n", npl_str_get(str)); | |
51 | + | |
52 | + // sbuf, str の使用を終える | |
53 | + npl_object_unref(NPL_OBJECT(str)); | |
54 | + npl_object_unref(NPL_OBJECT(sbuf)); | |
55 | + | |
56 | + // NPLライブラリ終了処理 | |
57 | + if (npl_final()) { | |
58 | + fprintf(stderr, "npl_final() failed.\n"); | |
59 | + exit(1); | |
60 | + } | |
61 | + | |
62 | + return 0; | |
63 | +} | |
64 | + |
@@ -0,0 +1,48 @@ | ||
1 | +/* string.c | |
2 | + * | |
3 | + */ | |
4 | + | |
5 | +#include <stdio.h> | |
6 | +#include <stdlib.h> | |
7 | +#include "../npl.h" | |
8 | + | |
9 | +int main() | |
10 | +{ | |
11 | + npl_str_t *s1, *s2; | |
12 | + npl_mbstr_t *m1, *m2; | |
13 | + | |
14 | + // NPLライブラリ初期化 | |
15 | + if (npl_init()) { | |
16 | + fprintf(stderr, "npl_init() failed.\n"); | |
17 | + exit(1); | |
18 | + } | |
19 | + | |
20 | + s1 = npl_str_new(L"あいうえお"); | |
21 | + s2 = npl_str_new_from_mbs("かきくけこ"); | |
22 | + m1 = npl_mbstr_new("さしすせそ"); | |
23 | + m2 = npl_mbstr_new_from_wcs(L"たちつてと"); | |
24 | + | |
25 | + if (s1 && s2 && m1 && m2) { | |
26 | + printf("%S\n", npl_str_get(s1)); | |
27 | + printf("%S\n", npl_str_get(s2)); | |
28 | + printf("%s\n", npl_mbstr_get(m1)); | |
29 | + printf("%s\n", npl_mbstr_get(m2)); | |
30 | + } else { | |
31 | + fprintf(stderr, "error!\n"); | |
32 | + exit(1); | |
33 | + } | |
34 | + | |
35 | + npl_object_unref(NPL_OBJECT(s1)); | |
36 | + npl_object_unref(NPL_OBJECT(s2)); | |
37 | + npl_object_unref(NPL_OBJECT(m1)); | |
38 | + npl_object_unref(NPL_OBJECT(m2)); | |
39 | + | |
40 | + // NPLライブラリ終了処理 | |
41 | + if (npl_final()) { | |
42 | + fprintf(stderr, "npl_final() failed.\n"); | |
43 | + exit(1); | |
44 | + } | |
45 | + | |
46 | + return 0; | |
47 | +} | |
48 | + |