All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
fileio.h
Go to the documentation of this file.
1 /**********************************************************************
2  * File: fileio.h
3  * Description: File I/O utilities.
4  * Author: Samuel Charron
5  * Created: Tuesday, July 9, 2013
6  *
7  * (C) Copyright 2013, Google Inc.
8  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9  * use this file except in compliance with the License. You may obtain a copy
10  * of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
11  * by applicable law or agreed to in writing, software distributed under the
12  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
13  * OF ANY KIND, either express or implied. See the License for the specific
14  * language governing permissions and limitations under the License.
15  *
16  **********************************************************************/
17 #ifndef TESSERACT_TRAINING_FILEIO_H_
18 #define TESSERACT_TRAINING_FILEIO_H_
19 
20 #include <stddef.h>
21 #include <cstdio>
22 #include <string>
23 
24 #ifdef USE_STD_NAMESPACE
25 using std::string;
26 #endif
27 
28 namespace tesseract {
29 
30 // A class to manipulate FILE*s.
31 class File {
32  public:
33  // Try to open the file 'filename' in mode 'mode'.
34  // Stop the program if it cannot open it.
35  static FILE* OpenOrDie(const string& filename, const string& mode);
36  static FILE* Open(const string& filename, const string& mode);
37 
38  // Try to open the file 'filename' and to write 'str' in it.
39  // Stop the program if it fails.
40  static void WriteStringToFileOrDie(const string& str, const string& filename);
41 
42  // Return true if the file 'filename' is readable.
43  static bool Readable(const string& filename);
44 
45  static void ReadFileToStringOrDie(const string& filename, string* out);
46  static bool ReadFileToString(const string& filename, string* out);
47 
48  // Helper methods
49 
50  // Concatenate file paths removing any extra intervening '/' symbols.
51  static string JoinPath(const string& prefix, const string& suffix);
52  // Delete a filename or all filenames matching a glob pattern.
53  static bool Delete(const char* pathname);
54  static bool DeleteMatchingFiles(const char* pattern);
55 };
56 
57 // A class to manipulate Files for reading.
58 class InputBuffer {
59  public:
60  explicit InputBuffer(FILE* stream);
61  // 'size' is ignored.
62  InputBuffer(FILE* stream, size_t size);
63 
64  ~InputBuffer();
65 
66  // Read data until end-of-file.
67  // The data is stored in '*out'.
68  // Return false if an error occurs, true otherwise.
69  bool Read(string* out);
70 
71  // Close the FILE* used by InputBuffer.
72  // Return false if an error occurs, true otherwise.
73  bool CloseFile();
74 
75  private:
76  FILE* stream_;
77  int filesize_;
78 };
79 
80 // A class to manipulate Files for writing.
81 class OutputBuffer {
82  public:
83  explicit OutputBuffer(FILE* stream);
84  // 'size' is ignored.
85  OutputBuffer(FILE* stream, size_t size);
86 
87  ~OutputBuffer();
88 
89  // Write string 'str' to the open FILE*.
90  void WriteString(const string& str);
91 
92  // Close the FILE* used by InputBuffer.
93  // Return false if an error occurs, true otherwise.
94  bool CloseFile();
95 
96  private:
97  FILE* stream_;
98 };
99 
100 } // namespace tesseract
101 #endif // TESSERACT_TRAINING_FILEIO_H_
static bool ReadFileToString(const string &filename, string *out)
Definition: fileio.cpp:73
void WriteString(const string &str)
Definition: fileio.cpp:193
static bool Readable(const string &filename)
Definition: fileio.cpp:64
static string JoinPath(const string &prefix, const string &suffix)
Definition: fileio.cpp:89
static bool Delete(const char *pathname)
Definition: fileio.cpp:94
static FILE * OpenOrDie(const string &filename, const string &mode)
Definition: fileio.cpp:43
CMD_EVENTS mode
Definition: pgedit.cpp:116
static void WriteStringToFileOrDie(const string &str, const string &filename)
Definition: fileio.cpp:53
static bool DeleteMatchingFiles(const char *pattern)
Definition: fileio.cpp:118
static FILE * Open(const string &filename, const string &mode)
Definition: fileio.cpp:39
bool Read(string *out)
Definition: fileio.cpp:155
OutputBuffer(FILE *stream)
Definition: fileio.cpp:179
static void ReadFileToStringOrDie(const string &filename, string *out)
Definition: fileio.cpp:83
InputBuffer(FILE *stream)
Definition: fileio.cpp:135