Merge with git+ssh://sgc@fondialog1.hum.uva.nl/fro/git/sgc.git
[sgc.git] / distribution.c
blob9a200856e65ad2b0abab2502840329aa7ca7eab9
1 #include "sgc.h"
2 #include <zip.h>
4 void saveSGC(gchar *dir, gchar *path) {
5 if (dir != NULL && path != NULL) {
6 gchar *wordlist = g_build_path(G_DIR_SEPARATOR_S, path, "wordlist.txt", NULL);
7 // g_debug("%s", wordlist);
8 if (wordlist != NULL) {
9 if (g_access(wordlist, R_OK) == 0) {
10 gchar *filename = g_strjoin(NULL, dir, ".sgc", NULL);
11 gchar *zippath = g_build_path(G_DIR_SEPARATOR_S, DISTPATH, filename, NULL);
12 g_free(filename);
14 // g_debug("%s", zippath);
16 if (zippath != NULL) {
17 struct zip *za;
18 int err;
19 g_mkdir_with_parents(DISTPATH, 0755);
20 if ((za = zip_open(zippath, ZIP_CREATE, &err)) != NULL) {
21 GDir *dirpath;
22 if ((dirpath = g_dir_open(path, 0, NULL)) != NULL) {
23 gchar *file;
24 struct zip_source *zs;
25 while ((file = g_dir_read_name(dirpath)) != NULL) {
26 gchar *pathtofile = g_build_path(G_DIR_SEPARATOR_S, path, file, NULL);
27 if ((zs=zip_source_file(za, pathtofile, 0, -1)) != NULL) {
28 zip_add(za, file, zs);
29 } else {
30 g_debug("%s %s", pathtofile, zip_strerror(za));
32 g_free(pathtofile);
34 g_dir_close(dirpath);
36 zip_close(za);
38 g_free(zippath);
40 } else
41 g_free(wordlist);
46 gboolean openSGC(gchar *oldfilename) {
47 struct zip *za;
48 int err;
49 gboolean toreturn = FALSE;
51 if (oldfilename == NULL || (za = zip_open(oldfilename, 0, &err)) == NULL) {
52 /* Kan bestand niet openen */
53 } else {
54 if (zip_name_locate(za, "wordlist.txt", 0) == -1) {
55 /* Geen valide bestand */
56 } else {
57 int i;
58 struct zip_file *zf;
59 struct zip_stat zs;
60 gchar *data;
61 gchar *name;
62 gchar *base;
63 gchar *filename = g_path_get_basename(oldfilename);
64 int end = strlen(filename);
66 if (g_str_has_suffix(filename, ".sgc") ||
67 g_str_has_suffix(filename, ".SGC")) {
68 end -= 3;
71 name = g_malloc(end * sizeof(gchar));
73 if (name != NULL) {
75 g_strlcpy(name, filename, end);
78 base = g_build_path(G_DIR_SEPARATOR_S, WORDLISTS, name, NULL);
80 g_mkdir_with_parents(base, 0755);
82 for (i = 0; i < zip_get_num_files(za); i++) {
83 if ((zf = zip_fopen_index(za, i, 0)) != NULL &&
84 (zip_stat_index(za, i, 0, &zs)) != -1) {
85 data = g_malloc(sizeof(char) * zs.size);
87 if (data != NULL) {
88 int test;
90 if ((test = zip_fread(zf, data, zs.size)) == zs.size) {
91 gchar *dir = g_build_path(G_DIR_SEPARATOR_S, base, zs.name, NULL);
92 g_file_set_contents(dir, data, zs.size, NULL);
93 g_free(dir);
95 g_free(data);
98 zip_fclose(zf);
101 g_free(name);
102 g_free(base);
103 toreturn = TRUE;
105 g_free(filename);
107 zip_close(za);
109 return toreturn;