tesseract  4.00.00dev
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 bool ReadFileToString(const string& filename, string* out);
46 
47  // Helper methods
48 
49  // Concatenate file paths removing any extra intervening '/' symbols.
50  static string JoinPath(const string& prefix, const string& suffix);
51  // Delete a filename or all filenames matching a glob pattern.
52  static bool Delete(const char* pathname);
53  static bool DeleteMatchingFiles(const char* pattern);
54 };
55 
56 // A class to manipulate Files for reading.
57 class InputBuffer {
58  public:
59  explicit InputBuffer(FILE* stream);
60  // 'size' is ignored.
61  InputBuffer(FILE* stream, size_t size);
62 
63  ~InputBuffer();
64 
65  // Read data until end-of-file.
66  // The data is stored in '*out'.
67  // Return false if an error occurs, true otherwise.
68  bool Read(string* out);
69 
70  // Close the FILE* used by InputBuffer.
71  // Return false if an error occurs, true otherwise.
72  bool CloseFile();
73 
74  private:
75  FILE* stream_;
76  int filesize_;
77 };
78 
79 // A class to manipulate Files for writing.
80 class OutputBuffer {
81  public:
82  explicit OutputBuffer(FILE* stream);
83  // 'size' is ignored.
84  OutputBuffer(FILE* stream, size_t size);
85 
86  ~OutputBuffer();
87 
88  // Write string 'str' to the open FILE*.
89  void WriteString(const string& str);
90 
91  // Close the FILE* used by InputBuffer.
92  // Return false if an error occurs, true otherwise.
93  bool CloseFile();
94 
95  private:
96  FILE* stream_;
97 };
98 
99 } // namespace tesseract
100 #endif // TESSERACT_TRAINING_FILEIO_H_
static void WriteStringToFileOrDie(const string &str, const string &filename)
Definition: fileio.cpp:52
voidpf stream
Definition: ioapi.h:39
static bool DeleteMatchingFiles(const char *pattern)
Definition: fileio.cpp:111
voidpf void uLong size
Definition: ioapi.h:39
static FILE * Open(const string &filename, const string &mode)
Definition: fileio.cpp:38
static string JoinPath(const string &prefix, const string &suffix)
Definition: fileio.cpp:81
static FILE * OpenOrDie(const string &filename, const string &mode)
Definition: fileio.cpp:42
static bool Readable(const string &filename)
Definition: fileio.cpp:63
const char int mode
Definition: ioapi.h:38
static bool Delete(const char *pathname)
Definition: fileio.cpp:87
const char * filename
Definition: ioapi.h:38
static bool ReadFileToString(const string &filename, string *out)
Definition: fileio.cpp:72