4 Interface for perl Low IO functions
12 class CPerlLIO : public IPerlLIO
15 CPerlLIO() { w32_platform = (-1); pPerl = NULL; pSock = NULL; pStdIO = NULL; };
17 virtual int Access(const char *path, int mode, int &err);
18 virtual int Chmod(const char *filename, int pmode, int &err);
19 virtual int Chsize(int handle, long size, int &err);
20 virtual int Close(int handle, int &err);
21 virtual int Dup(int handle, int &err);
22 virtual int Dup2(int handle1, int handle2, int &err);
23 virtual int Flock(int fd, int oper, int &err);
24 virtual int FStat(int handle, struct stat *buffer, int &err);
25 virtual int IOCtl(int i, unsigned int u, char *data, int &err);
26 virtual int Isatty(int handle, int &err);
27 virtual long Lseek(int handle, long offset, int origin, int &err);
28 virtual int Lstat(const char *path, struct stat *buffer, int &err);
29 virtual char *Mktemp(char *Template, int &err);
30 virtual int Open(const char *filename, int oflag, int &err);
31 virtual int Open(const char *filename, int oflag, int pmode, int &err);
32 virtual int Read(int handle, void *buffer, unsigned int count, int &err);
33 virtual int Rename(const char *oldname, const char *newname, int &err);
34 virtual int Setmode(int handle, int mode, int &err);
35 virtual int STat(const char *path, struct stat *buffer, int &err);
36 virtual char *Tmpnam(char *string, int &err);
37 virtual int Umask(int pmode, int &err);
38 virtual int Unlink(const char *filename, int &err);
39 virtual int Utime(char *filename, struct utimbuf *times, int &err);
40 virtual int Write(int handle, const void *buffer, unsigned int count, int &err);
42 inline void SetPerlObj(CPerlObj *p) { pPerl = p; };
43 inline void SetSockCtl(CPerlSock *p) { pSock = p; };
44 inline void SetStdObj(IPerlStdIOWin *p) { pStdIO = p; };
46 inline int IsWin95(void)
48 return (os_id() == VER_PLATFORM_WIN32_WINDOWS);
50 inline int IsWinNT(void)
52 return (os_id() == VER_PLATFORM_WIN32_NT);
54 int GetOSfhandle(int filenum)
56 return pStdIO->GetOSfhandle(filenum);
60 if((-1) == w32_platform)
64 memset(&osver, 0, sizeof(OSVERSIONINFO));
65 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
67 w32_platform = osver.dwPlatformId;
69 return (w32_platform);
75 IPerlStdIOWin *pStdIO;
78 #define CALLFUNCRET(x)\
84 #define CALLFUNCERR(x)\
90 #define LCALLFUNCERR(x)\
96 int CPerlLIO::Access(const char *path, int mode, int &err)
98 CALLFUNCRET(access(path, mode))
101 int CPerlLIO::Chmod(const char *filename, int pmode, int &err)
103 CALLFUNCRET(chmod(filename, pmode))
106 int CPerlLIO::Chsize(int handle, long size, int &err)
108 CALLFUNCRET(chsize(handle, size))
111 int CPerlLIO::Close(int fd, int &err)
113 CALLFUNCRET(close(fd))
116 int CPerlLIO::Dup(int fd, int &err)
121 int CPerlLIO::Dup2(int handle1, int handle2, int &err)
123 CALLFUNCERR(dup2(handle1, handle2))
127 #define LK_ERR(f,i) ((f) ? (i = 0) : (err = GetLastError()))
128 #define LK_LEN 0xffff0000
134 int CPerlLIO::Flock(int fd, int oper, int &err)
141 croak("flock() unimplemented on this platform");
144 fh = (HANDLE)GetOSfhandle(fd);
145 memset(&o, 0, sizeof(o));
148 case LOCK_SH: /* shared lock */
149 LK_ERR(LockFileEx(fh, 0, 0, LK_LEN, 0, &o),i);
151 case LOCK_EX: /* exclusive lock */
152 LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK, 0, LK_LEN, 0, &o),i);
154 case LOCK_SH|LOCK_NB: /* non-blocking shared lock */
155 LK_ERR(LockFileEx(fh, LOCKFILE_FAIL_IMMEDIATELY, 0, LK_LEN, 0, &o),i);
157 case LOCK_EX|LOCK_NB: /* non-blocking exclusive lock */
158 LK_ERR(LockFileEx(fh,
159 LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
160 0, LK_LEN, 0, &o),i);
162 case LOCK_UN: /* unlock lock */
163 LK_ERR(UnlockFileEx(fh, 0, LK_LEN, 0, &o),i);
165 default: /* unknown */
172 int CPerlLIO::FStat(int fd, struct stat *sbufptr, int &err)
174 int ret = fstat(fd, sbufptr);
180 int CPerlLIO::IOCtl(int i, unsigned int u, char *data, int &err)
182 return pSock->IoctlSocket((SOCKET)i, (long)u, (u_long*)data, err);
185 int CPerlLIO::Isatty(int fd, int &err)
190 long CPerlLIO::Lseek(int fd, long offset, int origin, int &err)
192 LCALLFUNCERR(lseek(fd, offset, origin))
195 int CPerlLIO::Lstat(const char *path, struct stat *sbufptr, int &err)
197 return stat(path, sbufptr);
200 char *CPerlLIO::Mktemp(char *Template, int &err)
202 return mktemp(Template);
205 int CPerlLIO::Open(const char *filename, int oflag, int &err)
207 CALLFUNCERR(open(filename, oflag))
210 int CPerlLIO::Open(const char *filename, int oflag, int pmode, int &err)
212 CALLFUNCERR(open(filename, oflag, pmode))
215 int CPerlLIO::Read(int fd, void *buffer, unsigned int cnt, int &err)
217 CALLFUNCERR(read(fd, buffer, cnt))
220 int CPerlLIO::Rename(const char *OldFileName, const char *newname, int &err)
222 char szNewWorkName[MAX_PATH+1];
223 WIN32_FIND_DATA fdOldFile, fdNewFile;
227 if((strchr(OldFileName, '\\') || strchr(OldFileName, '/'))
228 && strchr(newname, '\\') == NULL
229 && strchr(newname, '/') == NULL)
231 strcpy(szNewWorkName, OldFileName);
232 if((ptr = strrchr(szNewWorkName, '\\')) == NULL)
233 ptr = strrchr(szNewWorkName, '/');
234 strcpy(++ptr, newname);
237 strcpy(szNewWorkName, newname);
239 if(stricmp(OldFileName, szNewWorkName) != 0)
240 { // check that we're not being fooled by relative paths
241 // and only delete the new file
243 // 2) it is not the same file as the old file
245 // GetFullPathName does not return the long file name on some systems
246 handle = FindFirstFile(OldFileName, &fdOldFile);
247 if(handle != INVALID_HANDLE_VALUE)
251 handle = FindFirstFile(szNewWorkName, &fdNewFile);
253 if(handle != INVALID_HANDLE_VALUE)
256 fdNewFile.cFileName[0] = '\0';
258 if(strcmp(fdOldFile.cAlternateFileName, fdNewFile.cAlternateFileName) != 0
259 && strcmp(fdOldFile.cFileName, fdNewFile.cFileName) != 0)
260 { // file exists and not same file
261 DeleteFile(szNewWorkName);
265 int ret = rename(OldFileName, szNewWorkName);
272 int CPerlLIO::Setmode(int fd, int mode, int &err)
274 CALLFUNCRET(setmode(fd, mode))
277 int CPerlLIO::STat(const char *path, struct stat *sbufptr, int &err)
279 return stat(path, sbufptr);
282 char *CPerlLIO::Tmpnam(char *string, int &err)
284 return tmpnam(string);
287 int CPerlLIO::Umask(int pmode, int &err)
292 int CPerlLIO::Unlink(const char *filename, int &err)
294 chmod(filename, _S_IREAD | _S_IWRITE);
295 CALLFUNCRET(unlink(filename))
298 int CPerlLIO::Utime(char *filename, struct utimbuf *times, int &err)
300 CALLFUNCRET(utime(filename, times))
303 int CPerlLIO::Write(int fd, const void *buffer, unsigned int cnt, int &err)
305 CALLFUNCERR(write(fd, buffer, cnt))