1 /*
2 Provide some simple platform abstraction, and
3 the right headers for GL stuff depending on OS
4
5 This file is a part of the RepSnapper project.
6 Copyright (C) 2010 Michael Meeks
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
17
18 You should have received a copy of the GNU Library General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "stdafx.h"
24 #include "platform.h"
25 #ifndef WIN32
26 # include <sys/time.h>
27 #endif
28 #include <gdkmm.h>
29
getTickCount()30 unsigned long Platform::getTickCount()
31 {
32 #ifdef WIN32
33 return GetTickCount();
34 #else
35 struct timeval now;
36 gettimeofday (&now, NULL);
37 return now.tv_sec * 1000 + now.tv_usec / 1000;
38 #endif
39 }
40
41 static char *binary_path = NULL;
42
setBinaryPath(const char * apparg)43 void Platform::setBinaryPath (const char *apparg)
44 {
45 const char *p;
46
47 if (!(p = strrchr (apparg, G_DIR_SEPARATOR)))
48 return;
49
50 binary_path = g_strndup (apparg, p - apparg);
51 }
52
has_extension(const std::string & fname,const char * extn)53 bool Platform::has_extension(const std::string &fname, const char *extn)
54 {
55 if (fname.find_last_of(".") == std::string::npos)
56 return false;
57 std::string this_extn = fname.substr(fname.find_last_of(".") + 1);
58 return this_extn == extn;
59 }
60
getConfigPaths()61 std::vector<std::string> Platform::getConfigPaths()
62 {
63 const gchar * const *datadirs = g_get_system_data_dirs();
64 std::vector<std::string> dirs;
65
66 /* Always prefer config files in the current directory */
67 dirs.push_back(std::string("src") + G_DIR_SEPARATOR);
68
69 /* Otherwise prefer the app's current directory */
70 if (binary_path) {
71 dirs.push_back(std::string(binary_path) + G_DIR_SEPARATOR);
72 /* Finally prefer an etc/xdg path in app's current directory */
73 #ifdef WIN32
74 dirs.push_back(std::string(binary_path) + G_DIR_SEPARATOR + ".." + G_DIR_SEPARATOR +
75 "etc" + G_DIR_SEPARATOR + "xdg" + G_DIR_SEPARATOR + "repsnapper");
76 #endif
77 }
78
79 dirs.push_back(std::string(G_STRINGIFY(RSDATADIR)) + G_DIR_SEPARATOR);
80 dirs.push_back(std::string(G_STRINGIFY(SYSCONFDIR)) + G_DIR_SEPARATOR);
81 for(gsize i = 0; datadirs[i] != NULL; ++i)
82 dirs.push_back(std::string(datadirs[i]) + G_DIR_SEPARATOR + "repsnapper" + G_DIR_SEPARATOR);
83
84 return dirs;
85 }
86
87
88
str(double r,int prec)89 std::string str(double r, int prec) {
90 ostringstream o;
91 if (prec>=0) o.precision(prec);
92 o<<r;
93 return o.str();
94 }
95