|
| 1 | +/* -*-c++-*- |
| 2 | +Copyright (C) 2015 Runtime Revolution Ltd. |
| 3 | +
|
| 4 | +This file is part of LiveCode. |
| 5 | +
|
| 6 | +LiveCode is free software; you can redistribute it and/or modify it under |
| 7 | +the terms of the GNU General Public License v3 as published by the Free |
| 8 | +Software Foundation. |
| 9 | +
|
| 10 | +LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 | +WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | +for more details. |
| 14 | +
|
| 15 | +You should have received a copy of the GNU General Public License |
| 16 | +along with LiveCode. If not see <http://www.gnu.org/licenses/>. */ |
| 17 | + |
| 18 | +#if !defined(__MCS_SYSTEM_H_INSIDE__) |
| 19 | +# error "Only <foundation-system.h> can be included directly" |
| 20 | +#endif |
| 21 | + |
| 22 | +/* ================================================================ |
| 23 | + * Errors |
| 24 | + * ================================================================ */ |
| 25 | + |
| 26 | +MC_DLLEXPORT extern MCTypeInfoRef kMCSFileIOErrorTypeInfo; |
| 27 | +MC_DLLEXPORT extern MCTypeInfoRef kMCSFileEndOfFileErrorTypeInfo; |
| 28 | +MC_DLLEXPORT extern MCTypeInfoRef kMCSFileInvalidPathErrorTypeInfo; |
| 29 | + |
| 30 | +#ifdef __MCS_INTERNAL_API__ |
| 31 | + |
| 32 | +bool __MCSFileThrowIOErrorWithErrno (MCStringRef p_native_path, MCStringRef p_message, int p_errno); |
| 33 | +bool __MCSFileThrowReadErrorWithErrno (MCStringRef p_native_path, int p_errno); |
| 34 | +bool __MCSFileThrowWriteErrorWithErrno (MCStringRef p_native_path, int p_errno); |
| 35 | +bool __MCSFileThrowOpenErrorWithErrno (MCStringRef p_native_path, int p_errno); |
| 36 | +bool __MCSFileThrowInvalidPathError (MCStringRef p_path); |
| 37 | + |
| 38 | +#endif |
| 39 | + |
| 40 | +/* ================================================================ |
| 41 | + * Path manipulation |
| 42 | + * ================================================================ */ |
| 43 | + |
| 44 | +#ifdef __MCS_INTERNAL_API__ |
| 45 | + |
| 46 | +bool __MCSFilePathToNative (MCStringRef p_path, MCStringRef & r_native_path); |
| 47 | +bool __MCSFilePathFromNative (MCStringRef p_native_path, MCStringRef & r_path); |
| 48 | + |
| 49 | +#endif |
| 50 | + |
| 51 | +/* ================================================================ |
| 52 | + * Whole-file IO |
| 53 | + * ================================================================ */ |
| 54 | + |
| 55 | +/* Read an entire file into allocated memory, with good error checking. */ |
| 56 | +MC_DLLEXPORT bool MCSFileGetContents(MCStringRef p_filename, MCDataRef & r_data); |
| 57 | + |
| 58 | +/* Write all of p_data to a file called p_filename, with good error |
| 59 | + * checking. If a file called p_filename already exists it will be |
| 60 | + * overwritten. |
| 61 | + * |
| 62 | + * The data is first written to a temporary file which is then renamed |
| 63 | + * to the final name. On some systems, the write will therefore be |
| 64 | + * atomic in some sense. Note that: |
| 65 | + * |
| 66 | + * 1) On POSIX systems, if p_filename already exists hard links to |
| 67 | + * p_filename will break. Also, existing permissions, access |
| 68 | + * control lists, metadata etc. may be lost. If p_filename is a |
| 69 | + * symbolic link, the link itself will be replaced, not the linked |
| 70 | + * file. |
| 71 | + * |
| 72 | + * 2) On Windows it isn't possible to rename over an existing file. |
| 73 | + * There will therefore be a race condition between the existing |
| 74 | + * file being removed and the temporary file being moved into |
| 75 | + * place. |
| 76 | + * |
| 77 | + * 3) On Windows, this function will fail if p_filename already exists |
| 78 | + * and is open. |
| 79 | + */ |
| 80 | +MC_DLLEXPORT bool MCSFileSetContents(MCStringRef p_filename, MCDataRef p_data); |
| 81 | + |
| 82 | +#ifdef __MCS_INTERNAL_API__ |
| 83 | + |
| 84 | +bool __MCSFileGetContents (MCStringRef p_native_path, MCDataRef & r_data); |
| 85 | +bool __MCSFileSetContents (MCStringRef p_native_path, MCDataRef p_data); |
| 86 | + |
| 87 | +#endif |
| 88 | + |
| 89 | +/* ================================================================ |
| 90 | + * File streams |
| 91 | + * ================================================================ */ |
| 92 | + |
| 93 | +enum MCSFileOpenMode |
| 94 | +{ |
| 95 | + kMCSFileOpenModeRead = (1 << 0), |
| 96 | + kMCSFileOpenModeWrite = (1 << 1), |
| 97 | + kMCSFileOpenModeAppend = (1 << 2), |
| 98 | + kMCSFileOpenModeCreate = (1 << 3), /* Force creation of file */ |
| 99 | + |
| 100 | + kMCSFileOpenModeUpdate = (kMCSFileOpenModeRead | kMCSFileOpenModeWrite), |
| 101 | +}; |
| 102 | + |
| 103 | +MC_DLLEXPORT bool MCSFileCreateStream(MCStringRef p_filename, intenum_t p_mode, MCStreamRef& r_stream); |
| 104 | + |
| 105 | +#ifdef __MCS_INTERNAL_API__ |
| 106 | + |
| 107 | +bool __MCSFileCreateStream (MCStringRef p_native_path, intenum_t p_mode, MCStreamRef & r_stream); |
| 108 | + |
| 109 | +#endif |
| 110 | + |
| 111 | +/* ================================================================ |
| 112 | + * Filesystem operations |
| 113 | + * ================================================================ */ |
| 114 | + |
| 115 | +/* Delete the file at path. */ |
| 116 | +MC_DLLEXPORT bool MCSFileDelete (MCStringRef p_path); |
| 117 | + |
| 118 | +/* Create a directory at p_path. Does not recursively create |
| 119 | + * directories. */ |
| 120 | +MC_DLLEXPORT bool MCSFileCreateDirectory (MCStringRef p_path); |
| 121 | + |
| 122 | +/* Delete a directory at p_path. The directory must be empty. */ |
| 123 | +MC_DLLEXPORT bool MCSFileDeleteDirectory (MCStringRef p_path); |
| 124 | + |
| 125 | +/* Return a list of the entries in the directory at p_path. The |
| 126 | + * returned list never includes "." and "..". */ |
| 127 | +MC_DLLEXPORT bool MCSFileGetDirectoryEntries (MCStringRef p_path, MCProperListRef & r_entries); |
| 128 | + |
| 129 | +#ifdef __MCS_INTERNAL_API__ |
| 130 | + |
| 131 | +bool __MCSFileDelete (MCStringRef p_native_path); |
| 132 | +bool __MCSFileCreateDirectory (MCStringRef p_native_path); |
| 133 | +bool __MCSFileDeleteDirectory (MCStringRef p_native_path); |
| 134 | +bool __MCSFileGetDirectoryEntries (MCStringRef p_native_path, MCProperListRef & r_entries); |
| 135 | + |
| 136 | +#endif |
| 137 | + |
| 138 | +/* ================================================================ |
| 139 | + * File API initialization |
| 140 | + * ================================================================ */ |
| 141 | + |
| 142 | +#ifdef __MCS_INTERNAL_API__ |
| 143 | + |
| 144 | +bool __MCSFileInitialize (void); |
| 145 | +void __MCSFileFinalize (void); |
| 146 | + |
| 147 | +#endif |
0 commit comments