Brian Silverman | 70325d6 | 2015-09-20 17:00:43 -0400 | [diff] [blame^] | 1 | // Copyright (c) 2011, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | // --- |
| 31 | // Author: csilvers@google.com (Craig Silverstein) |
| 32 | // |
| 33 | // A tiny wrapper around struct stat and FILE*. |
| 34 | |
| 35 | #ifndef TEMPLATE_OPENSOURCE_FILEUTIL_H_ |
| 36 | #define TEMPLATE_OPENSOURCE_FILEUTIL_H_ |
| 37 | |
| 38 | #include <config.h> |
| 39 | #include <stdio.h> |
| 40 | #include <sys/stat.h> |
| 41 | #include <time.h> |
| 42 | #ifdef HAVE_UNISTD_H |
| 43 | # include <unistd.h> |
| 44 | #endif |
| 45 | #include <string> |
| 46 | |
| 47 | namespace ctemplate { |
| 48 | |
| 49 | class FileStat { |
| 50 | public: |
| 51 | time_t mtime; |
| 52 | off_t length; |
| 53 | bool IsDirectory() { return S_ISDIR(internal_statbuf.st_mode); } |
| 54 | |
| 55 | private: |
| 56 | friend class File; |
| 57 | struct stat internal_statbuf; |
| 58 | }; |
| 59 | |
| 60 | class File { |
| 61 | public: |
| 62 | static bool Stat(const std::string& filename, FileStat* statbuf) { |
| 63 | if (stat(filename.c_str(), &statbuf->internal_statbuf) != 0) |
| 64 | return false; |
| 65 | statbuf->mtime = statbuf->internal_statbuf.st_mtime; |
| 66 | statbuf->length = statbuf->internal_statbuf.st_size; |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | static bool Readable(const char* filename) { |
| 71 | return access(filename, R_OK) == 0; |
| 72 | } |
| 73 | |
| 74 | static File* Open(const char* filename, const char* mode) { |
| 75 | char binary_mode[3]; |
| 76 | const char* mode_to_use = mode; |
| 77 | if ((mode[0] == 'r' || mode[0] == 'w') && mode[1] == '\0') { |
| 78 | // We add a 'b' to make sure we do the right thing even on |
| 79 | // Windows. On unix, this will be a noop. |
| 80 | binary_mode[0] = mode[0]; |
| 81 | binary_mode[1] = 'b'; |
| 82 | binary_mode[2] = '\0'; |
| 83 | mode_to_use = binary_mode; |
| 84 | } |
| 85 | FILE* fp = fopen(filename, mode_to_use); |
| 86 | if (!fp) return NULL; |
| 87 | return new File(fp); |
| 88 | } |
| 89 | |
| 90 | size_t Read(char* buf, size_t size) { |
| 91 | return fread(buf, 1, size, fp_); |
| 92 | } |
| 93 | |
| 94 | void Close() { |
| 95 | fclose(fp_); |
| 96 | delete this; // naughty naughty! |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | explicit File(FILE* fp) : fp_(fp) { } |
| 101 | FILE* fp_; |
| 102 | }; |
| 103 | |
| 104 | } |
| 105 | |
| 106 | #endif // TEMPLATE_OPENSOURCE_FILEUTIL_H_ |