source: nanovis/branches/1.1/util/FilePath.cpp @ 4923

Last change on this file since 4923 was 3465, checked in by ldelgass, 11 years ago

Rename R2 library to nv::graphics and nv::util.

  • Property svn:eol-style set to native
File size: 2.4 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *  Copyright (c) 2004-2013  HUBzero Foundation, LLC
4 */
5
6#include <string.h>
7#ifdef _WIN32
8#include <direct.h>
9#endif
10#include <stdio.h>
11#include <sys/types.h>
12#include <sys/stat.h>
13#include <unistd.h>
14
15#include "FilePath.h"
16
17#include "Trace.h"
18
19using namespace nv::util;
20
21std::string FilePath::_curDirectory;
22FilePath FilePath::_instance;
23
24static char seps[] = ":";
25
26FilePath::FilePath()
27{
28    char buff[BUFSIZ+2];
29#ifdef _WIN32
30    _getcwd(buff, sizeof(buff)-1);
31#else
32    if (getcwd(buff, sizeof(buff)-1) == NULL) {
33        ERROR("can't get current working directory\n");
34    }
35#endif
36    size_t length = strlen(buff);
37    buff[length] = '/';
38    buff[length + 1] = '\0';
39    _curDirectory = buff;
40}
41
42bool
43FilePath::setPath(const char *filePath)
44{
45    char buff[256];
46
47    if (filePath == NULL) {
48        return false;
49    }
50    _pathList.clear();
51
52    char *p;
53    strcpy(buff, filePath);
54    for (p = strtok(buff, seps); p != NULL; p = strtok(NULL, seps)) {
55        char *last;
56        struct stat buf;
57        if ((stat(p, &buf) != 0) || (!S_ISDIR(buf.st_mode))) {
58            return false;
59        }
60        last = p + strlen(p) - 1;
61        if (*p == '/') {
62            if (*last == '/' || *last == '\\') {
63                _pathList.push_back(std::string(p));
64            } else {
65                _pathList.push_back(std::string(p) + "/");
66            }
67        } else {
68            if (*last == '/' || *last == '\\') {
69                _pathList.push_back(_curDirectory + std::string(p));
70            } else {
71                _pathList.push_back(_curDirectory + std::string(p) + "/");
72            }
73        }
74    }
75    return true;
76}
77
78FilePath *
79FilePath::getInstance()
80{
81    return &_instance;
82}
83
84std::string
85FilePath::getPath(const char* fileName)
86{
87    std::string path;
88
89    for (StringListIter iter = _pathList.begin();
90         iter != _pathList.end(); ++iter) {
91        path = *iter + std::string(fileName);
92
93        if (access(path.c_str(), R_OK) == 0) {
94            return path;
95        } else {
96            path = "";
97        }
98    }
99    return path;
100}
101
102void
103FilePath::setWorkingDirectory(int argc, const char **argv)
104{
105    char buff[256];
106
107    strcpy(buff, argv[0]);
108    for (int i = strlen(buff) - 1; i >= 0; --i) {
109        if (buff[i] == '\\' || buff[i] == '/') {
110            buff[i] = '/';
111            buff[i + 1] = '\0';
112            break;
113        }
114    }
115    _curDirectory = buff;
116}
Note: See TracBrowser for help on using the repository browser.