Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 123 lines (114 sloc) 3.355 kB
1eb7909 @cruppstahl unit tests no longer modify big/little endian samples
authored
1 /**
2 * Copyright (C) 2005-2007 Christoph Rupp (chris@crupp.de).
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * See file COPYING.GPL2 and COPYING.GPL3 for License information.
10 */
11
12 #ifndef OS_HPP__
13 #define OS_HPP__
14
15 #ifdef WIN32
16 # include <windows.h>
17 #else
18 # include <unistd.h>
a9c9a6a @cruppstahl now compiles with gcc 4.3.x
authored
19 # include <stdlib.h>
1eb7909 @cruppstahl unit tests no longer modify big/little endian samples
authored
20 #endif
095dce2 @cruppstahl started unittests for remote client/server
authored
21 #include <string.h>
1eb7909 @cruppstahl unit tests no longer modify big/little endian samples
authored
22
23 #include <ham/types.h>
2319df2 @cruppstahl all tests are working now
authored
24 #include <../src/error.h>
1eb7909 @cruppstahl unit tests no longer modify big/little endian samples
authored
25
26 class os
27 {
2319df2 @cruppstahl all tests are working now
authored
28 protected:
29 #ifdef WIN32
e762d76 @cruppstahl added the first unittest for the log - appending an allocated page, a…
authored
30 static const char *DisplayError(char* buf, ham_size_t buflen, DWORD errorcode)
31 {
82d7619 @cruppstahl fixes for Windows CE
authored
32 #ifdef UNDER_CE
33 strcpy(buf, "((WinCE: DisplayError() not implemented))");
34 #else
e762d76 @cruppstahl added the first unittest for the log - appending an allocated page, a…
authored
35 buf[0] = 0;
36 FormatMessageA(/* FORMAT_MESSAGE_ALLOCATE_BUFFER | */
37 FORMAT_MESSAGE_FROM_SYSTEM |
38 FORMAT_MESSAGE_IGNORE_INSERTS,
39 NULL, errorcode,
40 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
41 (LPSTR)buf, buflen, NULL);
42 buf[buflen-1]=0;
82d7619 @cruppstahl fixes for Windows CE
authored
43 #endif
e762d76 @cruppstahl added the first unittest for the log - appending an allocated page, a…
authored
44 return buf;
45 }
2319df2 @cruppstahl all tests are working now
authored
46 #endif
47
1eb7909 @cruppstahl unit tests no longer modify big/little endian samples
authored
48 public:
49 /*
50 * delete a file
51 */
2319df2 @cruppstahl all tests are working now
authored
52 static bool unlink(const char *path, bool fail_silently = true)
1eb7909 @cruppstahl unit tests no longer modify big/little endian samples
authored
53 {
54 #ifdef WIN32
82d7619 @cruppstahl fixes for Windows CE
authored
55 # ifdef UNDER_CE
e762d76 @cruppstahl added the first unittest for the log - appending an allocated page, a…
authored
56 wchar_t wpath[1024];
57 MultiByteToWideChar(CP_ACP, 0, path, -1, wpath,
82d7619 @cruppstahl fixes for Windows CE
authored
58 sizeof(wpath)/sizeof(wchar_t));
59 BOOL rv = DeleteFileW(wpath);
60 # else
2319df2 @cruppstahl all tests are working now
authored
61 BOOL rv = DeleteFileA((LPCSTR)path);
82d7619 @cruppstahl fixes for Windows CE
authored
62 # endif
e762d76 @cruppstahl added the first unittest for the log - appending an allocated page, a…
authored
63 if (!rv) {
64 if (!fail_silently) {
65 char buf[1024];
66 char buf2[1024];
67 DWORD st;
68 st = GetLastError();
69 _snprintf(buf2, sizeof(buf2),
70 "DeleteFileA('%s') failed with OS status %u (%s)",
71 path, st, DisplayError(buf, sizeof(buf), st));
72 buf2[sizeof(buf2)-1] = 0;
73 ham_log(("%s", buf2));
74 }
75 return false;
76 }
77 return true;
1eb7909 @cruppstahl unit tests no longer modify big/little endian samples
authored
78 #else
79 return (0==::unlink(path));
80 #endif
81 }
82
83 /*
84 * copy a file
85 */
e762d76 @cruppstahl added the first unittest for the log - appending an allocated page, a…
authored
86 static bool copy(const char *src, const char *dest) {
1eb7909 @cruppstahl unit tests no longer modify big/little endian samples
authored
87 #ifdef WIN32
82d7619 @cruppstahl fixes for Windows CE
authored
88 # ifdef UNDER_CE
e762d76 @cruppstahl added the first unittest for the log - appending an allocated page, a…
authored
89 wchar_t wsrc[1024];
82d7619 @cruppstahl fixes for Windows CE
authored
90 wchar_t wdest[1024];
e762d76 @cruppstahl added the first unittest for the log - appending an allocated page, a…
authored
91 MultiByteToWideChar(CP_ACP, 0, src, -1, wsrc,
82d7619 @cruppstahl fixes for Windows CE
authored
92 sizeof(wsrc)/sizeof(wchar_t));
e762d76 @cruppstahl added the first unittest for the log - appending an allocated page, a…
authored
93 MultiByteToWideChar(CP_ACP, 0, dest, -1, wdest,
82d7619 @cruppstahl fixes for Windows CE
authored
94 sizeof(wdest)/sizeof(wchar_t));
95 BOOL rv = CopyFileW(wsrc, wdest, FALSE);
96 # else
2319df2 @cruppstahl all tests are working now
authored
97 BOOL rv = CopyFileA((LPCSTR)src, dest, FALSE);
82d7619 @cruppstahl fixes for Windows CE
authored
98 # endif
e762d76 @cruppstahl added the first unittest for the log - appending an allocated page, a…
authored
99 if (!rv) {
100 char buf[2048];
101 char buf2[1024];
102 DWORD st;
103 st = GetLastError();
104 _snprintf(buf2, sizeof(buf2),
105 "CopyFileA('%s', '%s') failed with OS status %u (%s)",
106 src, dest, st, DisplayError(buf, sizeof(buf), st));
107 buf2[sizeof(buf2)-1] = 0;
108 ham_log(("%s", buf2));
109 return false;
110 }
111 return true;
1eb7909 @cruppstahl unit tests no longer modify big/little endian samples
authored
112 #else
113 char buffer[1024*4];
114
1471857 @cruppstahl removed blob_user_size and blob_real_size; there's only one size now
authored
115 snprintf(buffer, sizeof(buffer), "\\cp %s %s && chmod 644 %s",
116 src, dest, dest);
1eb7909 @cruppstahl unit tests no longer modify big/little endian samples
authored
117 return (0==system(buffer));
118 #endif
119 }
120 };
121
122 #endif /* OS_HPP__ */
Something went wrong with that request. Please try again.