file.h

Go to the documentation of this file.
00001 
00006 /*
00007  Copyright (C) 2001-2006, William Joseph.
00008  All Rights Reserved.
00009 
00010  This file is part of GtkRadiant.
00011 
00012  GtkRadiant is free software; you can redistribute it and/or modify
00013  it under the terms of the GNU General Public License as published by
00014  the Free Software Foundation; either version 2 of the License, or
00015  (at your option) any later version.
00016 
00017  GtkRadiant is distributed in the hope that it will be useful,
00018  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  GNU General Public License for more details.
00021 
00022  You should have received a copy of the GNU General Public License
00023  along with GtkRadiant; if not, write to the Free Software
00024  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00025  */
00026 
00027 #if !defined(INCLUDED_OS_FILE_H)
00028 #define INCLUDED_OS_FILE_H
00029 
00030 #if defined(WIN32)
00031 #include <io.h> // _access()
00032 #define access(path, mode) _access(path, mode)
00033 #else
00034 #include <unistd.h> // access()
00035 #endif
00036 
00037 #include <string>
00038 #include <stdio.h> // rename(), remove()
00039 #include <sys/stat.h> // stat()
00040 #include <sys/types.h> // this is included by stat.h on win32
00041 #include <cstddef>
00042 #include <ctime>
00043 
00044 #include "debugging/debugging.h"
00045 
00053 inline bool file_move (const std::string& from, const std::string& to)
00054 {
00055     return rename(from.c_str(), to.c_str()) == 0;
00056 }
00057 
00063 inline bool file_remove (const std::string& path)
00064 {
00065     return remove(path.c_str()) == 0;
00066 }
00067 
00068 namespace FileAccess
00069 {
00070     enum Mode
00071     {
00072         Read = R_OK, Write = W_OK, ReadWrite = Read | Write, Exists = F_OK
00073     };
00074 }
00075 
00077 inline bool file_accessible (const std::string& path, FileAccess::Mode mode)
00078 {
00079     return access(path.c_str(), static_cast<int> (mode)) == 0;
00080 }
00081 
00083 inline bool file_readable (const std::string& path)
00084 {
00085     return file_accessible(path, FileAccess::Read);
00086 }
00087 
00089 inline bool file_writeable (const std::string& path)
00090 {
00091     return file_accessible(path, FileAccess::Write);
00092 }
00093 
00095 inline bool file_exists (const std::string& path)
00096 {
00097     return file_accessible(path, FileAccess::Exists);
00098 }
00099 
00101 inline bool file_is_directory (const std::string& path)
00102 {
00103     struct stat st;
00104     if (stat(path.c_str(), &st) == -1)
00105         return false;
00106     return S_ISDIR (st.st_mode) != 0;
00107 }
00108 
00109 typedef std::size_t FileSize;
00110 
00112 inline FileSize file_size (const std::string& path)
00113 {
00114     struct stat st;
00115     if (stat(path.c_str(), &st) == -1)
00116         return 0;
00117     return st.st_size;
00118 }
00119 
00121 typedef std::time_t FileTime;
00123 const FileTime c_invalidFileTime = -1;
00124 
00126 inline FileTime file_modified (const std::string& path)
00127 {
00128     struct stat st;
00129     if (stat(path.c_str(), &st) == -1)
00130         return c_invalidFileTime;
00131     return st.st_mtime;
00132 }
00133 
00134 typedef unsigned char byte;
00135 
00139 inline int file_write (const void *buffer, int len, FILE * f)
00140 {
00141     int block, remaining;
00142     int written;
00143     byte *buf;
00144     int tries;
00145 
00146     if (!f)
00147         return 0;
00148 
00149     buf = (byte *) buffer;
00150 
00151     remaining = len;
00152     tries = 0;
00153     while (remaining) {
00154         block = remaining;
00155         written = fwrite(buf, 1, block, f);
00156         if (written == 0) {
00157             if (!tries) {
00158                 tries = 1;
00159             } else {
00160                 g_message("file_write: 0 bytes written\n");
00161                 return 0;
00162             }
00163         }
00164 
00165         if (written == -1) {
00166             g_message("file_write: -1 bytes written\n");
00167             return 0;
00168         }
00169 
00170         remaining -= written;
00171         buf += written;
00172     }
00173     /*  fflush(f->f); */
00174     return len;
00175 }
00176 
00177 #endif

Generated by  doxygen 1.6.2