/[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 1399 by schoenebeck, Thu Oct 11 18:53:29 2007 UTC revision 1943 by persson, Tue Jul 14 18:25:11 2009 UTC
# Line 21  Line 21 
21  #include "Path.h"  #include "Path.h"
22    
23  // for function hexsToNumber()  // for function hexsToNumber()
24  #include "global.h"  #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 56  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 71  std::string Path::toDbPath() { Line 80  std::string Path::toDbPath() {
80      return result;      return result;
81  }  }
82    
83  std::string Path::toLscp() {  std::string Path::toLscp() const {
84      std::string result;      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++) {      for (int iElement = 0; iElement < elements.size(); iElement++) {
92          // replace "special characters" by LSCP escape sequences          // replace "special characters" by LSCP escape sequences
93          std::string e = elements[iElement];          std::string e = elements[iElement];
# Line 104  std::string Path::toLscp() { Line 119  std::string Path::toLscp() {
119      return result;      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        if (elements.empty()) result << '\\';
134        return result.str();
135    }
136    
137  Path Path::operator+(const Path& p) {  Path Path::operator+(const Path& p) {
138      Path result = *this;      Path result = *this;
139      for (int i = 0; i < p.elements.size(); i++)      for (int i = 0; i < p.elements.size(); i++)
# Line 161  Path Path::fromPosix(std::string path) { Line 191  Path Path::fromPosix(std::string path) {
191      return result;      return result;
192  }  }
193    
194    Path Path::fromDbPath(std::string path) {
195        Path result;
196        // first split the nodes
197        {
198            int nodeEnd;
199            for (
200                int nodeBegin = path.find_first_not_of('/');
201                nodeBegin != std::string::npos;
202                nodeBegin = path.find_first_not_of('/', nodeEnd)
203            ) {
204                nodeEnd = path.find_first_of('/', nodeBegin);
205    
206                std::string s = (nodeEnd != std::string::npos) ?
207                    path.substr(nodeBegin, nodeEnd - nodeBegin) :
208                    path.substr(nodeBegin);
209    
210                for (int i = 0; i < s.length(); i++) if (s.at(i) == '\0') s.at(i) = '/';
211                result.appendNode(s);
212            }
213        }
214        return result;
215    }
216    
217    Path Path::fromWindows(std::string path) {
218        Path result;
219    
220        int nodeEnd = 0;
221    
222        // first retrieve drive
223        if (path.size() >= 2 && path.c_str()[1] == ':') {
224            result.setDrive(path.c_str()[0]);
225            nodeEnd = 2;
226        }
227    
228        // split the nodes
229        {
230            for (
231                int nodeBegin = path.find_first_not_of('\\', nodeEnd);
232                nodeBegin != std::string::npos;
233                nodeBegin = path.find_first_not_of('\\', nodeEnd)
234            ) {
235                nodeEnd = path.find_first_of('\\', nodeBegin);
236                result.appendNode(
237                    (nodeEnd != std::string::npos) ?
238                        path.substr(nodeBegin, nodeEnd - nodeBegin) :
239                        path.substr(nodeBegin)
240                );
241            }
242        }
243    
244        return result;
245    }
246    
247    std::string Path::getName(std::string path) {
248        Path p;
249        #if WIN32
250        p.fromWindows(path);
251        #else
252        p.fromPosix(path);
253        #endif
254        
255        return p.getName();
256    }
257    
258    std::string Path::getName() {
259        if(elements.empty()) return "";
260        return elements[elements.size() - 1];
261    }
262    
263    std::string Path::getBaseName(std::string path) {
264         Path p;
265        #if WIN32
266        p = fromWindows(path);
267        #else
268        p = fromPosix(path);
269        #endif
270        
271        return p.getBaseName();
272    }
273    
274    std::string Path::getBaseName() {
275        std::string name = getName();
276        size_t lastdot = name.find_last_of('.');
277        if(lastdot == std::string::npos) return name;
278        return name.substr(0, lastdot);
279    }
280    
281  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.1399  
changed lines
  Added in v.1943

  ViewVC Help
Powered by ViewVC