xref: /reactos/sdk/tools/hhpcomp/utils.cpp (revision 11345aed)
1 
2 // This file is part of hhpcomp, a free HTML Help Project (*.hhp) compiler.
3 // Copyright (C) 2015  Benedikt Freisen
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 
19 
20 #include <string>
21 #include <algorithm>
22 #include <stdexcept>
23 
24 #if defined(_WIN32)
25     #define WIN32_LEAN_AND_MEAN
26     #include <intrin.h>
27     #include <windows.h>  // for GetFullPathNameA
28 #else
29     #include <unistd.h>
30 #endif
31 
32 #include <stdlib.h>
33 
34 using namespace std;
35 
36 string to_upper(string s)
37 {
38     string temp = s;
39     transform(temp.begin(), temp.end(), temp.begin(), ::toupper);
40     return temp;
41 }
42 
43 string real_path(const char* path)
44 {
45     char* temp = NULL;
46     #if defined(_WIN32)
47         char temp2[MAX_PATH];
48         if (GetFullPathNameA(path, MAX_PATH, temp2, NULL)) {
49             temp = temp2;
50         }
51     #else
52         temp = realpath(path, NULL);
53     #endif
54     if (temp == NULL)
55         throw runtime_error("realpath failed");
56     string result(temp);
57     #if !defined(_WIN32)
58         free(temp);
59     #endif
60     return result;
61 }
62 
63 string replace_backslashes(string s)
64 {
65     string temp = s;
66     for (string::iterator it = temp.begin(); it != temp.end(); ++it)
67         if (*it == '\\')
68             *it = '/';
69     return temp;
70 }
71