source: trunk/src/sys/init.cpp @ 2403

Last change on this file since 2403 was 2403, checked in by sam, 7 years ago

sys: add the base source directory to the list of search directories
on automake platforms, too.

  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://www.wtfpl.net/ for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#if defined HAVE_UNISTD_H
16#   include <unistd.h>
17#endif
18
19#ifdef _WIN32
20#   define WIN32_LEAN_AND_MEAN
21#   include <direct.h>
22#endif
23
24#include "core.h"
25
26namespace lol
27{
28
29namespace System
30{
31
32#if defined _WIN32
33#   define SEPARATOR '\\'
34#else
35#   define SEPARATOR '/'
36#endif
37
38static Array<String> data_dir;
39
40void Init(int argc, char *argv[],
41          String const &projectdir,
42          String const &solutiondir,
43          String const &sourcesubdir)
44{
45    using namespace std;
46
47    /*
48     * Retrieve binary directory, defaulting to current dir.
49     */
50
51#if defined HAVE_GETCWD
52    char *cwd = getcwd(NULL, 0);
53#elif defined HAVE__GETCWD || (defined _WIN32 && !defined _XBOX)
54    char *cwd = _getcwd(NULL, 0);
55#else
56    char *cwd = NULL;
57#endif
58    String binarydir = String(cwd ? cwd : ".") + SEPARATOR;
59    free(cwd);
60    if (argc > 0)
61    {
62        char const *last_sep = strrchr(argv[0], SEPARATOR);
63        if (last_sep)
64            binarydir = String(argv[0], last_sep - argv[0] + 1);
65    }
66
67    /*
68     * Find the common prefix between project dir and solution dir.
69     */
70
71    bool got_rootdir = false;
72    for (int i = 0; ; i++)
73    {
74        if (projectdir[i] != solutiondir[i] || projectdir[i] == '\0')
75        {
76            /* FIXME: at this point we should check whether the binary
77             * was launched from this subdirectory; from now we just
78             * assume it was. */
79            if (i)
80            {
81                String rootdir = solutiondir;
82                if (rootdir.Last() != SEPARATOR)
83                    rootdir += SEPARATOR;
84                rootdir += "../../src/"; /* FIXME: use SEPARATOR? */
85                AddDataDir(rootdir);
86
87                rootdir = projectdir;
88                if (rootdir.Last() != SEPARATOR)
89                    rootdir += SEPARATOR;
90                AddDataDir(rootdir);
91
92                got_rootdir = true;
93            }
94            break;
95        }
96    }
97
98    /* If no rootdir found, use the executable location */
99    if (!got_rootdir)
100    {
101        String rootdir = binarydir;
102        if (rootdir.Last() != SEPARATOR)
103            rootdir += SEPARATOR;
104        for (int i = 1; i < sourcesubdir.Count(); ++i)
105        {
106            if ((sourcesubdir[i] == SEPARATOR
107                  && sourcesubdir[i - 1] != SEPARATOR)
108                 || i == sourcesubdir.Count() - 1)
109                rootdir += "../";
110        }
111        rootdir += "src/";
112        AddDataDir(rootdir);
113
114        rootdir = binarydir;
115        AddDataDir(rootdir);
116
117        got_rootdir = true;
118    }
119
120    Log::Debug("binary dir: %s\n", binarydir.C());
121    for (int i = 0; i < data_dir.Count(); ++i)
122        Log::Debug("data dir %d/%d: %s\n", i + 1, data_dir.Count(),
123                   data_dir[i].C());
124}
125
126/*
127 * Data directory handling
128 */
129
130void AddDataDir(String const &dir)
131{
132    data_dir << dir;
133}
134
135Array<String> GetPathList(String const &file)
136{
137    Array<String> ret;
138
139    for (int i = 0; i < data_dir.Count(); ++i)
140        ret << data_dir[i] + file;
141
142    if (ret.Count() == 0)
143        ret << file;
144
145    return ret;
146}
147
148} /* namespace System */
149
150} /* namespace lol */
151
Note: See TracBrowser for help on using the repository browser.