1 /*
2 get_fname.c
3
4 Copyright (c) 2009 - 2021
5 http://www.tuxpaint.org/
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 (See COPYING.txt)
21
22 $Id$
23 */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "get_fname.h"
30 #include "debug.h"
31 #include "compiler.h"
32
33 /*
34 See tuxpaint.c for the OS-specific defaults.
35
36 * DIR_SAVE: Where does the user's drawings get saved?
37
38 This is where their saved files (PNG) are stored, and where the
39 "current_id.txt" file is saved (so we can re-load the latest
40 picture upon a subsequent launch). Generally, end users aren't
41 expected to access the files in here directly, but they can.
42
43 The defaults may be overridden with the "--savedir" option.
44
45 * DIR_DATA: Where is the user's data directory?
46
47 This is where local (user-specific) fonts, brushes, stamps,
48 starter images, etc., can be found. End users only put things
49 here if they wish to extend their Tux Paint experience.
50
51 The defaults may be overridden with the "--datadir" option.
52
53 * DIR_EXPORT: Where does Tux Paint export drawings / animations?
54
55 This is where single images, or animated GIF slideshows,
56 will be exported. It is expected that this is an obvious,
57 and easily-accessible place for end users to retrieve the exports.
58
59 The defaults may be overridden with the "--exportdir" option.
60
61 * DIR_EXPORT_PARENT: The parent of the directory
62 specified by DIR_EXPORT. (e.g., if /home/username/Pictures/TuxPaint/
63 is our export dir., we may need to make .../Pictures first,
64 the first time we export something.)
65 */
66
67
68 const char *savedir;
69 const char *datadir;
70 const char *exportdir;
71
72 // FIXME: We shouldn't be allocating memory all the time.
73 // There should be distinct functions for each directory.
74 // There should be distinct functions for each thread,
75 // for caller-provided space, and maybe callee strdup.
76 // That's at most 4 functions per Tux Paint thread.
77
78 /**
79 * Construct a filepath, given a filename, and what kind of file
80 * (data file, or saved images?)
81 *
82 * @param name Filaneme
83 * @param kind What kind of file? (DIR_SAVE, DIR_DATA, or DIR_EXPORT?)
84 * @return Full fillpath
85 */
get_fname(const char * const name,int kind)86 char *get_fname(const char *const name, int kind)
87 {
88 char f[512];
89 // const char *restrict const dir;
90 const char * dir;
91
92 if (kind == DIR_SAVE) {
93 dir = savedir;
94 } else if (kind == DIR_DATA) {
95 dir = datadir;
96 } else if (kind == DIR_EXPORT || kind == DIR_EXPORT_PARENT) {
97 dir = exportdir;
98 }
99
100 snprintf(f, sizeof(f),
101 "%s%c%s",
102 dir, (*name) ? '/' : '\0', /* Some mkdir()'s don't like trailing slashes */
103 name);
104
105 if (kind == DIR_EXPORT_PARENT) {
106 int len, i, stop;
107
108 stop = -1;
109 len = strlen(f);
110 for (i = len - 1; i >= 0 && stop == -1; i--) {
111 if (f[i] == '/')
112 stop = i;
113 }
114 if (stop != -1) {
115 f[stop] = '\0';
116 }
117 }
118
119 return strdup(f);
120 }
121
122