DataWorks
Database Library/Client
Loading...
Searching...
No Matches
database_table.c
1/* $Id: database_table.c 78 2024-05-22 06:08:48Z nishi $ */
2/* --- START LICENSE --- */
3/* -------------------------------------------------------------------------- */
4/* Copyright (c) 2024 Crabware. */
5/* Redistribution and use in source and binary forms, with or without modific */
6/* ation, are permitted provided that the following conditions are met: */
7/* 1. Redistributions of source code must retain the above copyright noti */
8/* ce, this list of conditions and the following disclaimer. */
9/* 2. Redistributions in binary form must reproduce the above copyright n */
10/* otice, this list of conditions and the following disclaimer in the documen */
11/* tation and/or other materials provided with the distribution. */
12/* 3. Neither the name of the copyright holder nor the names of its contr */
13/* ibutors may be used to endorse or promote products derived from this softw */
14/* are without specific prior written permission. */
15/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS */
16/* " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, TH */
17/* E IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPO */
18/* SE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS */
19/* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CON */
20/* SEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITU */
21/* TE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPT */
22/* ION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, S */
23/* TRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN AN */
24/* Y WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY */
25/* OF SUCH DAMAGE. */
26/* -------------------------------------------------------------------------- */
27/* --- END LICENSE --- */
28
29#include "dw_database.h"
30
31#include "dw_util.h"
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36
37int dataworks_database_create_table(struct dataworks_db* db, const char* name, char** fields, const char* fieldtypes) {
38 if(db->version == 1) {
39 __dw_lockfile(db->fp);
40 fseek(db->fp, 3 + 10, SEEK_SET);
41 int i;
42 struct dataworks_db_v1_indexentry index;
43 char* buf = malloc(1 + 8 + 1 + 256 + 4096);
44 for(i = 0; i < 256; i++) {
45 fread(buf, 1, 1 + 8 + 1 + 256 + 4096, db->fp);
46 __dw_buffer_to_db_v1_indexentry(buf, index);
47 if(index.flag & DATAWORKS_V1_INDEXENTRY_USED) {
48 char* dbname = malloc(index.dbname_len + 1);
49 memcpy(dbname, index.dbname, index.dbname_len);
50 dbname[index.dbname_len] = 0;
51 if(strcmp(name, dbname) == 0) {
52 free(dbname);
53 free(buf);
54 __dw_unlockfile(db->fp);
55 return DW_ERR_USED;
56 }
57 }
58 }
59 fseek(db->fp, 3 + 10, SEEK_SET);
60 for(i = 0; i < 256; i++) {
61 fread(buf, 1, 1 + 8 + 1 + 256 + 4096, db->fp);
62 __dw_buffer_to_db_v1_indexentry(buf, index);
63 if(!(index.flag & DATAWORKS_V1_INDEXENTRY_USED)) {
64 fseek(db->fp, -(1 + 8 + 1 + 256 + 4096), SEEK_CUR);
66 buf[8 + 1] = strlen(name);
67 memcpy(buf + 1 + 8 + 1, name, strlen(name));
68 fwrite(buf, 1, 1 + 8 + 1 + 256, db->fp);
69 int j;
70 for(j = 0; fieldtypes[j] != 0; j++) {
71 fwrite(fieldtypes + j, 1, 1, db->fp);
72 fwrite(fields[j], 1, strlen(fields[j]), db->fp);
73 fwrite("\0", 1, 1, db->fp);
74 }
75 fwrite("\0", 1, 1, db->fp);
76 break;
77 }
78 }
79 free(buf);
80 __dw_unlockfile(db->fp);
82 }
83}
84
86 if(db->version == 1) {
87 __dw_lockfile(db->fp);
88 fseek(db->fp, 3 + 10, SEEK_SET);
89 int i;
90 struct dataworks_db_v1_indexentry index;
91 char* buf = malloc(1 + 8 + 1 + 256 + 4096);
92 int c = 0;
93 for(i = 0; i < 256; i++) {
94 fread(buf, 1, 1 + 8 + 1 + 256 + 4096, db->fp);
95 __dw_buffer_to_db_v1_indexentry(buf, index);
96 if(index.flag & DATAWORKS_V1_INDEXENTRY_USED) {
97 c++;
98 }
99 }
100 char** list = malloc(sizeof(*list) * (c + 1));
101 fseek(db->fp, 3 + 10, SEEK_SET);
102 c = 0;
103 for(i = 0; i < 256; i++) {
104 fread(buf, 1, 1 + 8 + 1 + 256 + 4096, db->fp);
105 __dw_buffer_to_db_v1_indexentry(buf, index);
106 if(index.flag & DATAWORKS_V1_INDEXENTRY_USED) {
107 list[c] = malloc(index.dbname_len + 1);
108 memcpy(list[c], index.dbname, index.dbname_len);
109 list[c][index.dbname_len] = 0;
110 c++;
111 }
112 }
113 list[c] = NULL;
114 free(buf);
115 __dw_unlockfile(db->fp);
116 return list;
117 } else {
118 /* Not implemented for the version */
119 return NULL;
120 }
121}
DataWorks database.
#define DATAWORKS_V1_INDEXENTRY_USED
"Used" bitmask for indexentry for v1 database.
void dataworks_database_update_mtime(struct dataworks_db *db)
Update mtime.
Definition database.c:141
char ** dataworks_database_get_table_list(struct dataworks_db *db)
Get the table list of the database.
@ DW_ERR_USED
Used already.
Definition dw_database.h:74
DataWorks utils.
indexentry for v1 database.
Database struct.
FILE * fp
File pointer to the database file.
uint16_t version
Version of the database.