small problem with windows, hope this fixes it
[sgc.git] / distribution.c
blob234d5ceb9ad9daa3ecdccf097442234599058e7a
1 #include "sgc.h"
2 #include <zip.h>
4 void saveSGC(const 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 *path = g_build_path(G_DIR_SEPARATOR_S, g_getenv(STORAGE), STOREAS, DISTPATH, NULL);
12 if (path != NULL) {
13 gchar *zippath = g_build_path(G_DIR_SEPARATOR_S, path, filename, NULL);
15 // g_debug("%s", zippath);
17 if (zippath != NULL) {
18 struct zip *za;
19 int err;
20 g_mkdir_with_parents(path, 0755);
21 if ((za = zip_open(zippath, ZIP_CREATE, &err)) != NULL) {
22 GDir *dirpath;
23 if ((dirpath = g_dir_open(path, 0, NULL)) != NULL) {
24 const gchar *file;
25 struct zip_source *zs;
26 while ((file = g_dir_read_name(dirpath)) != NULL) {
27 gchar *pathtofile = g_build_path(G_DIR_SEPARATOR_S, path, file, NULL);
28 if ((zs=zip_source_file(za, pathtofile, 0, -1)) != NULL) {
29 zip_add(za, file, zs);
30 } else {
31 g_debug("%s %s", pathtofile, zip_strerror(za));
33 g_free(pathtofile);
35 g_dir_close(dirpath);
37 zip_close(za);
39 g_free(zippath);
41 g_free(path);
43 g_free(filename);
44 } else
45 g_free(wordlist);
50 gboolean openSGC(gchar *oldfilename) {
51 struct zip *za;
52 int err;
53 gboolean toreturn = FALSE;
55 if (oldfilename == NULL || (za = zip_open(oldfilename, 0, &err)) == NULL) {
56 /* Kan bestand niet openen */
57 } else {
58 if (zip_name_locate(za, "wordlist.txt", 0) == -1) {
59 /* Geen valide bestand */
60 } else {
61 int i;
62 struct zip_file *zf;
63 struct zip_stat zs;
64 gchar *data;
65 gchar *name;
66 gchar *base;
67 gchar *filename = g_path_get_basename(oldfilename);
68 int end = strlen(filename);
70 if (g_str_has_suffix(filename, ".sgc") ||
71 g_str_has_suffix(filename, ".SGC")) {
72 end -= 3;
75 name = g_malloc(end * sizeof(gchar));
77 if (name != NULL) {
79 g_strlcpy(name, filename, end);
82 base = g_build_path(G_DIR_SEPARATOR_S, g_getenv(STORAGE), STOREAS, WORDLISTS, name, NULL);
84 g_mkdir_with_parents(base, 0755);
86 for (i = 0; i < zip_get_num_files(za); i++) {
87 if ((zf = zip_fopen_index(za, i, 0)) != NULL &&
88 (zip_stat_index(za, i, 0, &zs)) != -1) {
89 data = g_malloc(sizeof(char) * zs.size);
91 if (data != NULL) {
92 int test;
94 if ((test = zip_fread(zf, data, zs.size)) == zs.size) {
95 gchar *dir = g_build_path(G_DIR_SEPARATOR_S, base, zs.name, NULL);
96 g_file_set_contents(dir, data, zs.size, NULL);
97 g_free(dir);
99 g_free(data);
102 zip_fclose(zf);
105 g_free(name);
106 g_free(base);
107 toreturn = TRUE;
109 g_free(filename);
111 zip_close(za);
113 return toreturn;