]> Nishi Git Mirror - dataworks.git/commitdiff
can list database now
authornishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Wed, 22 May 2024 01:58:18 +0000 (01:58 +0000)
committernishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Wed, 22 May 2024 01:58:18 +0000 (01:58 +0000)
git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@64 d4a5a174-5a4a-5b4b-b672-37683c10d7d5

Client/main.c
Library/database.c
Library/dw_database.h
Library/dw_parser.h
Library/dw_util.h
Library/util.c

index c7b221bd67f1f2bd9062a351ac1e99bb22848be1..e7338678f35b0338fe4def9bac06f8abd7345d20 100644 (file)
@@ -152,18 +152,19 @@ int main(int argc, char** argv) {
                                        printf("Shows the version of DataWorks.\n");
                                        padleft(16, ".tables");
                                        printf("Shows the table list.\n");
-                               } else if(__dw_strcaseequ(buf, ".tables")){
+                               } else if(__dw_strcaseequ(buf, ".tables")) {
                                        char** list = dataworks_database_get_table_list(db);
-                                       if(list != NULL){
+                                       if(list != NULL) {
                                                int i;
-                                               for(i = 0; list[i] != NULL; i++);
+                                               for(i = 0; list[i] != NULL; i++)
+                                                       ;
                                                printf("%d tables found.\n", i);
-                                               for(i = 0; list[i] != NULL; i++){
+                                               for(i = 0; list[i] != NULL; i++) {
                                                        printf("  %s\n", list[i]);
                                                        free(list[i]);
                                                }
                                                free(list);
-                                       }else{
+                                       } else {
                                                printf("Failed to get the list.\n");
                                        }
                                } else {
index 0b856577cb7ce84d03ae12d0d59b47c7bc008a45..a55072079721643ae023d5e1c28048245d0216bf 100644 (file)
@@ -120,40 +120,47 @@ int dataworks_database_get_version(struct dataworks_db* db) { return db->version
 uint64_t dataworks_database_get_mtime(struct dataworks_db* db) { return db->mtime; }
 
 #define buffer_to_db_v1_indexentry(buf, index) \
-                       memcpy(&index.flag, buf, 1); \
-                       uint64_t be_count; \
-                       memcpy(&be_count, buf + 1, 8); \
-                       __dw_native_endian(be_count, uint64_t, index.count = __converted); \
-                       memcpy(&index.dbname_len, buf + 1 + 8, 1); \
-                       memcpy(index.dbname, buf + 1 + 8 + 1, 256); \
-                       memcpy(index.fields, buf + 1 + 8 + 1 + 256, 4096);
+       memcpy(&index.flag, buf, 1); \
+       uint64_t be_count; \
+       memcpy(&be_count, buf + 1, 8); \
+       __dw_native_endian(be_count, uint64_t, index.count = __converted); \
+       memcpy(&index.dbname_len, buf + 1 + 8, 1); \
+       memcpy(index.dbname, buf + 1 + 8 + 1, 256); \
+       memcpy(index.fields, buf + 1 + 8 + 1 + 256, 4096);
 
-char** dataworks_database_get_table_list(struct dataworks_db* db){
-       if(db->version == 1){
+char** dataworks_database_get_table_list(struct dataworks_db* db) {
+       if(db->version == 1) {
                __dw_lockfile(db->fp);
                fseek(db->fp, sizeof(sig) + 10, SEEK_SET);
                int i;
                struct dataworks_db_v1_indexentry index;
                char* buf = malloc(1 + 8 + 1 + 256 + 4096);
-               for(i = 0; i < 256; i++){
+               int c = 0;
+               for(i = 0; i < 256; i++) {
                        fread(buf, 1, 1 + 8 + 1 + 256 + 4096, db->fp);
                        buffer_to_db_v1_indexentry(buf, index);
+                       if(index.flag & DATAWORKS_V1_INDEXENTRY_USED) {
+                               c++;
+                       }
                }
+               char** list = malloc(sizeof(*list) * (c + 1));
                fseek(db->fp, sizeof(sig) + 10, SEEK_SET);
-               for(i = 0; i < 256; i++){
+               c = 0;
+               for(i = 0; i < 256; i++) {
                        fread(buf, 1, 1 + 8 + 1 + 256 + 4096, db->fp);
                        buffer_to_db_v1_indexentry(buf, index);
-                       if(index.flag & DATAWORKS_V1_INDEXENTRY_USED){
-                               printf("%d\n", index.dbname_len);
-                               printf("[");
-                               fflush(stdout);
-                               fwrite(index.dbname, 1, index.dbname_len, stdout);
-                               printf("]\n");
+                       if(index.flag & DATAWORKS_V1_INDEXENTRY_USED) {
+                               list[c] = malloc(index.dbname_len + 1);
+                               memcpy(list[c], index.dbname, index.dbname_len);
+                               list[c][index.dbname_len] = 0;
+                               c++;
                        }
                }
+               list[c] = NULL;
                free(buf);
                __dw_unlockfile(db->fp);
-       }else{
+               return list;
+       } else {
                /* Not implemented for the version */
                return NULL;
        }
index f8b463fe45596e1476b790e0e52164532688489c..7777781ae7d6fc4dc60203d12506efe08012d6b5 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: dw_database.h 7 2024-05-16 15:56:44Z nishi $ */
+/* $Id$ */
 /* --- START LICENSE --- */
 /* -------------------------------------------------------------------------- */
 /* Copyright (c) 2024 Nishi.                                                  */
@@ -155,7 +155,7 @@ uint64_t dataworks_database_get_mtime(struct dataworks_db* db);
  * @~english
  * @brief Get the table list of the database.
  * @param db Database
- * @return Table list of the databas 
+ * @return Table list of the databas
  *
  */
 char** dataworks_database_get_table_list(struct dataworks_db* db);
index b14c38c69d724ebd1201835ff87d4a177c14e65c..d734e61f5d8cba3e94901cc2d569069b36dede55 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: dw_util.h 7 2024-05-16 15:56:44Z nishi $ */
+/* $Id$ */
 /* --- START LICENSE --- */
 /* -------------------------------------------------------------------------- */
 /* Copyright (c) 2024 Nishi.                                                  */
index 549b0e536e2869da620deb38566b8aa64b2dcae9..e6c97a3eafed4cbb214eb8b6ce4d3b4f0ae844e5 100644 (file)
@@ -45,6 +45,7 @@ extern "C" {
 
 #include "dataworks.h"
 
+char* __dw_strdup(const char* a);
 bool __dw_strcaseequ(const char* a, const char* b);
 bool __dw_lockfile(FILE* fp);
 bool __dw_unlockfile(FILE* fp);
index dd53f41bbc173580d5a4b8604a093b4510d2de5c..11b7d3a6829adcf4a23110c87ee60b62fe23a345 100644 (file)
 #include <unistd.h>
 #endif
 
+char* __dw_strdup(const char* a) {
+       char* str = malloc(strlen(a) + 1);
+       memcpy(str, a, strlen(a));
+       str[strlen(a)] = 0;
+       return str;
+}
+
 bool __dw_strcaseequ(const char* a, const char* b) {
        if(strlen(a) != strlen(b)) return false;
        int i;