/[svn]/linuxsampler/trunk/src/common/Path.cpp
ViewVC logotype

Annotation of /linuxsampler/trunk/src/common/Path.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1481 - (hide annotations) (download)
Wed Nov 14 23:42:15 2007 UTC (16 years, 5 months ago) by senoner
File size: 8755 byte(s)
* win32 port work in progress:
* - implemented win32 support in the following classes:
* Thread, Condition, Mutex, Path, LscpServer
* - lscp.y use DONTCARE instead of VOID
*  (a win32 symbol defined)
* - completed win32 editor plugin loader

1 schoenebeck 1332 /***************************************************************************
2     * *
3     * Copyright (C) 2007 Christian Schoenebeck *
4     * *
5     * This library is free software; you can redistribute it and/or modify *
6     * it under the terms of the GNU General Public License as published by *
7     * the Free Software Foundation; either version 2 of the License, or *
8     * (at your option) any later version. *
9     * *
10     * This library is distributed in the hope that it will be useful, *
11     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13     * GNU General Public License for more details. *
14     * *
15     * You should have received a copy of the GNU General Public License *
16     * along with this library; if not, write to the Free Software *
17     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
18     * MA 02111-1307 USA *
19     ***************************************************************************/
20    
21     #include "Path.h"
22    
23 schoenebeck 1399 // for function hexsToNumber()
24 schoenebeck 1424 #include "global_private.h"
25 schoenebeck 1399
26 schoenebeck 1471 #include <sstream>
27    
28 schoenebeck 1332 namespace LinuxSampler {
29    
30 schoenebeck 1471 Path::Path() : drive(0) {
31     }
32    
33 schoenebeck 1332 void Path::appendNode(std::string Name) {
34     if (!Name.size()) return;
35     elements.push_back(Name);
36     }
37    
38 schoenebeck 1471 void Path::setDrive(const char& Drive) {
39     drive = Drive;
40     }
41    
42     std::string Path::toPosix() const {
43 schoenebeck 1332 // POSIX paths consist of forward slash separators and requires forward
44     // slashes in path and file names to be encoded as "%2f", the file names
45     // "." and ".." have special meanings
46     // (see http://www.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap03.html#tag_03_169
47     // and http://www.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap03.html#tag_03_266 )
48     std::string result;
49     for (int iElement = 0; iElement < elements.size(); iElement++) {
50 schoenebeck 1338 // encode percent characters
51 schoenebeck 1332 std::string e = elements[iElement];
52     for (
53 schoenebeck 1338 int pos = e.find("%"); pos != std::string::npos;
54     pos = e.find("%", pos+2)
55     ) e.replace(pos/*offset*/, 1/*length*/, "%%"/*by*/);
56     // encode forward slashes
57     for (
58 schoenebeck 1332 int pos = e.find("/"); pos != std::string::npos;
59 schoenebeck 1338 pos = e.find("/", pos+3)
60 schoenebeck 1332 ) e.replace(pos/*offset*/, 1/*length*/, "%2f"/*by*/);
61     // append encoded node to full encoded path
62     result += "/" + e;
63     }
64     if (!result.size()) result = "/";
65     return result;
66     }
67    
68 schoenebeck 1471 std::string Path::toDbPath() const {
69 iliev 1345 std::string result;
70     for (int iElement = 0; iElement < elements.size(); iElement++) {
71     // replace all slashes with '\0'
72     std::string e = elements[iElement];
73     for (int i = 0; i < e.length(); i++) {
74     if (e.at(i) == '/') e.at(i) = '\0';
75     }
76     // append encoded node to full encoded path
77     result += "/" + e;
78     }
79     if (!result.size()) result = "/";
80     return result;
81     }
82    
83 schoenebeck 1471 std::string Path::toLscp() const {
84 schoenebeck 1399 std::string result;
85     for (int iElement = 0; iElement < elements.size(); iElement++) {
86     // replace "special characters" by LSCP escape sequences
87     std::string e = elements[iElement];
88     for (int i = 0; i < e.length(); i++) {
89     const char c = e.c_str()[i];
90     if (
91     !(c >= '0' && c <= '9') &&
92     !(c >= 'a' && c <= 'z') &&
93     !(c >= 'A' && c <= 'Z') &&
94     !(c == '!') && !(c == '#') && !(c == '$') && !(c == '%') &&
95     !(c == '&') && !(c == '(') && !(c == ')') && !(c == '*') &&
96     !(c == '+') && !(c == ',') && !(c == '-') && !(c == '.') &&
97     !(c == ':') && !(c == ';') && !(c == '<') && !(c == '=') &&
98     !(c == '>') && !(c == '?') && !(c == '@') && !(c == '[') &&
99     !(c == ']') && !(c == '^') && !(c == '_') && !(c == '`') &&
100     !(c == '{') && !(c == '|') && !(c == '}') && !(c == '~')
101     ) {
102     // convert the "special" character into a "\xHH" LSCP escape sequence
103     char buf[5];
104     snprintf(buf, sizeof(buf), "\\x%02x", static_cast<unsigned char>(c));
105     e.replace(i, 1, buf);
106     i += 3;
107     }
108     }
109     // append encoded node to full encoded path
110     result += "/" + e;
111     }
112     if (!result.size()) result = "/";
113     return result;
114     }
115    
116 schoenebeck 1471 std::string Path::toWindows() const {
117     std::stringstream result;
118 senoner 1481 const char cDrive =
119     ((drive >= 'A' && drive <= 'Z') || (drive >= 'a' && drive <= 'z'))
120 schoenebeck 1471 ? drive : '?';
121 senoner 1481 result << cDrive;
122 schoenebeck 1471 result << ':';
123     for (int iElement = 0; iElement < elements.size(); iElement++) {
124     // append encoded node to full encoded path
125     result << "\\" << elements[iElement];
126     }
127     return result.str();
128     }
129    
130 schoenebeck 1332 Path Path::operator+(const Path& p) {
131     Path result = *this;
132     for (int i = 0; i < p.elements.size(); i++)
133     result.elements.push_back(p.elements[i]);
134     return result;
135     }
136    
137     Path Path::operator+(const Path* p) {
138     return *this + *p;
139     }
140    
141 schoenebeck 1399 Path Path::fromPosix(std::string path) {
142     Path result;
143     // first split the nodes
144     {
145     int nodeEnd;
146     for (
147     int nodeBegin = path.find_first_not_of('/');
148     nodeBegin != std::string::npos;
149     nodeBegin = path.find_first_not_of('/', nodeEnd)
150     ) {
151     nodeEnd = path.find_first_of('/', nodeBegin);
152     result.appendNode(
153     (nodeEnd != std::string::npos) ?
154     path.substr(nodeBegin, nodeEnd - nodeBegin) :
155     path.substr(nodeBegin)
156     );
157     }
158     }
159     // resolve POSIX escape sequences in all nodes
160     for (int iNode = 0; iNode < result.elements.size(); iNode++) {
161     std::string& s = result.elements[iNode];
162     for (int pos = s.find('%'); pos < s.length(); pos = s.find('%', ++pos)) {
163     if (pos+1 >= s.length()) { // unexpected character
164     //TODO: we might want to throw an exception here, for now we simply replace the character by '?'
165     s.replace(pos, 1, "?");
166     continue;
167     }
168     if (s.c_str()[pos+1] == '%') {
169     s.replace(pos, 2, "%");
170     continue;
171     }
172     if (pos+2 >= s.length()) {
173     //TODO: we might want to throw an exception here, for now we simply replace the character by '?'
174     s.replace(pos, 2, "?");
175     continue;
176     }
177     // expecting a "%HH" sequence here, convert it into the respective character
178     const std::string sHex = s.substr(pos+1, 2);
179     char cAscii = hexsToNumber(sHex.c_str()[1], sHex.c_str()[0]);
180     char pcAscii[] = { cAscii, 0 };
181     s.replace(pos, 3, pcAscii);
182     }
183     }
184     return result;
185     }
186    
187 iliev 1403 Path Path::fromDbPath(std::string path) {
188     Path result;
189     // first split the nodes
190     {
191     int nodeEnd;
192     for (
193     int nodeBegin = path.find_first_not_of('/');
194     nodeBegin != std::string::npos;
195     nodeBegin = path.find_first_not_of('/', nodeEnd)
196     ) {
197     nodeEnd = path.find_first_of('/', nodeBegin);
198    
199     std::string s = (nodeEnd != std::string::npos) ?
200     path.substr(nodeBegin, nodeEnd - nodeBegin) :
201     path.substr(nodeBegin);
202    
203     for (int i = 0; i < s.length(); i++) if (s.at(i) == '\0') s.at(i) = '/';
204     result.appendNode(s);
205     }
206     }
207     return result;
208     }
209    
210 senoner 1481 Path Path::fromWindows(std::string path) {
211 schoenebeck 1471 Path result;
212    
213     int nodeEnd = 0;
214    
215     // first retrieve drive
216     if (path.size() >= 2 && path.c_str()[1] == ':') {
217     result.setDrive(path.c_str()[0]);
218     nodeEnd = 2;
219     }
220    
221     // split the nodes
222     {
223     for (
224     int nodeBegin = path.find_first_not_of('\\', nodeEnd);
225     nodeBegin != std::string::npos;
226     nodeBegin = path.find_first_not_of('\\', nodeEnd)
227     ) {
228     nodeEnd = path.find_first_of('\\', nodeBegin);
229     result.appendNode(
230     (nodeEnd != std::string::npos) ?
231     path.substr(nodeBegin, nodeEnd - nodeBegin) :
232     path.substr(nodeBegin)
233     );
234     }
235     }
236    
237     return result;
238     }
239    
240 schoenebeck 1332 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC