1 /* file.h                                          -*- mode:c; coding:utf-8; -*-
2  *
3  *   Copyright (c) 2010-2021  Takashi Kato <ktakashi@ymail.com>
4  *
5  *   Redistribution and use in source and binary forms, with or without
6  *   modification, are permitted provided that the following conditions
7  *   are met:
8  *
9  *   1. Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above copyright
13  *      notice, this list of conditions and the following disclaimer in the
14  *      documentation and/or other materials provided with the distribution.
15  *
16  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22  *   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  *   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24  *   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  *  $Id: $
29  */
30 #ifndef SAGITTARIUS_PRIVATE_FILE_H_
31 #define SAGITTARIUS_PRIVATE_FILE_H_
32 
33 #include "sagittariusdefs.h"
34 #include "system.h"
35 #include "clos.h"
36 
37 enum OpenMode {
38   SG_READ      = 0x00000001,
39   SG_WRITE     = 0x00000002,
40   SG_CREATE    = 0x00000010,
41   SG_TRUNCATE  = 0x00000020
42 };
43 
44 SG_CLASS_DECL(Sg_FileClass);
45 #define SG_CLASS_FILE (&Sg_FileClass)
46 
47 typedef struct SgFileTableRec
48 {
49   /* read contents */
50   int64_t (*read)(SgObject self, uint8_t *buf, int64_t size);
51   /* write buffer to file */
52   int64_t (*write)(SgObject self, uint8_t *buf, int64_t size);
53   int64_t (*seek)(SgObject self, int64_t offset, SgWhence whence); /* seek */
54   int64_t (*tell)(SgObject self);
55   int64_t (*size)(SgObject self);
56   int     (*isOpen)(SgObject self);
57   int     (*open)(SgObject self, SgString *path, int flags);
58   int     (*close)(SgObject self);
59   int     (*canClose)(SgObject self); /* for port close */
60   int     (*ready)(SgObject self);
61 } SgFileTable;
62 
63 struct SgFileRec
64 {
65   SG_HEADER;
66   void         *osdependance; /* this will be defined in os depends file */
67   const SgChar *name;	    /* file name */
68   SgFileTable  *vtbl;
69 };
70 
71 #define SG_FILEP(obj) SG_XTYPEP(obj, SG_CLASS_FILE)
72 #define SG_FILE(obj)  ((SgFile*)obj)
73 
74 #define SG_FILE_NAME(obj)   (SG_FILE(obj)->name)
75 #define SG_FILE_DATA(obj)   (SG_FILE(obj)->osdependance)
76 #define SG_FILE_VTABLE(obj) (SG_FILE(obj)->vtbl)
77 
78 enum SgGlobFlags {
79   SG_DOTMATCH = 1 << 0,
80   SG_NOESCAPE = 1 << 1,
81   SG_CASEFOLD = 1 << 2,
82   SG_PATHNAME = 1 << 3,
83 };
84 
85 enum SgFileLockType {
86   SG_SHARED    = 1U << 0,
87   SG_EXCLUSIVE = 1U << 1,
88   SG_DONT_WAIT = 1U << 2
89 };
90 
91 #define SG_OPEN_FILE(r, file, path, flags)			\
92   do {								\
93     Sg_InitFile(file);						\
94     (r) = SG_FILE_VTABLE(file)->open((file), (path), (flags));	\
95   } while (0)
96 
97 SG_CDECL_BEGIN
98 
99 SG_EXTERN SgObject Sg_MakeFile();
100 SG_EXTERN SgObject Sg_InitFile(SgFile *file);
101 SG_EXTERN SgObject Sg_MakeFileFromFD(uintptr_t handle);
102 SG_EXTERN SgObject Sg_MakeCustomFile(void *data, SgFileTable *vtbl);
103 SG_EXTERN SgObject Sg_OpenFile(SgString *path, int flags);
104 SG_EXTERN int      Sg_CloseFile(SgObject file);
105 SG_EXTERN SgObject Sg_FileErrorMessage(SgObject file);
106 SG_EXTERN int      Sg_LockFile(SgObject file, enum SgFileLockType mode);
107 SG_EXTERN int      Sg_UnlockFile(SgObject file);
108 /* wrappers */
109 SG_EXTERN int64_t  Sg_FileSeek(SgObject file, int64_t off, SgWhence whence);
110 SG_EXTERN void     Sg_FileTruncate(SgObject file, int64_t size);
111 
112 /*
113    On Windows the returning FD is HANDLE
114    (the same as Sg_MakeFileFromFD, it takes HANDLE)
115  */
116 SG_EXTERN uintptr_t Sg_FileFD(SgObject file);
117 
118 /* These methods are just creating wraps for stdout, stdin, stderr */
119 SG_EXTERN SgObject Sg_StandardOut();
120 SG_EXTERN SgObject Sg_StandardIn();
121 SG_EXTERN SgObject Sg_StandardError();
122 
123 SG_EXTERN int      Sg_IsUTF16Console(SgObject file);
124 
125 SG_EXTERN SgObject Sg_FindFile(SgString *name, SgObject loadPaths,
126 			       SgString *suffix, int quiet);
127 
128 SG_EXTERN int      Sg_FileExistP(SgString *path);
129 SG_EXTERN int      Sg_DeleteFile(SgString *path);
130 SG_EXTERN int      Sg_CopyFile(SgString *src, SgString *dst, int overwriteP);
131 /* file stat */
132 SG_EXTERN int      Sg_FileWritableP(SgString *path);
133 SG_EXTERN int      Sg_FileReadableP(SgString *path);
134 SG_EXTERN int      Sg_FileRegularP(SgString *path);
135 SG_EXTERN int      Sg_FileSymbolicLinkP(SgString *path);
136 SG_EXTERN int      Sg_FileExecutableP(SgString *path);
137 SG_EXTERN int      Sg_DirectoryP(SgString *path);
138 SG_EXTERN int      Sg_DeleteFileOrDirectory(SgString *path);
139 SG_EXTERN int      Sg_FileRename(SgString *oldpath, SgString *newpath);
140 SG_EXTERN int      Sg_ChangeFileMode(SgString *path, int mode);
141 SG_EXTERN int      Sg_CreateSymbolicLink(SgString *oldpath, SgString *newpath);
142 SG_EXTERN int      Sg_CreateDirectory(SgString *path);
143 SG_EXTERN SgObject Sg_FileModifyTime(SgString *path);
144 SG_EXTERN SgObject Sg_FileAccessTime(SgString *path);
145 SG_EXTERN SgObject Sg_FileChangeTime(SgString *path);
146 SG_EXTERN SgObject Sg_FileSize(SgString *path);
147 SG_EXTERN SgObject Sg_ReadDirectory(SgString *path);
148 SG_EXTERN SgObject Sg_CurrentDirectory();
149 SG_EXTERN void     Sg_SetCurrentDirectory(SgString *path);
150 
151 SG_EXTERN SgObject Sg_DirectoryName(SgString *path);
152 SG_EXTERN SgObject Sg_BuildPath(SgString *path, SgString *file);
153 SG_EXTERN int      Sg_AbsolutePathP(SgString *path);
154 SG_EXTERN SgObject Sg_AbsolutePath(SgString *path);
155 
156 SG_EXTERN SgObject Sg_InstalledDirectory();
157 
158 /* ACL */
159 SG_EXTERN int      Sg_CopyAccessControl(SgString *src, SgString *dst);
160 
161 /* glob */
162 SG_EXTERN SgObject Sg_Glob(SgString *path, int flags);
163 
164 SG_EXTERN int      Sg_Utimes(SgString *path, SgObject atime, SgObject mtime);
165 
166 SG_CDECL_END
167 
168 #endif /* SAGITTARIUS_FILE_HPP_ */
169 
170 /*
171   end of file
172   Local Variables:
173   coding: utf-8-unix
174   End:
175 */
176