Debian Codegraph @

src/gengine/Path.cpp

raw
1 /*
2 * Copyright (C) 2004 Ivo Danihelka ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9 #include "Path.h"
10
11 #include "Log.h"
12 #include "OptionAgent.h"
13 #include "Dialog.h"
14 #include "FsPath.h"
15 #include "PathException.h"
16
17 #include <stdio.h>
18
19 //-----------------------------------------------------------------
20 Path::Path(const std::string &file)
21 : m_path(file)
22 {
23 /* empty */
24 }
25 //-----------------------------------------------------------------
26 /**
27 * Try return user data path,
28 * otherwise return system data path.
29 * NOTE: OptionAgent must be initialized before this
30 *
31 * @param file path to file
32 * @param writeable whether we want write to the file
33 * @return path to user or system file
34 */
35 Path
36 Path::dataPath(const std::string &file, bool writeable)
37 {
38 Path datapath = dataUserPath(file);
39
40 if (!datapath.exists()) {
41 FILE *try_open = NULL;
42 if (writeable) {
43 try {
44 LOG_INFO(ExInfo("creating path")
45 .addInfo("path", datapath.getNative()));
46 FsPath::createPath(datapath.getPosixName());
47
48 try_open = fopen(datapath.getNative().c_str(), "wb");
49 }
50 catch (PathException &e) {
51 LOG_WARNING(e.info());
52 }
53 }
54
55 if (try_open) {
56 fclose(try_open);
57 }
58 else {
59 datapath = dataSystemPath(file);
60 }
61 }
62
63 return datapath;
64 }
65 //-----------------------------------------------------------------
66 Path
67 Path::dataReadPath(const std::string &file)
68 {
69 return dataPath(file, false);
70 }
71 //-----------------------------------------------------------------
72 Path
73 Path::dataWritePath(const std::string &file)
74 {
75 return dataPath(file, true);
76 }
77 //-----------------------------------------------------------------
78 /**
79 * Return path to system file.
80 * Path does not need to exist.
81 */
82 Path
83 Path::dataSystemPath(const std::string &file)
84 {
85 std::string systemdir = OptionAgent::agent()->getParam("systemdir");
86 return constructPath(systemdir, file);
87 }
88 //-----------------------------------------------------------------
89 /**
90 * Return path to user file.
91 * Path does not need to exist.
92 */
93 Path
94 Path::dataUserPath(const std::string &file)
95 {
96 std::string userdir = OptionAgent::agent()->getParam("userdir");
97 return constructPath(userdir, file);
98 }
99 //-----------------------------------------------------------------
100 /**
101 * Create path to the given file in the given directory.
102 * Tries to use path to a localized resource if it exists.
103 */
104 Path
105 Path::constructPath(const std::string &dir, const std::string &file)
106 {
107 std::string localized = localizePath(file);
108 Path localizedPath = Path(FsPath::join(dir, localized));
109
110 if (localized == file) {
111 return localizedPath;
112 } else if (localizedPath.exists()) {
113 LOG_INFO(ExInfo("localized")
114 .addInfo("path", localizedPath.getNative()));
115 return localizedPath;
116 } else {
117 return Path(FsPath::join(dir, file));
118 }
119 }
120 //-----------------------------------------------------------------
121 /**
122 * Return path to a localized resource or the original path.
123 * The localized path has format: <name>__<lang>.<extension>
124 *
125 * @param original original path
126 * @return localized path if it is meaningful or the original path
127 */
128 std::string
129 Path::localizePath(const std::string &original)
130 {
131 std::string::size_type dotPos = original.rfind('.');
132 if (dotPos == std::string::npos) {
133 return original;
134 }
135
136 std::string lang = OptionAgent::agent()->getParam("lang");
137 if (Dialog::DEFAULT_LANG == lang || lang.empty()) {
138 return original;
139 }
140
141 std::string appendix = "__" + lang;
142
143 std::string::size_type dirPos = original.rfind('/');
144 if (dirPos != std::string::npos && dotPos < dirPos) {
145 return original;
146 }
147
148 std::string path = original;
149 path.insert(dotPos, appendix);
150 return path;
151 }
152
153 //-----------------------------------------------------------------
154 std::string
155 Path::getNative() const
156 {
157 return FsPath::getNative(m_path);
158 }
159 //-----------------------------------------------------------------
160 bool
161 Path::exists() const
162 {
163 return FsPath::exists(m_path);
164 }