Add newline at end of file.
[obnox/wireshark/wip.git] / wsutil / file_util.h
1 /* file_util.h
2  * File utility definitions
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifndef __FILE_UTIL_H__
26 #define __FILE_UTIL_H__
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif /* __cplusplus */
31
32 #include <glib.h>
33
34 #ifdef _WIN32
35 #include <io.h>
36 #include <gmodule.h>
37 #endif
38
39 #ifdef HAVE_SYS_STAT_H
40 #include <sys/stat.h>
41 #endif
42
43
44 /*  Win32: Since GLib2.6, we use UTF8 throughout the code, so file functions
45  *  must tweak a given filename from UTF8 to UTF16 as we use NT Unicode (Win9x
46  *  - now unsupported - used locale based encoding here).
47  */
48 #if defined _WIN32 && GLIB_CHECK_VERSION(2,6,0)
49 #include <stdio.h>
50
51 extern int ws_stdio_open (const gchar *filename, int flags, int mode);
52 extern int ws_stdio_rename (const gchar *oldfilename, const gchar *newfilename);
53 extern int ws_stdio_mkdir (const gchar *filename, int mode);
54 extern int ws_stdio_stat (const gchar *filename, struct stat *buf);
55 extern int ws_stdio_unlink (const gchar *filename);
56 extern int ws_stdio_remove (const gchar *filename);
57 extern FILE * ws_stdio_fopen (const gchar *filename, const gchar *mode);
58 extern FILE * ws_stdio_freopen (const gchar *filename, const gchar *mode, FILE *stream);
59
60 #define ws_open         ws_stdio_open
61 #define ws_rename       ws_stdio_rename
62 #define ws_mkdir        ws_stdio_mkdir
63 #define ws_stat         ws_stdio_stat
64 #define ws_unlink       ws_stdio_unlink
65 #define ws_remove       ws_stdio_remove
66 #define ws_fopen        ws_stdio_fopen
67 #define ws_freopen      ws_stdio_freopen
68
69 #else   /* _WIN32 && GLIB_CHECK_VERSION */
70
71 /* "Not Windows" or GLib < 2.6: use "old school" functions */
72 #ifdef _WIN32
73 #define ws_open         _open
74 #define ws_stat         _stat
75 #define ws_unlink       _unlink
76 #define ws_mkdir(dir,mode)      _mkdir(dir)
77 #else
78 #define ws_open         open
79 #define ws_stat         stat
80 #define ws_unlink       unlink
81 #define ws_mkdir(dir,mode)      mkdir(dir,mode)
82 #endif /* _WIN32 */
83
84 #define ws_rename       rename
85 #define ws_remove       remove
86 #define ws_fopen        fopen
87 #define ws_freopen      freopen
88
89 #endif  /* _WIN32 && GLIB_CHECK_VERSION */
90
91
92 /* some common file function differences between UNIX and WIN32 */
93 #ifdef _WIN32
94 /* the Win32 API prepends underscores for whatever reasons */
95 #define ws_read  _read
96 #define ws_write _write
97 #define ws_close _close
98 #define ws_dup   _dup
99 #define ws_lseek _lseek
100
101 /* DLL loading */
102
103 /** Try to remove the current directory from the DLL search path.
104  * SetDllDirectory is tried, then SetCurrentDirectory(program_dir)
105  *
106  * @return TRUE if we were able to call SetDllDirectory, FALSE otherwise.
107  */
108 gboolean ws_init_dll_search_path();
109
110 /** Load a DLL using LoadLibrary.
111  * Only the system and program directories are searched.
112  *
113  * @param library_name The name of the DLL.
114  * @return A handle to the DLL if found, NULL on failure.
115  */
116
117 void *ws_load_library(gchar *library_name);
118 /** Load a DLL using g_module_open.
119  * Only the system and program directories are searched.
120  *
121  * @param module_name The name of the DLL.
122  * @param flags Flags to be passed to g_module_open.
123  * @return A handle to the DLL if found, NULL on failure.
124  */
125 GModule *ws_module_open(gchar *module_name, GModuleFlags flags);
126 #else /* _WIN32 */
127 #define ws_read  read
128 #define ws_write write
129 #define ws_close close
130 #define ws_dup   dup
131 #define ws_lseek lseek
132 #define O_BINARY        0               /* Win32 needs the O_BINARY flag for open() */
133 #endif /* _WIN32 */
134
135 /* directory handling */
136 #define WS_DIR                          GDir
137 #define WS_DIRENT                       const char
138 #define ws_dir_open                     g_dir_open
139 #define ws_dir_read_name                g_dir_read_name
140 #define ws_dir_get_name(dirent) dirent
141 #define ws_dir_rewind                   g_dir_rewind
142 #define ws_dir_close                    g_dir_close
143
144 /* XXX - remove include "dirent.h" */
145 /* XXX - remove include "direct.h" */
146 /* XXX - remove include "sys/stat.h" */
147 /* XXX - update docs (e.g. README.developer) */
148
149 #ifdef __cplusplus
150 }
151 #endif /* __cplusplus */
152
153 #endif /* __FILE_UTIL_H__ */