source: tuxpaint-config/trunk/fuentes/src/win32utils.cxx @ 220

Last change on this file since 220 was 220, checked in by mabarracus, 4 years ago

added tuxpaint-config sources from trusty

File size: 2.2 KB
Line 
1/* win32utils.cxx */
2/* Windows utilities Tux Paint Config */
3/* John Popplewell <john@johnnypops.demon.co.uk> */
4/* Adapted for the Cygwin environment, Make K. Kim <mkkim214@gmail.com> */
5
6#include <windows.h>
7#include "win32utils.h"
8#include <stdio.h>
9
10
11#ifdef __CYGWIN__
12  #include <sys/stat.h>
13
14  void _mkdir(const char* dir)
15  {
16    mkdir(dir, 0777);
17  }
18#else
19  #include <direct.h>
20  #define snprintf      _snprintf
21#endif
22
23extern HINSTANCE fl_display;        // hInstance on win32 platform
24
25/*
26  Read access to Windows Registry
27*/
28static HRESULT ReadRegistry(const char *key, const char *option, char *value, int size)
29{
30  LONG  res;
31  HKEY  hKey = NULL;
32
33  res = RegOpenKeyEx(HKEY_CURRENT_USER, key, 0, KEY_READ, &hKey);
34  if (res != ERROR_SUCCESS)
35    goto err_exit;
36  res = RegQueryValueEx(hKey, option, NULL, NULL, (LPBYTE)value, (LPDWORD)&size);
37  if (res != ERROR_SUCCESS)
38    goto err_exit;
39  res = ERROR_SUCCESS;
40
41err_exit:
42  if (hKey) RegCloseKey(hKey);
43  return HRESULT_FROM_WIN32(res);
44}
45
46/*
47  Removes a single '\' or '/' from end of path
48*/
49static char *remove_slash(char *path)
50{
51  int len = strlen(path);
52
53  if (!len)
54    return path;
55
56  if (path[len-1] == '/' || path[len-1] == '\\')
57    path[len-1] = 0;
58
59  return path;
60}
61
62/*
63  Returns heap string containing default application data path.
64  Creates suffix subdirectory (only one level).
65  E.g. C:\Documents and Settings\jfp\Application Data\suffix
66*/
67char *GetDefaultSaveDir(const char *suffix)
68{
69  char          prefix[MAX_PATH];
70  char          path[2*MAX_PATH];
71  const char   *key    = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders";
72  const char   *option = "AppData";
73  HRESULT hr = S_OK;
74
75  if (SUCCEEDED(hr = ReadRegistry(key, option, prefix, sizeof(prefix))))
76  {
77    remove_slash(prefix);
78    snprintf(path, sizeof(path), "%s/%s", prefix, suffix);
79    _mkdir(path);
80    return strdup(path);
81  }
82  return strdup("userdata");
83}
84
85/*
86  Returns heap string containing the application executable directory path.
87*/
88char *GetApplicationDir(void)
89{
90  char  path[2*MAX_PATH];
91  char  *p;
92
93  if (GetModuleFileName(fl_display, path, sizeof(path)))
94  {
95    if ((p = strrchr(path, '\\')) != NULL)
96    {
97        *p = 0;
98        return strdup(path);
99    }
100  }
101  return strdup("");
102}
103
Note: See TracBrowser for help on using the repository browser.