-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtbo-files.c
118 lines (101 loc) · 2.71 KB
/
tbo-files.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* This file is part of TBO, a gnome comic editor
* Copyright (C) 2010 Daniel Garcia Moreno <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tbo-files.h"
#include <glib.h>
char **tbo_files_get_dirs ()
{
// Possible doodle dirs
char **possible_dirs = malloc (4*sizeof(char*));
possible_dirs[0] = malloc (255*sizeof(char*));
possible_dirs[1] = malloc (255*sizeof(char*));
possible_dirs[2] = malloc (255*sizeof(char*));
possible_dirs[3] = NULL;
strcat (strcpy (possible_dirs[0], getenv("HOME")), "/.tbo/doodle");
strcat (strcpy (possible_dirs[1], g_get_user_data_dir ()), "/tbo/doodle");
strcpy (possible_dirs[2], DATA_DIR "/doodle");
return possible_dirs;
}
int
tbo_files_prefix_len (char *str)
{
int n, i = 0;
char **possible_dirs = tbo_files_get_dirs ();
while (possible_dirs[i])
{
if (g_str_has_prefix (str, possible_dirs[i]))
{
n = strlen (possible_dirs[i]) + 1;
break;
}
i++;
}
tbo_files_free (possible_dirs);
return n;
}
void
tbo_files_free (char **files)
{
int i = 0;
while(files[i])
{
free (files[i]);
i++;
}
free (files);
}
void
tbo_files_expand_path (char *source, char *dest)
{
int st, i = 0;
char **possible_dirs = tbo_files_get_dirs ();
struct stat filestat;
while (possible_dirs[i])
{
snprintf (dest, 255, "%s/%s", possible_dirs[i], source);
st = stat (dest, &filestat);
if (!st)
break;
else
snprintf (dest, 255, "%s", source);
i++;
}
tbo_files_free (possible_dirs);
}
gboolean
tbo_files_is_svg_file (char *source)
{
gchar **paths;
gchar **ext;
gchar *lower_ext;
gboolean is_svg = FALSE;
paths = g_strsplit (source, ".", 0);
ext = paths;
while (*ext) ext++;
ext--;
lower_ext = g_ascii_strdown (*ext, -1);
if (strcmp (lower_ext, "svg") == 0) {
is_svg = TRUE;
}
g_strfreev (paths);
g_free (lower_ext);
return is_svg;
}