src/SysDeps/posix.cpp

説明を見る。
00001 // 
00002 // Copyright (c) 2005-2008 Kenichi Watanabe.
00003 // Copyright (c) 2005-2008 Yasuhiro Watari.
00004 // Copyright (c) 2005-2008 Hironori Ichibayashi.
00005 // Copyright (c) 2008-2009 Kazuo Horio.
00006 // Copyright (c) 2009-2013 Naruki Kurata.
00007 // Copyright (c) 2005-2013 Ryota Shioya.
00008 // Copyright (c) 2005-2013 Masahiro Goshima.
00009 // 
00010 // This software is provided 'as-is', without any express or implied
00011 // warranty. In no event will the authors be held liable for any damages
00012 // arising from the use of this software.
00013 // 
00014 // Permission is granted to anyone to use this software for any purpose,
00015 // including commercial applications, and to alter it and redistribute it
00016 // freely, subject to the following restrictions:
00017 // 
00018 // 1. The origin of this software must not be misrepresented; you must not
00019 // claim that you wrote the original software. If you use this software
00020 // in a product, an acknowledgment in the product documentation would be
00021 // appreciated but is not required.
00022 // 
00023 // 2. Altered source versions must be plainly marked as such, and must not be
00024 // misrepresented as being the original software.
00025 // 
00026 // 3. This notice may not be removed or altered from any source
00027 // distribution.
00028 // 
00029 // 
00030 
00031 
00032 #include <pch.h>
00033 #if defined(HOST_IS_WINDOWS)
00034 #include <windows.h>
00035 #elif defined(HOST_IS_CYGWIN) || defined(HOST_IS_LINUX)
00036 //
00037 #endif
00038 #include "SysDeps/posix.h"
00039 
00040 using namespace std;
00041 using namespace Onikiri;
00042 using namespace Onikiri::POSIX;
00043 
00044 #if defined(HOST_IS_WINDOWS)
00045 //
00046 // Windows
00047 //
00048 namespace {
00049     // XbhZ[tVOXbh
00050     // }`XbhTLSgK
00051     int posix_errno = 0;
00052 
00053     // MSDN errno UNIX
00054     // x86_64 LinuxERANGE=34mF
00055 
00056     // G[`FbNKverrno
00057     template <typename T, T ErrorVal>
00058     T check_error(T result)
00059     {
00060         if (result == ErrorVal) {
00061             posix_errno = errno;
00062         }
00063         return result;
00064     }
00065 }
00066 
00067 namespace Onikiri {
00068     namespace POSIX {
00069         int posix_geterrno()
00070         {
00071             return posix_errno;
00072         }
00073 
00074         int posix_getpid()
00075         {
00076             return _getpid();
00077         }
00078         char *posix_getcwd(char* buf, int maxlen)
00079         {
00080             return check_error<char*, 0>( _getcwd(buf, maxlen) );
00081         }
00082         int posix_chdir(const char* path)
00083         {
00084             return check_error<int, -1>( _chdir(path) );
00085         }
00086 
00087         int posix_open(const char* name, int flags)
00088         {
00089             return check_error<int, -1>( _open(name, flags | POSIX_O_BINARY) );
00090         }
00091         int posix_open(const char* name, int flags, int pmode)
00092         {
00093             return check_error<int, -1>( _open(name, flags | POSIX_O_BINARY, pmode) );
00094         }
00095         int posix_read(int fd, void* buf, unsigned int count)
00096         {
00097             return check_error<int, -1>( _read(fd, buf, count) );
00098         }
00099         int posix_write(int fd, void* buf, unsigned int count)
00100         {
00101             return check_error<int, -1>( _write(fd, buf, count) );
00102         }
00103         int posix_close(int fd)
00104         {
00105             return check_error<int, -1>( _close(fd) );
00106         }
00107         s64 posix_lseek(int fd, s64 offset, int whence)
00108         {
00109             return check_error<s64, -1>( _lseeki64(fd, offset, whence) );
00110         }
00111         int posix_dup(int fd)
00112         {
00113             return check_error<int, -1>( _dup(fd) );
00114         }
00115 
00116 
00117         int posix_fstat(int fd, posix_struct_stat* s)
00118         {
00119             return check_error<int, -1>( fstat(fd, s) );
00120         }
00121         int posix_stat(const char* path, posix_struct_stat* s)
00122         {
00123             return check_error<int, -1>( stat(path, s) );
00124         }
00125         int posix_lstat(const char* path, posix_struct_stat* s)
00126         {
00127             // Windows V{bNNCstatlstat
00128             return posix_stat(path, s);
00129         }
00130 
00131         int posix_fileno(FILE *file)
00132         {
00133             return check_error<int, -1>( _fileno(file) );
00134         }
00135         int posix_access(const char* path, int mode)
00136         {
00137             return check_error<int, -1>( _access(path, mode) );
00138         }
00139         int posix_unlink(const char* path)
00140         {
00141             return check_error<int, -1>( _unlink(path) );
00142         }
00143         int posix_rename(const char* oldpath, const char* newpath)
00144         {
00145             if (MoveFileEx(oldpath, newpath, MOVEFILE_REPLACE_EXISTING)) {
00146                 return 0;
00147             }
00148             else {
00149                 switch (GetLastError()) {
00150                 case ERROR_FILE_NOT_FOUND:
00151                 case ERROR_PATH_NOT_FOUND:
00152                     posix_errno = ENOENT;
00153                     break;
00154                 case ERROR_ACCESS_DENIED:
00155                 default:
00156                     posix_errno = EACCES;
00157                     break;
00158                 }
00159                 return -1;
00160             }
00161         }
00162 
00163         int posix_truncate(const char* path, s64 length)
00164         {
00165             int fd = posix_open(path, POSIX_O_RDWR);
00166             if (fd == -1)
00167                 return -1;
00168             int result = posix_ftruncate(fd, length);
00169             posix_close(fd);
00170             return result;
00171         }
00172 
00173         int posix_ftruncate(int fd, s64 length)
00174         {
00175             int result = _chsize_s(fd, length);
00176             if (result != 0) {
00177                 posix_errno = result;
00178                 return -1;
00179             }
00180             else {
00181                 return 0;
00182             }
00183         }
00184     }
00185 }
00186 
00187 #elif defined(HOST_IS_CYGWIN) || defined(HOST_IS_LINUX)
00188 //
00189 // Unix
00190 //
00191 #endif
00192 

Onikiri2に対してTue Jun 18 14:34:26 2013に生成されました。  doxygen 1.4.7