3 operating system dependent functions
5 Part of the swftools package.
7 Copyright (c) 2005 Matthias Kramm <kramm@quiss.org>
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.
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.
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 */
33 #ifdef HAVE_SYS_STAT_H
38 #ifdef HAVE_SYS_MMAN_H
43 #ifdef HAVE_SYS_TYPES_H
44 #include <sys/types.h>
50 char path_seperator = '/';
52 char path_seperator = '\\';
54 char path_seperator = '/';
58 char* getRegistryEntry(char*path)
66 rc = RegOpenKeyEx(HKEY_CURRENT_USER, path, 0, KEY_ALL_ACCESS, &key);
68 rc = RegOpenKeyEx(HKEY_CURRENT_USER, path, 0, KEY_READ, &key);
70 rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_ALL_ACCESS, &key);
72 rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &key);
75 fprintf(stderr, "RegOpenKeyEx failed\n");
78 rc = RegQueryValueEx(key, NULL, 0, 0, 0, (LPDWORD)&size) ;
80 fprintf(stderr, "RegQueryValueEx(1) failed: %d\n", rc);
83 buf = (char*)malloc(size+1);
84 rc = RegQueryValueEx(key, NULL, 0, &type, (BYTE*)buf, (LPDWORD)&size);
86 fprintf(stderr, "RegQueryValueEx(2) failed: %d\n", rc);
89 if(type == REG_SZ || type == REG_EXPAND_SZ) {
90 while(size && buf[size-1] == '\0')
95 } else if(type == REG_BINARY) {
101 int setRegistryEntry(char*key,char*value)
105 int ret1 = 0, ret2=0;
106 ret1 = RegCreateKey(HKEY_CURRENT_USER, key, &hkey1);
107 ret2 = RegCreateKey(HKEY_LOCAL_MACHINE, key, &hkey2);
109 fprintf(stderr, "registry: CreateKey %s failed\n", key);
113 ret1 = RegSetValue(hkey1, NULL, REG_SZ, value, strlen(value)+1);
115 ret2 = RegSetValue(hkey2, NULL, REG_SZ, value, strlen(value)+1);
117 fprintf(stderr, "registry: SetValue %s failed\n", key);
126 //HINSTANCE me = GetModuleHandle(NULL);
128 char* getInstallationPath()
131 char* path = getRegistryEntry("Software\\quiss.org\\swftools\\InstallPath");
136 #elif defined(CYGWIN)
137 return SWFTOOLS_DATADIR;
139 return SWFTOOLS_DATADIR;
143 char* concatPaths(const char*base, const char*add)
145 int l1 = strlen(base);
146 int l2 = strlen(add);
149 while(l1 && base[l1-1] == path_seperator)
151 while(pos < l2 && add[pos] == path_seperator)
154 n = (char*)malloc(l1 + (l2-pos) + 2);
156 n[l1]=path_seperator;
157 strcpy(&n[l1+1],&add[pos]);
161 char* stripFilename(const char*filename, const char*newext)
163 char*last1 = strrchr(filename, '/');
164 char*last2 = strrchr(filename, '\\');
165 const char*pos = filename;
168 if(last1>pos) pos = last1 + 1;
169 if(last2>pos) pos = last2 + 1;
170 name = (char*)malloc(strlen(pos)+2+(newext?strlen(newext):3));
172 dot = strrchr(name, '.');
177 strcat(name, newext);
181 static char* getTempDir()
184 char*dir = getenv("TMP");
185 if(!dir) dir = getenv("TEMP");
186 if(!dir) dir = getenv("tmp");
187 if(!dir) dir = getenv("temp");
188 if(!dir) dir = "C:\\";
195 char* mktempname(char*ptr) {
196 static char tmpbuf[128];
197 char*dir = getTempDir();
202 if(l && dir[l-1]!='/' && dir[l-1]!='\\') {
210 // used to be mktemp. This does remove the warnings, but
211 // It's not exactly an improvement.
213 sprintf(ptr, "%s%s%08x%08x",dir,sep,(unsigned int)lrand48(),(unsigned int)lrand48());
216 sprintf(ptr, "%s%s%08x%08x",dir,sep,rand(),rand());
218 static int count = 1;
219 sprintf(ptr, "%s%s%08x%04x%04x",dir,sep,time(0),(unsigned int)tmpbuf^((unsigned int)tmpbuf)>>16,count);
226 memfile_t* memfile_open(const char*path)
228 memfile_t*file = malloc(sizeof(memfile_t));
229 #if defined(HAVE_MMAP) && defined(HAVE_STAT)
230 int fi = open(path, O_RDONLY);
237 if(fstat(fi, &sb)<0) {
241 file->len = sb.st_size;
242 file->data = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, fi, 0);
244 FILE*fi = fopen(path, "rb");
250 fseek(fi, 0, SEEK_END);
251 file->len = ftell(fi);
252 fseek(fi, 0, SEEK_SET);
253 file->data = malloc(file->len);
255 fprintf(stderr, "Out of memory while allocating memory for file %s\n", path);
259 fread(file->data, file->len, 1, fi);
265 void memfile_close(memfile_t*file)
267 #if defined(HAVE_MMAP) && defined(HAVE_STAT)
268 munmap(file->data, file->len);
277 void move_file(const char*from, const char*to)
279 int result = rename(from, to);
281 if(result==0) return; //done!
283 /* if we can't rename, for some reason, copy the file
285 FILE*fi = fopen(from, "rb");
290 FILE*fo = fopen(to, "wb");
297 int bytes = fread(buffer, 16384, 1, fi);
300 fwrite(buffer, bytes, 1, to);