Merge with git+ssh://sgc@fondialog1.hum.uva.nl/fro/git/sgc.git
[sgc.git] / distribution.c
blobc962cc12f72812f408350e0eb8f0cff8cdca45a2
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 *filename) {
47 struct zip *za;
48 int err;
49 gboolean toreturn = FALSE;
51 if (filename == NULL || (za = zip_open(filename, 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, n=0;
58 struct zip_file *zf;
59 struct zip_stat zs;
60 char *data;
61 char *name;
62 char *base;
63 int end = strlen(filename);
65 if (g_str_has_suffix(filename, ".sgc") ||
66 g_str_has_suffix(filename, ".SGC")) {
67 end -= 3;
70 name = g_malloc(end * sizeof(gchar));
72 if (name != NULL) {
74 g_strlcpy(name, filename, end);
77 base = g_build_path(G_DIR_SEPARATOR_S, WORDLISTS, name, NULL);
79 g_mkdir_with_parents(base, 0755);
81 for (i = 0; i < zip_get_num_files(za); i++) {
82 if ((zf = zip_fopen_index(za, i, 0)) != NULL &&
83 (zip_stat_index(za, i, 0, &zs)) != -1) {
84 data = g_malloc(sizeof(char) * zs.size);
86 if (data != NULL) {
87 int test;
89 if ((test = zip_fread(zf, data, zs.size)) == zs.size) {
90 gchar *dir = g_build_path(G_DIR_SEPARATOR_S, base, zs.name, NULL);
91 g_file_set_contents(dir, data, zs.size, NULL);
92 g_free(dir);
94 g_free(data);
97 zip_fclose(zf);
100 g_free(name);
101 g_free(base);
102 toreturn = TRUE;
105 zip_close(za);
107 return toreturn;