first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / library / basis / environment.cpp
1 //////////////
2 // Name   : environment
3 // Author : Chris Koeritz
4 //////////////
5 // Copyright (c) 1994-$now By Author.  This program is free software; you can
6 // redistribute it and/or modify it under the terms of the GNU General Public
7 // License as published by the Free Software Foundation:
8 //     http://www.gnu.org/licenses/gpl.html
9 // or under the terms of the GNU Library license:
10 //     http://www.gnu.org/licenses/lgpl.html
11 // at your preference.  Those licenses describe your legal rights to this
12 // software, and no other rights or warranties apply.
13 // Please send updates for this code to: fred@gruntose.com -- Thanks, fred.
14 //////////////
15
16 #include "environment.h"
17
18 #include <stdlib.h>
19 #include <sys/types.h>
20 #ifdef __UNIX__
21   #include <unistd.h>
22   #include <sys/times.h>
23 #endif
24 #ifdef __WIN32__
25   #define _WINSOCKAPI_  // make windows.h happy about winsock.
26   #include <windows.h>
27   #include <mmsystem.h>
28 #endif
29
30 namespace basis {
31
32 astring environment::get(const astring &variable_name)
33 {
34 #ifdef __WIN32__
35   char *value = getenv(variable_name.upper().observe());
36     // dos & os/2 require upper case for the name, so we just do it that way.
37 #else
38   char *value = getenv(variable_name.observe());
39     // reasonable OSes support mixed-case environment variables.
40 #endif
41   astring to_return;
42   if (value)
43     to_return = astring(value);
44   return to_return;
45 }
46
47 bool environment::set(const astring &variable_name, const astring &value)
48 {
49   int ret = 0;
50 #ifdef __WIN32__
51   astring assignment = variable_name + "=" + value;
52   ret = _putenv(assignment.s());
53 #else
54   ret = setenv(variable_name.s(), value.s(), true);
55 #endif
56   return !ret;
57 }
58
59 basis::un_int environment::system_uptime()
60 {
61 #ifdef __WIN32__
62   return timeGetTime();
63 #else
64   static clock_t __ctps = sysconf(_SC_CLK_TCK);  // clock ticks per second.
65   static const double __multiplier = 1000.0 / double(__ctps);
66     // the multiplier gives us our full range for the tick counter.
67
68   // read uptime info from the OS.
69   tms uptime;
70   basis::un_int real_ticks = times(&uptime);
71
72   // now turn this into the number of milliseconds.
73   double ticks_up = (double)real_ticks;
74   ticks_up = ticks_up * __multiplier;  // convert to time here.
75
76   // we use the previous version of this calculation, which expected a basis::u_int
77   // to double conversion to provide a modulo operation rather than just leaving
78   // the basis::un_int at its maximum value (2^32-1).  however, that expectation is not
79   // guaranteed on some platforms (e.g., ARM processor with floating point
80   // emulation) and thus it becomes a bug around 49 days and 17 hours into
81   // OS uptime because the value gets stuck at 2^32-1 and never rolls over.
82   return basis::un_int(ticks_up);
83 #endif
84 }
85
86 } //namespace.
87