Merge master.
[dcpomatic.git] / src / lib / cross.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <fstream>
21 #include <boost/algorithm/string.hpp>
22 #include "cross.h"
23 #ifdef DCPOMATIC_POSIX
24 #include <unistd.h>
25 #endif
26 #ifdef DCPOMATIC_WINDOWS
27 #include "windows.h"
28 #endif
29 #ifdef DCPOMATIC_OSX
30 #include <sys/sysctl.h>
31 #endif
32
33 using std::pair;
34 using std::ifstream;
35 using std::string;
36
37 void
38 dcpomatic_sleep (int s)
39 {
40 #ifdef DCPOMATIC_POSIX
41         sleep (s);
42 #endif
43 #ifdef DCPOMATIC_WINDOWS
44         Sleep (s * 1000);
45 #endif
46 }
47
48 /** @return A pair containing CPU model name and the number of processors */
49 pair<string, int>
50 cpu_info ()
51 {
52         pair<string, int> info;
53         info.second = 0;
54         
55 #ifdef DCPOMATIC_LINUX
56         ifstream f ("/proc/cpuinfo");
57         while (f.good ()) {
58                 string l;
59                 getline (f, l);
60                 if (boost::algorithm::starts_with (l, "model name")) {
61                         string::size_type const c = l.find (':');
62                         if (c != string::npos) {
63                                 info.first = l.substr (c + 2);
64                         }
65                 } else if (boost::algorithm::starts_with (l, "processor")) {
66                         ++info.second;
67                 }
68         }
69 #endif
70
71 #ifdef DCPOMATIC_OSX
72         size_t N = sizeof (info.second);
73         sysctlbyname ("hw.ncpu", &info.second, &N, 0, 0);
74         char buffer[64];
75         N = sizeof (buffer);
76         if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
77                 info.first = buffer;
78         }
79 #endif          
80
81         return info;
82 }
83