ktable.c

最終更新:2009/10/10

ktable.c

001: /* ktable.c
002:  * テーブルの例
003:  * TABLE: apr_table_make / apr_table_set / apr_table_setn /
004:  * apr_table_get / apr_table_elts / apr_table_add / apr_table_unset /
005:  * apr_is_empty_table / apr_table_clear
006:  */
007: 
008: #include "apr_general.h"
009: #include "apr_file_io.h"
010: 
011: #include "apr_tables.h"
012: 
013: #include "apr_strings.h"
014: 
015: #include "mystab.h"
016: 
017: /*
018:  * apr_table_do 用コールバック関数
019:  * ctxにはコールバック関数へのコンテクストデータ。
020:  * この例ではファイルポインタ渡しに使用。
021:  */
022: 
023: int table_cb (void *ctx, const char *key, const char *value) {
024:   apr_file_t *out = (apr_file_t*)ctx;
025:   apr_file_printf(out, "(%s)=(%s)\n", key, value);
026:   return TRUE;/* TRUE:continue iteration. FALSE:stop iteration */
027: }
028: 
029: /*
030:  * apr_table_do 用コールバック関数
031:  * この例ではキー・値をコンカテネートしてみる。
032:  */
033: 
034: typedef struct mydata {
035:   apr_pool_t *pool;
036:   char *str; /* あらかじめ、"" で初期化すること*/
037: } mydata;
038: 
039: int table_cb2 (void *ctx, const char *key, const char *value) {
040:   mydata *md = (mydata*)ctx;
041:   md->str = apr_psprintf(md->pool, "%s{%s}={%s}\n", md->str, key, value);
042:   return TRUE;/* TRUE:continue iteration. FALSE:stop iteration */
043: }
044: 
045: 
046: int apr_my_main (int ac, char **av, apr_file_t * astdin, apr_file_t * astdout, apr_file_t * astderr, apr_pool_t * pool) {
047: 
048:   char *keys[] = {
049:     "key0"
050:     , "key1"
051:     , "key2"
052:     , "key3"
053:   };
054: 
055:   char *values[] = {
056:     "value0"
057:     , "value1"
058:     , "value2"
059:     , "value3"
060:   };
061: 
062:   /* apr_table_make / apr_table_set / apr_table_setn / apr_table_get / apr_table_elts / apr_table_add
063:    * apr_table_unset / apr_is_empty_table apr_table_clear
064:    * INCLUDE: apr_tables.h
065:    * LIBS: libapr-1.lib
066:    */
067:   {
068:     apr_table_t * tab;
069:     int i;
070:     
071:     /* テーブルの生成 */
072: 
073:     tab = apr_table_make(pool, 10); /* 第二引数はテーブルのサイズを指定するが、動的に変化する。*/
074: 
075:     /* テーブルは空?(1 になるはず) */
076: 
077:     apr_file_printf(astdout, "apr_is_empty_table=[%d]\n", apr_is_empty_table(tab));
078: 
079:     /* キー・値の追加 */
080: 
081:     for (i=0; i<4; i++) {
082:       /* apr_table_set はキーとバリューの値を「複製」する。apr_table_setn とすると「複製」されない*/
083:       apr_table_set(tab, keys[i], values[i]);
084:     }
085:     
086:     apr_table_set(tab, "FOO", "VALUE0");
087:     apr_table_set(tab, "BAR", "VALUE1");
088:     apr_table_set(tab, "QUUX", "VALUE2");
089: 
090:     /* テーブルは空?(0 になるはず) */
091: 
092:     apr_file_printf(astdout, "apr_is_empty_table=[%d]\n", apr_is_empty_table(tab));
093: 
094:     /* キーに対応する値の表示 */
095: 
096:     apr_file_printf(astdout, "---------------------------------------------\n");
097: 
098:     apr_file_printf(astdout, "[%s]=>[%s]\n", "QUUX", apr_table_get(tab, "QUUX"));
099: 
100:     for (i=0; i<4; i++) {
101:       apr_file_printf(astdout, "[%s]=>[%s]\n", keys[i], apr_table_get(tab, keys[i]));
102:     }
103: 
104:     /* 存在しないキーに対応する値の表示(NULL になる)*/
105:     
106:     apr_file_printf(astdout, "[%s]=>[%s]\n", "UNKNOWN", apr_table_get(tab, "UNKNOWN"));
107: 
108:     /* 格納されたキー・値の個数の表示 */
109: 
110:     if (1) {
111:       const apr_array_header_t *tarr = apr_table_elts(tab);
112:       apr_file_printf(astdout, "---------------------------------------------\n");
113:       apr_file_printf(astdout, "size=[%d]\n", tarr->nelts);
114:     }
115: 
116:     /* 格納されたキー・値の一覧表示(その1) */
117: 
118:     if (1) {
119:       const apr_table_entry_t *telts;
120:       const apr_array_header_t *tarr = apr_table_elts(tab);
121: 
122:       telts = (const apr_table_entry_t*)tarr->elts;
123:       
124:       apr_file_printf(astdout, "---------------------------------------------\n");
125:       for (i = 0; i < tarr->nelts; i++) {
126:           apr_file_printf(astdout, "%d: [%s]=[%s]\n", i, telts[i].key, telts[i].val);
127:       }
128:     }
129: 
130:     /* 格納されたキー・値の一覧表示(その2) */
131: 
132:     if (1) {
133:       apr_file_printf(astdout, "---------------------------------------------\n");
134:       apr_table_do(table_cb, astdout, tab, NULL);
135:     }
136: 
137:     /* 格納されたキー・値の一覧表示(その2・コールバック関数もう一例) */
138: 
139:     if (1) {
140:       mydata md;
141:       apr_file_printf(astdout, "#---------------------------------------------\n");
142:       md.pool = pool;
143:       md.str = "";
144:       apr_table_do(table_cb2, &md, tab, NULL);
145:       apr_file_printf(astdout, "%s", md.str);
146:     }
147:     
148:     /* 格納されたキー・値の一覧表示(その2・コールバック関数もう一例+フィルタ) */
149: 
150:     if (1) {
151:       mydata md;
152:       apr_file_printf(astdout, "#---------------------------------------------\n");
153:       md.pool = pool;
154:       md.str = "";
155:       apr_table_do(table_cb2, &md, tab, "FOO", "BAR", "QUUX", NULL); /* キーが FOO, BAR, QUUX となるもののみ処理される*/
156:       apr_file_printf(astdout, "%s", md.str);
157:     }
158:     
159:     
160:     /* 値の変更 */
161:     apr_table_set(tab, "QUUX", "ANOTHER VALUE");
162:     apr_table_set(tab, "key3", "ANOTHER VALUE2");
163: 
164:     /* 格納されたキー・値の一覧表示 */
165: 
166:     if (1) {
167:       apr_file_printf(astdout, "---------------------------------------------\n");
168:       apr_table_do(table_cb, astdout, tab, NULL);
169:     }
170:     
171:     /* すでに存在するキーの値の追加(多重値) */
172:     /* apr_table_add はキーとバリューの値を「複製」する。apr_table_addn とすると「複製」されない*/
173: 
174:     apr_table_add(tab, "FOO", "ANOTHER VALUE3");
175:     apr_table_add(tab, "key2", "ANOTHER VALUE4");
176: 
177:     /* 格納されたキー・値の一覧表示 */
178: 
179:     if (1) {
180:       apr_file_printf(astdout, "---------------------------------------------\n");
181:       apr_table_do(table_cb, astdout, tab, NULL);
182:     }
183: 
184:     /* 多重値になっているキーの値を apr_table_get すると(最初に登録したものになる) */
185: 
186:     if (1) {
187:       apr_file_printf(astdout, "---------------------------------------------\n");
188:       apr_file_printf(astdout, "[%s]=>[%s]\n", "FOO", apr_table_get(tab, "FOO"));
189:       apr_file_printf(astdout, "[%s]=>[%s]\n", "key2", apr_table_get(tab, "key2"));
190:     }
191:     
192:     /* キー・値の削除 */
193:     
194:     apr_table_unset(tab, "BAR");
195:     apr_table_unset(tab, "key0");
196:     
197:     /* 格納されたキー・値の一覧表示 */
198: 
199:     if (1) {
200:       apr_file_printf(astdout, "---------------------------------------------\n");
201:       apr_table_do(table_cb, astdout, tab, NULL);
202:     }
203: 
204:     /* 多重値になっているキー・値の削除 */
205:     
206:     apr_table_unset(tab, "FOO");
207:     apr_table_unset(tab, "key2");
208:     
209:     /* 格納されたキー・値の一覧表示 */
210: 
211:     if (1) {
212:       apr_file_printf(astdout, "---------------------------------------------\n");
213:       apr_table_do(table_cb, astdout, tab, NULL);
214:     }
215: 
216:     /* テーブルのクリア */
217:     apr_table_clear(tab);
218: 
219:     /* テーブルは空?(1 になるはず) */
220: 
221:     apr_file_printf(astdout, "apr_is_empty_table=[%d]\n", apr_is_empty_table(tab));
222: 
223: 
224:     /* 格納されたキー・値の一覧表示(空なのでコールバック関数は呼ばれない) */
225: 
226:     if (1) {
227:       apr_file_printf(astdout, "---------------------------------------------\n");
228:       apr_table_do(table_cb, astdout, tab, NULL);
229:     }
230: 
231: 
232:     /* より汎用的には、こんな使い方が安全なのでは? */
233:     if (tab && ! apr_is_empty_table(tab)) {
234:       apr_table_do(table_cb, astdout, tab, NULL);
235:     }
236:     
237:   }
238: 
239:   
240:   apr_file_printf(astdout, "\ndone.\n");
241:   return 0; /* 正常終了 */
242: }
Copyright (C) KAKU PROJECT (2009)KAKU PROJECT (2009)