~hikiko/nux/arb-srgba-shader

« back to all changes in this revision

Viewing changes to NuxCore/FilePath.cpp

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-01 19:25:37 UTC
  • Revision ID: neil.patel@canonical.com-20100901192537-mfz7rm6q262pewg6
Import and build NuxCore

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "NKernel.h"
 
2
 
 
3
#include "FilePath.h"
 
4
 
 
5
NAMESPACE_BEGIN
 
6
 
 
7
FilePath::FilePath()
 
8
{
 
9
    if(std::find(m_SearchPath.begin(), m_SearchPath.end(), TEXT("")) != m_SearchPath.end())
 
10
        return;
 
11
    m_SearchPath.push_back(TEXT("")); // for fully qualified path
 
12
}
 
13
 
 
14
FilePath::~FilePath()
 
15
{
 
16
 
 
17
}
 
18
 
 
19
void FilePath::AddSearchPath(const NString& searchpath)
 
20
{
 
21
    if(std::find(m_SearchPath.begin(), m_SearchPath.end(), searchpath) != m_SearchPath.end())
 
22
        return;
 
23
    m_SearchPath.push_back(searchpath);
 
24
}
 
25
 
 
26
void FilePath::AddSearchPath(const std::vector<NString>& searchpath)
 
27
{
 
28
    for(t_u32 i = 0; i < searchpath.size(); i++)
 
29
    {
 
30
        if(std::find(m_SearchPath.begin(), m_SearchPath.end(), searchpath[i]) == m_SearchPath.end())
 
31
            m_SearchPath.push_back(searchpath[i]);
 
32
    }
 
33
}
 
34
 
 
35
NString FilePath::GetPathToFile(const TCHAR* filename) const
 
36
{
 
37
    NString FileName = GetFile(filename);
 
38
    
 
39
    int loc = (int)FileName.FindLastOccurence(TEXT('\\'));
 
40
    if (loc == -1)
 
41
    {
 
42
        loc = (int)FileName.FindLastOccurence(TEXT('/'));
 
43
    }
 
44
 
 
45
    if (loc != -1)
 
46
        FileName = FileName.GetSubString(0, loc);
 
47
    else
 
48
        FileName = NString(TEXT("."));
 
49
    return FileName;
 
50
}
 
51
 
 
52
NString FilePath::GetFile(const TCHAR* filename) const
 
53
{
 
54
    INL_RETURN_VALUE_IF_NULL(filename, NString(TEXT("")));
 
55
    if(filename == TEXT(""))
 
56
        return NString(TEXT(""));
 
57
 
 
58
    NString FileName = filename;
 
59
 
 
60
    if(GFileManager.FileExist(FileName.GetTCharPtr()))
 
61
        return FileName;
 
62
 
 
63
    for(t_u32 i = 0; i < m_SearchPath.size(); i++)
 
64
    {
 
65
        if(m_SearchPath[i].Size() == 0)
 
66
            continue;
 
67
 
 
68
        NString FilePath = m_SearchPath[i] + INL_PATH_SEPARATOR_STRING + filename;
 
69
        if(GFileManager.FileExist(FilePath.GetTCharPtr()))
 
70
            return FilePath;
 
71
    }
 
72
    
 
73
    // Still not found. Then peel off the root of the filename and append our custom search path.
 
74
    //      filename = "MediaProg/UI3DGraphics/MyFile.txt"
 
75
    //      search for: 
 
76
    //            CustomPath0/UI3DGraphics/MyFile.txt
 
77
    //            CustomPath1/UI3DGraphics/MyFile.txt
 
78
    //            CustomPath2/UI3DGraphics/MyFile.txt
 
79
    //            CustomPath3/UI3DGraphics/MyFile.txt
 
80
    //            CustomPath0/MyFile.txt
 
81
    //            CustomPath1/MyFile.txt
 
82
    //            CustomPath2/MyFile.txt
 
83
    //            CustomPath3/MyFile.txt
 
84
    //          
 
85
        
 
86
    FileName = filename;
 
87
        for(t_size i = 0; i < m_SearchPath.size(); i++)
 
88
        {
 
89
                t_size pos; 
 
90
        NString PathName;
 
91
                while(FileName.FindFirstOccurenceOf(TEXT("\\/")) != std::string::npos)
 
92
                {
 
93
                        pos = FileName.FindFirstOccurenceOf(TEXT("\\/")) + 1;
 
94
                        
 
95
                        FileName = FileName.GetSubString(pos, FileName.Length()- pos);
 
96
                        PathName = m_SearchPath[i] + INL_PATH_SEPARATOR_STRING + FileName;
 
97
            if(GFileManager.FileExist(PathName.GetTCharPtr()))
 
98
                return PathName;
 
99
                }
 
100
        }
 
101
 
 
102
    nuxDebugMsg(TEXT("FilePath::GetFile] Cannot find file: %s"), filename);
 
103
    return NString(TEXT(""));
 
104
}
 
105
 
 
106
NAMESPACE_END