1
0
mirror of https://github.com/fumiama/simple-dict.git synced 2026-06-11 05:30:28 +08:00

add migratenew

This commit is contained in:
fumiama
2022-02-06 14:51:49 +08:00
parent 5075c822bf
commit 14a9a02e31
2 changed files with 56 additions and 0 deletions

View File

@@ -16,11 +16,13 @@ link_directories("/usr/local/lib")
add_executable(simple-dict-server server.c dict.c crypto.c)
add_executable(simple-dict-client client.c crypto.c)
add_executable(migrate migrate.c)
add_executable(migratenew migratenew.c)
add_executable(cfgwriter cfgwriter.c)
target_link_libraries(simple-dict-server scrypto spb pthread)
target_link_libraries(simple-dict-client scrypto pthread)
target_link_libraries(migrate spb)
target_link_libraries(migratenew spb)
target_link_libraries(cfgwriter spb)
INSTALL(TARGETS simple-dict-server RUNTIME DESTINATION bin)

54
migratenew.c Normal file
View File

@@ -0,0 +1,54 @@
#include <stdio.h>
#include <string.h>
#include <simple_protobuf.h>
#define DICTKEYSZOLD 64
#define DICTDATSZOLD 64
#define DICTKEYSZNEW 127
#define DICTDATSZNEW 127
typedef struct {
char key[DICTKEYSZOLD];
char data[DICTDATSZOLD];
} DICTOLD;
typedef struct {
char key[DICTKEYSZNEW];
char data[DICTDATSZNEW];
} DICTNEW;
DICTNEW nd;
#define has_next(fp, ch) ((ch=getc(fp)),(feof(fp)?0:(ungetc(ch,fp),1)))
int main(int argc, char** argv) {
if(argc == 3) {
uint32_t* items_len_new = align_struct(sizeof(DICTNEW), 2, nd.key, nd.data);
FILE* old = fopen(argv[1], "rb");
FILE* new = fopen(argv[2], "wb");
if(old && new) {
int ch;
while(has_next(old, ch)) {
SIMPLE_PB* spb = get_pb(old);
DICTNEW* d;
switch(spb->struct_len) {
case sizeof(DICTOLD):
d = &nd;
memset(d, 0, sizeof(DICTNEW));
memcpy(nd.key, ((DICTOLD*)spb->target)->key, DICTKEYSZOLD);
memcpy(nd.data, ((DICTOLD*)spb->target)->data, DICTDATSZOLD);
break;
case sizeof(DICTNEW):
d = spb->target;
break;
default: continue; break;
}
set_pb(new, items_len_new, sizeof(DICTNEW), d);
free(spb);
}
fclose(old);
fclose(new);
} else puts("Open file error.");
} else puts("Usage: <old_dict> <new_dict>");
}