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

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

sys: retrieve executable path if available.

  • Property svn:keywords set to Id
File size: 2.2 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#ifdef _WIN32
16#   define WIN32_LEAN_AND_MEAN
17#   include <direct.h>
18#endif
19
20#include "core.h"
21
22namespace lol
23{
24
25namespace System
26{
27
28#if defined _WIN32
29#   define SEPARATOR '\\'
30#else
31#   define SEPARATOR '/'
32#endif
33
34void Init(int argc, char *argv[],
35          String const projectdir, String const solutiondir)
36{
37    /*
38     * Retrieve binary directory, defaulting to current dir.
39     */
40
41#if defined _WIN32
42    char const *cwd = _getcwd(NULL, 0);
43    String binarydir = String(cwd ? cwd : ".") + SEPARATOR;
44    free((void *)cwd);
45#else
46    String binarydir = String(getcwd()) + SEPARATOR;
47#endif
48    if (argc > 0)
49    {
50        char const *last_sep = strchr(argv[0], SEPARATOR);
51        if (last_sep)
52            binarydir = String(argv[0], last_sep - argv[0] + 1);
53    }
54
55    /*
56     * Find the common prefix between project dir and solution dir.
57     */
58
59    bool got_rootdir = false;
60    for (int i = 0; ; i++)
61    {
62        if (projectdir[i] != solutiondir[i] || projectdir[i] == '\0')
63        {
64            /* FIXME: at this point we should check whether the binary
65             * was launched from this subdirectory; from now we just
66             * assume it was. */
67            if (i)
68            {
69                String rootdir = projectdir;
70                if (rootdir.Last() != SEPARATOR)
71                    rootdir += SEPARATOR;
72                SetDataDir(&rootdir[0]);
73                got_rootdir = true;
74            }
75            break;
76        }
77    }
78
79    /* If no rootdir found, use the executable location */
80    if (!got_rootdir)
81    {
82        SetDataDir(&binarydir[0]);
83        got_rootdir = true;
84    }
85}
86
87/*
88 * Data directory handling
89 */
90
91String data_dir = "";
92
93void SetDataDir(char const *dir)
94{
95    data_dir = dir;
96}
97
98char const *GetDataDir()
99{
100    return &data_dir[0];
101}
102
103
104} /* namespace System */
105
106} /* namespace lol */
107
Note: See TracBrowser for help on using the repository browser.