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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1345 by iliev, Thu Sep 13 21:46:25 2007 UTC revision 1537 by senoner, Mon Dec 3 18:30:47 2007 UTC
# Line 20  Line 20 
20    
21  #include "Path.h"  #include "Path.h"
22    
23    // for function hexsToNumber()
24    #include "global_private.h"
25    
26    #include <sstream>
27    
28  namespace LinuxSampler {  namespace LinuxSampler {
29    
30    Path::Path() : drive(0) {
31    }
32    
33  void Path::appendNode(std::string Name) {  void Path::appendNode(std::string Name) {
34      if (!Name.size()) return;      if (!Name.size()) return;
35      elements.push_back(Name);      elements.push_back(Name);
36  }  }
37    
38  std::string Path::toPosix() {  void Path::setDrive(const char& Drive) {
39        drive = Drive;
40    }
41    
42    std::string Path::toPosix() const {
43      // POSIX paths consist of forward slash separators and requires forward      // 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      // slashes in path and file names to be encoded as "%2f", the file names
45      // "." and ".." have special meanings      // "." and ".." have special meanings
# Line 53  std::string Path::toPosix() { Line 65  std::string Path::toPosix() {
65      return result;      return result;
66  }  }
67    
68  std::string Path::toDbPath() {  std::string Path::toDbPath() const {
69      std::string result;      std::string result;
70      for (int iElement = 0; iElement < elements.size(); iElement++) {      for (int iElement = 0; iElement < elements.size(); iElement++) {
71          // replace all slashes with '\0'          // replace all slashes with '\0'
# Line 68  std::string Path::toDbPath() { Line 80  std::string Path::toDbPath() {
80      return result;      return result;
81  }  }
82    
83    std::string Path::toLscp() const {
84        std::string result;
85        #if WIN32
86        if(drive) {
87            result.assign(&drive,1);
88            result += ":";
89        }
90        #endif
91        for (int iElement = 0; iElement < elements.size(); iElement++) {
92            // replace "special characters" by LSCP escape sequences
93            std::string e = elements[iElement];
94            for (int i = 0; i < e.length(); i++) {
95                const char c = e.c_str()[i];
96                if (
97                    !(c >= '0' && c <= '9') &&
98                    !(c >= 'a' && c <= 'z') &&
99                    !(c >= 'A' && c <= 'Z') &&
100                    !(c == '!') && !(c == '#') && !(c == '$') && !(c == '%') &&
101                    !(c == '&') && !(c == '(') && !(c == ')') && !(c == '*') &&
102                    !(c == '+') && !(c == ',') && !(c == '-') && !(c == '.') &&
103                    !(c == ':') && !(c == ';') && !(c == '<') && !(c == '=') &&
104                    !(c == '>') && !(c == '?') && !(c == '@') && !(c == '[') &&
105                    !(c == ']') && !(c == '^') && !(c == '_') && !(c == '`') &&
106                    !(c == '{') && !(c == '|') && !(c == '}') && !(c == '~')
107                ) {
108                    // convert the "special" character into a "\xHH" LSCP escape sequence
109                    char buf[5];
110                    snprintf(buf, sizeof(buf), "\\x%02x", static_cast<unsigned char>(c));
111                    e.replace(i, 1, buf);
112                    i += 3;
113                }
114            }
115            // append encoded node to full encoded path
116            result += "/" + e;
117        }
118        if (!result.size()) result = "/";
119        return result;
120    }
121    
122    std::string Path::toWindows() const {
123        std::stringstream result;
124            const char cDrive =
125                ((drive >= 'A' && drive <= 'Z') || (drive >= 'a' && drive <= 'z'))
126                ? drive : '?';
127        result << cDrive;
128        result << ':';
129        for (int iElement = 0; iElement < elements.size(); iElement++) {
130            // append encoded node to full encoded path
131            result << "\\" << elements[iElement];
132        }
133        return result.str();
134    }
135    
136  Path Path::operator+(const Path& p) {  Path Path::operator+(const Path& p) {
137      Path result = *this;      Path result = *this;
138      for (int i = 0; i < p.elements.size(); i++)      for (int i = 0; i < p.elements.size(); i++)
# Line 79  Path Path::operator+(const Path* p) { Line 144  Path Path::operator+(const Path* p) {
144      return *this + *p;      return *this + *p;
145  }  }
146    
147    Path Path::fromPosix(std::string path) {
148        Path result;
149        // first split the nodes
150        {
151            int nodeEnd;
152            for (
153                int nodeBegin = path.find_first_not_of('/');
154                nodeBegin != std::string::npos;
155                nodeBegin = path.find_first_not_of('/', nodeEnd)
156            ) {
157                nodeEnd = path.find_first_of('/', nodeBegin);
158                result.appendNode(
159                    (nodeEnd != std::string::npos) ?
160                        path.substr(nodeBegin, nodeEnd - nodeBegin) :
161                        path.substr(nodeBegin)
162                );
163            }
164        }
165        // resolve POSIX escape sequences in all nodes
166        for (int iNode = 0; iNode < result.elements.size(); iNode++) {
167            std::string& s = result.elements[iNode];
168            for (int pos = s.find('%'); pos < s.length(); pos = s.find('%', ++pos)) {
169                if (pos+1 >= s.length()) { // unexpected character
170                    //TODO: we might want to throw an exception here, for now we simply replace the character by '?'
171                    s.replace(pos, 1, "?");
172                    continue;
173                }
174                if (s.c_str()[pos+1] == '%') {
175                    s.replace(pos, 2, "%");
176                    continue;
177                }
178                if (pos+2 >= s.length()) {
179                    //TODO: we might want to throw an exception here, for now we simply replace the character by '?'
180                    s.replace(pos, 2, "?");
181                    continue;
182                }
183                // expecting a "%HH" sequence here, convert it into the respective character
184                const std::string sHex = s.substr(pos+1, 2);
185                char cAscii = hexsToNumber(sHex.c_str()[1], sHex.c_str()[0]);
186                char pcAscii[] = { cAscii, 0 };
187                s.replace(pos, 3, pcAscii);
188            }
189        }
190        return result;
191    }
192    
193    Path Path::fromDbPath(std::string path) {
194        Path result;
195        // first split the nodes
196        {
197            int nodeEnd;
198            for (
199                int nodeBegin = path.find_first_not_of('/');
200                nodeBegin != std::string::npos;
201                nodeBegin = path.find_first_not_of('/', nodeEnd)
202            ) {
203                nodeEnd = path.find_first_of('/', nodeBegin);
204    
205                std::string s = (nodeEnd != std::string::npos) ?
206                    path.substr(nodeBegin, nodeEnd - nodeBegin) :
207                    path.substr(nodeBegin);
208    
209                for (int i = 0; i < s.length(); i++) if (s.at(i) == '\0') s.at(i) = '/';
210                result.appendNode(s);
211            }
212        }
213        return result;
214    }
215    
216    Path Path::fromWindows(std::string path) {
217        Path result;
218    
219        int nodeEnd = 0;
220    
221        // first retrieve drive
222        if (path.size() >= 2 && path.c_str()[1] == ':') {
223            result.setDrive(path.c_str()[0]);
224            nodeEnd = 2;
225        }
226    
227        // split the nodes
228        {
229            for (
230                int nodeBegin = path.find_first_not_of('\\', nodeEnd);
231                nodeBegin != std::string::npos;
232                nodeBegin = path.find_first_not_of('\\', nodeEnd)
233            ) {
234                nodeEnd = path.find_first_of('\\', nodeBegin);
235                result.appendNode(
236                    (nodeEnd != std::string::npos) ?
237                        path.substr(nodeBegin, nodeEnd - nodeBegin) :
238                        path.substr(nodeBegin)
239                );
240            }
241        }
242    
243        return result;
244    }
245    
246  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.1345  
changed lines
  Added in v.1537

  ViewVC Help
Powered by ViewVC