On win32, return 0 if installation path isn't set in the registry.
[swftools.git] / lib / os.c
1 /* os.c
2
3 operating system dependent functions
4
5 Part of the swftools package. 
6
7 Copyright (c) 2005 Matthias Kramm <kramm@quiss.org>
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
22
23 #include "os.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #ifdef WIN32
28 #include <windows.h>
29 #endif
30
31 #if defined(CYGWIN)
32 static char seperator = '/';
33 #elif defined(WIN32)
34 static char seperator = '\\';
35 #else
36 static char seperator = '/';
37 #endif
38
39 #ifdef WIN32
40 char* getRegistryEntry(char*path)
41 {
42     int res = 0;
43     HKEY key;
44     long rc;
45     long size = 0;
46     DWORD type;
47     char*buf;
48     rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_ALL_ACCESS/* KEY_READ*/, &key);
49     if (rc != ERROR_SUCCESS) {
50         fprintf(stderr, "RegOpenKeyEx failed\n");
51         return 0;
52     }
53     rc = RegQueryValueEx(key, NULL, 0, 0, 0, &size) ;
54     if(rc != ERROR_SUCCESS) {
55         fprintf(stderr, "RegQueryValueEx(1) failed: %d\n", rc);
56         return 0;
57     }
58     buf = malloc(size+1);
59     rc = RegQueryValueEx(key, NULL, 0, &type, (BYTE*)buf, &size);
60     if(rc != ERROR_SUCCESS) {
61         fprintf(stderr, "RegQueryValueEx(2) failed: %d\n", rc);
62         return 0;
63     }
64     if(type == REG_SZ || type == REG_EXPAND_SZ) {
65         while(size && buf[size-1] == '\0')
66           --size;
67         buf[size] = 0;
68         /* TODO: convert */
69         return buf;
70     } else if(type == REG_BINARY) {
71         return buf;
72     }
73 }
74
75 int setRegistryEntry(char*key,char*value)
76 {
77     HKEY hkey;
78     int ret = 0;
79     ret = RegCreateKey(HKEY_LOCAL_MACHINE, key, &hkey);
80     if(ret != ERROR_SUCCESS) {
81         fprintf(stderr, "registry: CreateKey %s failed\n", key);
82         return 0;
83     }
84     ret = RegSetValue(hkey, NULL, REG_SZ, value, strlen(value)+1);
85     if(ret != ERROR_SUCCESS) {
86         fprintf(stderr, "registry: SetValue %s failed\n", key);
87         return 0;
88     }
89     return 1;
90 }
91
92
93 #endif
94
95 //HINSTANCE me =  GetModuleHandle(NULL);
96
97 char* getInstallationPath()
98 {
99 #if defined(WIN32)
100     char* path = getRegistryEntry("Software\\quiss.org\\swftools\\InstallPath");
101     if(path)
102         return path;
103     else
104         return 0;
105 #elif defined(CYGWIN)
106     return SWFTOOLS_DATADIR;
107 #else
108     return SWFTOOLS_DATADIR;
109 #endif
110 }
111
112 char* concatPaths(char*base, char*add)
113 {
114     int l1 = strlen(base);
115     int l2 = strlen(add);
116     int pos = 0;
117     char*n = 0;
118     while(l1 && base[l1-1] == seperator)
119         l1--;
120     while(pos < l2 && add[pos] == seperator)
121         pos++;
122
123     n = malloc(l1 + (l2-pos) + 2);
124     memcpy(n,base,l1);
125     n[l1]=seperator;
126     strcpy(&n[l1+1],&add[pos]);
127     return n;
128 }
129
130 char* stripFilename(char*filename, char*newext)
131 {
132     char*last1 = strrchr(filename, '/');
133     char*last2 = strrchr(filename, '\\');
134     char*pos = filename;
135     char*name;
136     char*dot;
137     if(last1>pos) pos = last1 + 1;
138     if(last2>pos) pos = last2 + 1;
139     name = (char*)malloc(strlen(pos)+2+(newext?strlen(newext):3));
140     strcpy(name, pos);
141     dot = strrchr(name, '.');
142     if(dot) {
143         *dot = 0;
144     }
145     if(newext)
146         strcat(name, newext);
147     return name;
148 }
149