summaryrefslogtreecommitdiffstats
path: root/debian/uncrustify-trinity/uncrustify-trinity-0.78.0/src/compat_win32.cpp
blob: ff79199dd2f23b3d6c5c17eab61bc629c97827f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
 * @file compat_win32.cpp
 * Compatibility functions for win32
 *
 * @author  Ben Gardner
 * @license GPL v2+
 */

#if defined (_WIN32) \
   && !defined (__CYGWIN__)

#include "windows_compat.h"

#include <cstdio>
#include <string>


bool unc_getenv(const char *name, std::string &str)
{
   DWORD len = GetEnvironmentVariableA(name, NULL, 0);
   char  *buf;

   if (len == 0)
   {
      if (GetLastError() == ERROR_ENVVAR_NOT_FOUND)
      {
         return(false);
      }
   }
   buf = (char *)malloc(len);

   if (buf != nullptr)
   {
      len = GetEnvironmentVariableA(name, buf, len);
   }
   buf[len] = 0;

   str = buf;
   //printf("%s: name=%s len=%zu value=%s\n", __func__, name, len, str.c_str());
   free(buf);

   return(true);
}


bool unc_homedir(std::string &home)
{
   if (unc_getenv("HOME", home))
   {
      return(true);
   }

   if (unc_getenv("USERPROFILE", home))
   {
      return(true);
   }
   std::string hd, hp;

   if (  unc_getenv("HOMEDRIVE", hd)
      && unc_getenv("HOMEPATH", hp))
   {
      home = hd + hp;
      return(true);
   }
   return(false);
}


void convert_log_zu2lu(char *fmt)
{
   for (size_t i = 0; i < strlen(fmt); i++)
   {
      if (  (fmt[i] == '%')
         && (fmt[i + 1] == 'z')
         && (fmt[i + 2] == 'u'))
      {
         fmt[i + 1] = 'l';
      }
   }
}

#endif /* if defined(_WIN32) && !defined(__CYGWIN__) */