[Swfed-svn] swfed-svn [336] y_strtable ユーティリティーを追加

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2010年 11月 29日 (月) 01:49:21 JST


Revision: 336
          http://sourceforge.jp/projects/swfed/svn/view?view=rev&revision=336
Author:   yoya
Date:     2010-11-29 01:49:21 +0900 (Mon, 29 Nov 2010)

Log Message:
-----------
y_strtable ユーティリティーを追加

Modified Paths:
--------------
    trunk/src/config.m4

Added Paths:
-----------
    trunk/src/y_strtable.c
    trunk/src/y_strtable.h


-------------- next part --------------
Modified: trunk/src/config.m4
===================================================================
--- trunk/src/config.m4	2010-11-28 16:32:31 UTC (rev 335)
+++ trunk/src/config.m4	2010-11-28 16:49:21 UTC (rev 336)
@@ -73,6 +73,6 @@
 	swf_shape_record.c swf_shape_record_end.c swf_shape_record_setup.c \
 	swf_shape_record_edge.c swf_gradient.c  swf_gradient_record.c \
 	swf_tag_jpeg.c swf_tag_edit.c swf_tag_action.c swf_tag_lossless.c \
-	swf_tag_sound.c swf_tag_sprite.c swf_tag_shape.c \
+	swf_tag_sound.c swf_tag_sprite.c swf_tag_shape.c y_strtable.c \
 	, $ext_shared)
 fi

Added: trunk/src/y_strtable.c
===================================================================
--- trunk/src/y_strtable.c	                        (rev 0)
+++ trunk/src/y_strtable.c	2010-11-28 16:49:21 UTC (rev 336)
@@ -0,0 +1,107 @@
+#include <stdio.h>
+#include <malloc.h>
+#include <string.h>
+#include "swf_define.h"
+#include "y_strtable.h"
+
+#define Y_STRTABLE_INITSIZE 10
+
+y_strtable_t *
+y_strtable_open() {
+    int i;
+    y_strtable_t *st = calloc(sizeof(*st), 1);
+    if (st == NULL) {
+        fprintf(stderr, "y_strtable_open: calloc failed\n");
+        return NULL;
+    }
+    st->use_len = 0;
+    st->alloc_len = Y_STRTABLE_INITSIZE;
+    st->table = malloc(sizeof(st->table) * Y_STRTABLE_INITSIZE);
+    return st;
+}
+
+int
+y_strtable_set(y_strtable_t *st, char *key, int key_len, char *value, int value_len) {
+    int i, offset = -1;
+    void *tmp;
+    if (st->use_len < st->alloc_len) {
+        offset = st->use_len;
+    } else {
+        for (i = 0 ; i < st->alloc_len ; i++) {
+            if (st->table[i].use == 0) {
+                offset = i;
+                break;
+            }
+        }
+        if (offset == -1) {
+            tmp = realloc(st->table, 2 * st->alloc_len);
+            if (tmp == NULL) {
+                fprintf(stderr, "y_strtable_set: realloc failed\n");
+                return 1;
+            }
+            st->table = tmp;
+            st->alloc_len = 2 * st->alloc_len;
+            offset = st->use_len;
+        }
+    }
+    st->table[offset].key = malloc(key_len);
+    if (st->table[offset].key == NULL) {
+        
+    }
+    st->table[offset].value = malloc(value_len);
+    if (st->table[offset].value == NULL) {
+        free(st->table[offset].key);
+        return 1; // NG
+    }
+    memcpy(st->table[offset].key, key, key_len);
+    memcpy(st->table[offset].value, value, value_len);
+    st->table[offset].use = 1;
+    if (offset == st->use_len) {
+        st->use_len = offset + 1;
+    }
+}
+
+char *
+y_strtable_get(y_strtable_t *st, char *key, int key_len, int *value_len) {
+    int i;
+    for (i = 0 ; i < st->use_len ; i++) {
+        if ((st->table[i].use) &&
+            (st->table[i].key_len == key_len) &&
+            (memcmp(st->table[i].key, key, key_len) == 0)) {
+            *value_len = st->table[i].value_len;
+            return st->table[i].key;
+        }
+    }
+    return NULL;
+}
+
+int
+y_strtable_delete(y_strtable_t *st, char *key, int key_len) {
+    int i;
+    for (i = 0 ; i < st->use_len ; i++) {
+        if ((st->table[i].use) &&
+            (st->table[i].key_len == key_len) &&
+            (memcmp(st->table[i].key, key, key_len) == 0)) {
+            free(st->table[i].key);
+            free(st->table[i].value);
+            st->table[i].key = 0;
+            st->table[i].value = 0;
+            st->table[i].use = 0;
+            return 0;
+        }
+    }
+    return 1;
+}
+
+void
+y_strtable_close(y_strtable_t *st) {
+    int i;
+    for (i = 0 ; i < st->use_len ; i++) {
+        if (st->table[i].use) {
+            free(st->table[i].key);
+            free(st->table[i].value);
+        }
+    }
+    free(st->table);
+    free(st);
+}

Added: trunk/src/y_strtable.h
===================================================================
--- trunk/src/y_strtable.h	                        (rev 0)
+++ trunk/src/y_strtable.h	2010-11-28 16:49:21 UTC (rev 336)
@@ -0,0 +1,23 @@
+#ifndef __STRTABLE_H
+#define __STRTABLE_H
+
+typedef struct _y_strtable_entry_t {
+    char *key, *value;
+    int key_len, value_len;
+    int use;
+} y_strtable_entry_t;
+
+typedef struct _y_strtable_t {
+    int use_len;
+    int alloc_len;
+    y_strtable_entry_t *table;
+} y_strtable_t;
+
+extern y_strtable_t *y_strtable_open();
+extern int y_strtable_set(y_strtable_t *st, char *key, int key_len, char *value, int value_len);
+extern char *y_strtable_get(y_strtable_t *st, char *key, int key_len,int *value_len);
+extern int y_strtable_delete(y_strtable_t *st, char *key, int key_len);
+
+extern void y_strtable_close(y_strtable_t *st);
+
+#endif /* __STRTABLE_H */



Swfed-svn メーリングリストの案内
Back to archive index