1 /*
2  * Copyright (C) 2002 2003 2004 2005 2006 2011, Magnus Hjorth
3  *
4  * This file is part of mhWaveEdit.
5  *
6  * mhWaveEdit is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * mhWaveEdit is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with mhWaveEdit; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 /* GTK wrappers around the standard I/O functions, and some other file-related
22  * stuff. */
23 
24 #ifndef GTKFILES_H_INCLUDED
25 #define GTKFILES_H_INCLUDED
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <gtk/gtk.h>
31 
32 typedef struct {
33      int fd;
34      char *filename;
35 } EFILE;
36 
37 #define EFILE_READ 0
38 #define EFILE_WRITE 1
39 #define EFILE_APPEND 2
40 
41 gboolean xunlink(char *filename);
42 
43 /* Renames oldname into newname. If oldname and newname are on
44  * different file systems, and allow_copy is TRUE, the file will be copied.
45  *
46  * Return value:
47  *  0. Success.
48  *  1. Failure. (an error message has already been displayed)
49  *  2. allow_copy is FALSE and the filenames were not on the same filesystem.
50  */
51 
52 gint xrename(char *oldname, char *newname, gboolean allow_copy);
53 
54 /* This flag is TRUE by default. If it is FALSE, e_fopen with mode!=EFILE_READ
55  * and e_fwrite will not popup error dialog boxes when an error occurs. If an
56  * error other than disk full occurs, a warning message is issued to stderr. */
57 
58 extern gboolean report_write_errors;
59 
60 int xopen(char *filename, int flags, int mode);
61 
62 /* Constants for openmode are defined above (EFILE_READ/WRITE/APPEND) */
63 EFILE *e_fopen(char *filename, int openmode);
64 
65 EFILE *e_fopen_fd(int fd, char *virtual_filename);
66 gboolean e_fclose(EFILE *stream);
67 gboolean e_fclose_remove(EFILE *stream);
68 void close_all_files(void); /* Used after forking */
69 void close_all_files_except(int *fds, int count);
70 gboolean e_fseek(EFILE *stream, off_t offset, int whence);
71 gboolean e_fread(void *data, size_t size, EFILE *stream);
72 gboolean e_fwrite(void *data, size_t size, EFILE *stream);
73 gboolean e_fwrite0(size_t size, EFILE *stream);
74 off_t e_ftell(EFILE *stream);
75 long int e_readline(gchar **line, size_t *size, EFILE *stream);
76 gboolean e_copydata(EFILE *from, EFILE *to, off_t bytes);
77 gboolean errdlg_copyfile(gchar *from, gchar *to);
78 
79 gboolean e_fread_bswap(void *data, size_t size, EFILE *stream);
80 gboolean e_fwrite_bswap(void *data, size_t size, EFILE *stream);
81 
82 gboolean is_same_file(char *filename1, char *filename2);
83 off_t errdlg_filesize(gchar *filename);
84 
85 gboolean file_exists(char *filename);
86 gboolean file_is_normal(char *filename);
87 gboolean file_is_directory(char *filename);
88 
89 gboolean program_exists(char *progname);
90 
91 /* Meant to be called from signal handlers when inside get_filename */
92 void get_filename_modify_extension(gchar *new_extension);
93 gchar *get_filename(gchar *current_name, gchar *filemask, gchar *title_text,
94 		    gboolean savemode, GtkWidget *custom_widget);
95 gchar *get_directory(gchar *current_name, gchar *title_text);
96 
97 gchar *make_filename_rooted(gchar *name);
98 
99 gboolean fd_canread(int fd);
100 gboolean fd_canwrite(int fd);
101 
102 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
103 #define e_fwrite_le(a,b,c) e_fwrite_bswap(a,b,c)
104 #define e_fread_le(a,b,c) e_fread_bswap(a,b,c)
105 #define e_fwrite_be(a,b,c) e_fwrite(a,b,c)
106 #define e_fread_be(a,b,c) e_fread(a,b,c)
107 #else
108 #define e_fwrite_le(a,b,c) e_fwrite(a,b,c)
109 #define e_fread_le(a,b,c) e_fread(a,b,c)
110 #define e_fwrite_be(a,b,c) e_fwrite_bswap(a,b,c)
111 #define e_fread_be(a,b,c) e_fread_bswap(a,b,c)
112 #endif
113 
114 #define e_fwrite_xe(a,b,c,be) ((be)?e_fwrite_be(a,b,c):e_fwrite_le(a,b,c))
115 #define e_fread_xe(a,b,c,be) ((be)?e_fread_be(a,b,c):e_fread_le(a,b,c))
116 
117 gboolean e_fread_u16_xe(guint16 *data, EFILE *stream, gboolean be);
118 gboolean e_fread_u32_xe(guint32 *data, EFILE *stream, gboolean be);
119 
120 #endif
121