~ubuntu-branches/ubuntu/warty/xplanet/warty

« back to all changes in this revision

Viewing changes to src/findFile.cpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:14:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040824071400-2dr4qnjbjmm8z3ia
Tags: 1.0.6-1ubuntu1
Build-depend: libtiff4-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <cstdio>
 
2
#include <fstream>
 
3
#include <sstream>
 
4
#include <string>
 
5
#include <vector>
 
6
using namespace std;
 
7
 
 
8
#include "Options.h"
 
9
#include "xpDefines.h"
 
10
#include "xpUtil.h"
 
11
 
 
12
static bool
 
13
fileExists(string &filename)
 
14
{
 
15
    bool returnVal = false;
 
16
 
 
17
    stringstream msg;
 
18
    msg << "Looking for " << filename << " ... ";
 
19
    ifstream f(filename.c_str());
 
20
    if (f.is_open())
 
21
    {
 
22
        msg << "found\n";
 
23
        f.close();
 
24
        returnVal = true;
 
25
    }
 
26
    else
 
27
    {
 
28
        msg << "not found\n";
 
29
        returnVal = false;
 
30
    }
 
31
 
 
32
    Options *options = Options::getInstance();
 
33
    if (options->Verbosity() > 2)
 
34
        xpMsg(msg.str(), __FILE__, __LINE__);
 
35
 
 
36
    return(returnVal);
 
37
}
 
38
 
 
39
bool
 
40
findFile(string &filename, const string &subdir)
 
41
{
 
42
    // Check if the file exists in the current directory before going
 
43
    // to searchdir
 
44
    if (fileExists(filename)) return(true);
 
45
 
 
46
    string newname;
 
47
 
 
48
    Options *options = Options::getInstance();
 
49
    vector<string> searchdir = options->getSearchDir();
 
50
 
 
51
    for (int i = searchdir.size() - 1; i >= 0; i--)
 
52
    {
 
53
        // Check in searchdir itself
 
54
        newname = searchdir[i];
 
55
        newname += separator;
 
56
        newname += filename;
 
57
 
 
58
        if (fileExists(newname))
 
59
        {
 
60
            filename = newname;
 
61
            return(true);
 
62
        }
 
63
 
 
64
        // Now look in searchdir + subdir
 
65
        newname = searchdir[i];
 
66
        newname += separator;
 
67
        if (!subdir.empty())
 
68
        {
 
69
            newname += subdir;
 
70
            newname += separator;
 
71
        }
 
72
        newname += filename;
 
73
 
 
74
        if (fileExists(newname))
 
75
        {
 
76
            filename = newname;
 
77
            return(true);
 
78
        }
 
79
    }
 
80
 
 
81
    string errMsg("Can't find ");
 
82
    errMsg += filename;
 
83
    errMsg += " in\n";
 
84
    for (int i = searchdir.size() - 1; i >= 0; i--)
 
85
    {
 
86
        errMsg += searchdir[i];
 
87
        errMsg += separator;
 
88
        errMsg += subdir;
 
89
        errMsg += "\n";
 
90
    }
 
91
    xpWarn(errMsg, __FILE__, __LINE__);
 
92
    return(false);
 
93
}