source: trunk/packages/vizservers/nanovis/vrutil/vrFilePath.cpp @ 2798

Last change on this file since 2798 was 2798, checked in by ldelgass, 13 years ago

Add emacs mode magic line in preparation for indentation cleanup

  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2#include <vrutil/vrFilePath.h>
3
4#ifdef _WIN32
5#pragma warning  ( disable : 4996)
6#endif
7
8#include <string.h>
9#include <stdio.h>
10#include <stdlib.h>
11
12#ifdef _WIN32
13#include <direct.h>
14#endif
15
16std::string vrFilePath::_curDirectory;
17vrFilePath vrFilePath::_instance;
18
19static char seps[]   = ";";
20vrFilePath::vrFilePath()
21{
22        char buff[255];
23#ifdef WIN32
24        if (_getcwd(buff, 255) == 0)
25        {
26                printf("failed to get the current directory in vrFilePath::vrFilePath\n");
27        }
28#else
29        if (getcwd(buff, 255) == 0)
30        {
31                printf("failed to get the current directory in vrFilePath::vrFilePath\n");
32        }
33#endif
34
35        size_t length = strlen(buff);
36        buff[length] = '/';
37        buff[length + 1] = '\0';
38
39        unsigned int len = (unsigned int) strlen(buff);
40        for (unsigned int i = 0; i < len; ++i)
41        {
42                if (buff[i] == '\\') buff[i] = '/';
43        }
44        _curDirectory = buff;
45
46        char * path;
47        path = getenv("VR_ROOT_PATH");
48        if (path)
49        {
50                int len = (int) strlen(path);
51                for (int i = 0; i < len; ++i)
52                {
53                        if (buff[i] == '\\') buff[i] = '/';
54                }
55
56                strcpy(buff, path);
57                strcpy(buff + strlen(path), "/resources/");
58                _pathList.push_back(buff);
59        }
60}
61
62void vrFilePath::setPath(const std::string& filePath)
63{
64        char buff[255];
65
66        _pathList.clear();
67
68        char * path;
69        path = getenv ("VR_ROOT_PATH");
70        if (path)
71        {
72                strcpy(buff, path);
73                int len = (int) strlen(path);
74                for (int i = 0; i < len; ++i)
75                {
76                        if (buff[i] == '\\') buff[i] = '/';
77                }
78
79
80                strcpy(buff + strlen(path), "/resources/");
81                _pathList.push_back(buff);
82        }
83
84        char *token;
85        strcpy(buff, filePath.c_str());
86        token = strtok(buff, seps );
87
88        int lastIndex;
89        while( token != NULL )
90        {
91                lastIndex = (int) strlen(token) - 1;
92                //if (token[0] == '/' || token[1] == ';')
93                if (token[0] == '/' || ((lastIndex >= 1) && token[1] == ':'))
94                {
95                        if (token[lastIndex] == '/' || token[lastIndex] == '\\')
96                        {
97                                _pathList.push_back(std::string(token));
98                        }
99                        else
100                        {
101                                _pathList.push_back(std::string(token) + "/");
102                        }
103                       
104                }
105                else
106                {
107                        if (token[lastIndex] == '/' || token[lastIndex] == '\\')
108                        {
109                                _pathList.push_back(_curDirectory + std::string(token));
110                        }
111                        else
112                        {
113                                _pathList.push_back(_curDirectory + std::string(token) + "/");
114                        }
115
116                       
117                }
118                token = strtok( NULL, seps );
119        }
120}
121
122vrFilePath* vrFilePath::getInstance()
123{
124        return &_instance;
125}
126
127std::string vrFilePath::getPath(const char* fileName)
128{
129        std::string path;
130        FILE* file;
131
132        StringListIter iter;
133        for (iter = _pathList.begin(); iter != _pathList.end(); ++iter)
134        {
135#ifdef WIN32
136                if (_chdir(iter->c_str()) == -1)
137                {
138                        printf("error : change dir (%s)", iter->c_str());
139                }
140#else
141                if (chdir(iter->c_str()) == -1)
142                {
143                        printf("error : change dir (%s)", iter->c_str());
144                }
145#endif
146
147                if ((file = fopen(fileName, "rb")) != NULL)
148                {
149                        fclose(file);
150                       
151                        path = (*iter) + fileName;
152
153                        printf("returned [%s]\n", path.c_str());
154                        break;
155                }
156        }
157
158#ifdef WIN32
159        if (_chdir(_curDirectory.c_str()) != -1)
160        {
161                printf("error : change dir (%s)", _curDirectory.c_str());
162        }
163#else
164        if (chdir(_curDirectory.c_str()) != -1)
165        {
166                printf("error : change dir (%s)", _curDirectory.c_str());
167        }
168#endif
169
170        return path;
171}
172
173void vrFilePath::setWorkingDirectory(int argc, const char** argv)
174{
175    char buff[255];
176    strcpy(buff, argv[0]);
177    for (int i = (int) strlen(buff) - 1; i >= 0; --i)
178    {
179        if (buff[i] == '\\' || buff[i] == '/')
180        {
181            buff[i] = '/';
182            buff[i + 1] = '\0';
183            break;
184        }
185    }
186
187        int len = (int) strlen(buff);
188        for (int i = 0; i < len; ++i)
189        {
190                if (buff[i] == '\\') buff[i] = '/';
191        }
192        _curDirectory = buff;
193}
194
Note: See TracBrowser for help on using the repository browser.