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

Last change on this file since 2358 was 2358, checked in by sam, 6 years ago

sys: create an empty File class.

  • Property svn:keywords set to Id
File size: 2.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
38void Init(int argc, char *argv[],
39          String const &projectdir, String const &solutiondir)
40{
41    using namespace std;
42
43    /*
44     * Retrieve binary directory, defaulting to current dir.
45     */
46
47#if defined HAVE_GETCWD
48    char *cwd = getcwd(NULL, 0);
49#elif defined HAVE__GETCWD || (defined _WIN32 && !defined _XBOX)
50    char *cwd = _getcwd(NULL, 0);
51#else
52    char *cwd = NULL;
53#endif
54    String binarydir = String(cwd ? cwd : ".") + SEPARATOR;
55    free(cwd);
56    if (argc > 0)
57    {
58        char const *last_sep = strrchr(argv[0], SEPARATOR);
59        if (last_sep)
60            binarydir = String(argv[0], last_sep - argv[0] + 1);
61    }
62
63    /*
64     * Find the common prefix between project dir and solution dir.
65     */
66
67    bool got_rootdir = false;
68    for (int i = 0; ; i++)
69    {
70        if (projectdir[i] != solutiondir[i] || projectdir[i] == '\0')
71        {
72            /* FIXME: at this point we should check whether the binary
73             * was launched from this subdirectory; from now we just
74             * assume it was. */
75            if (i)
76            {
77                String rootdir = projectdir;
78                if (rootdir.Last() != SEPARATOR)
79                    rootdir += SEPARATOR;
80                SetDataDir(&rootdir[0]);
81                got_rootdir = true;
82            }
83            break;
84        }
85    }
86
87    /* If no rootdir found, use the executable location */
88    if (!got_rootdir)
89    {
90        SetDataDir(&binarydir[0]);
91        got_rootdir = true;
92    }
93
94    Log::Debug("binary dir: %s\n", &binarydir[0]);
95    Log::Debug("root dir: %s\n", &GetDataDir()[0]);
96}
97
98/*
99 * Data directory handling
100 */
101
102String data_dir = "";
103
104void SetDataDir(String const &dir)
105{
106    data_dir = dir;
107}
108
109String const &GetDataDir()
110{
111    return data_dir;
112}
113
114} /* namespace System */
115
116} /* namespace lol */
117
Note: See TracBrowser for help on using the repository browser.