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
, const char*ext
) {
196 static char tmpbuf
[160];
197 char*dir
= getTempDir();
202 if(l
&& dir
[l
-1]!='/' && dir
[l
-1]!='\\') {
211 unsigned int r1
= (unsigned int)lrand48();
212 unsigned int r2
= (unsigned int)lrand48();
214 unsigned int r1
= rand();
215 unsigned int r2
= rand();
217 static int count
= 1;
218 unsigned int r1
= time(0);
219 unsigned int r2
= (unsigned int)tmpbuf
<<8^count
;
223 sprintf(ptr
, "%s%s%04x%04x.%s",dir
,sep
,r1
,r2
,ext
);
225 sprintf(ptr
, "%s%s%04x%04x",dir
,sep
,r1
,r2
);
230 memfile_t
* memfile_open(const char*path
)
232 memfile_t
*file
= malloc(sizeof(memfile_t
));
233 #if defined(HAVE_MMAP) && defined(HAVE_STAT)
234 int fi
= open(path
, O_RDONLY
);
241 if(fstat(fi
, &sb
)<0) {
245 file
->len
= sb
.st_size
;
246 file
->data
= mmap(0, sb
.st_size
, PROT_READ
, MAP_PRIVATE
, fi
, 0);
248 FILE*fi
= fopen(path
, "rb");
254 fseek(fi
, 0, SEEK_END
);
255 file
->len
= ftell(fi
);
256 fseek(fi
, 0, SEEK_SET
);
257 file
->data
= malloc(file
->len
);
259 fprintf(stderr
, "Out of memory while allocating memory for file %s\n", path
);
263 fread(file
->data
, file
->len
, 1, fi
);
269 void memfile_close(memfile_t
*file
)
271 #if defined(HAVE_MMAP) && defined(HAVE_STAT)
272 munmap(file
->data
, file
->len
);
281 void move_file(const char*from
, const char*to
)
283 int result
= rename(from
, to
);
285 if(result
==0) return; //done!
287 /* if we can't rename, for some reason, copy the file
289 FILE*fi
= fopen(from
, "rb");
294 FILE*fo
= fopen(to
, "wb");
301 int bytes
= fread(buffer
, 16384, 1, fi
);
304 fwrite(buffer
, bytes
, 1, fo
);