tesseract  v4.0.0-17-g361f3264
Open Source OCR Engine
fileio.h
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 <cstddef>
21 #include <cstdio>
22 #include <string>
23 
24 #include "platform.h"
25 
26 namespace tesseract {
27 
28 // A class to manipulate FILE*s.
29 class File {
30  public:
31  // Try to open the file 'filename' in mode 'mode'.
32  // Stop the program if it cannot open it.
33  static FILE* OpenOrDie(const std::string& filename, const std::string& mode);
34  static FILE* Open(const std::string& filename, const std::string& mode);
35 
36  // Try to open the file 'filename' and to write 'str' in it.
37  // Stop the program if it fails.
38  static void WriteStringToFileOrDie(const std::string& str, const std::string& filename);
39 
40  // Return true if the file 'filename' is readable.
41  static bool Readable(const std::string& filename);
42 
43  static bool ReadFileToString(const std::string& filename, std::string* out);
44 
45  // Helper methods
46 
47  // Concatenate file paths removing any extra intervening '/' symbols.
48  static std::string JoinPath(const std::string& prefix, const std::string& suffix);
49  // Delete a filename or all filenames matching a glob pattern.
50  static bool Delete(const char* pathname);
51  static bool DeleteMatchingFiles(const char* pattern);
52 };
53 
54 // A class to manipulate Files for reading.
55 class InputBuffer {
56  public:
57  explicit InputBuffer(FILE* stream);
58  // 'size' is ignored.
59  InputBuffer(FILE* stream, size_t size);
60 
61  ~InputBuffer();
62 
63  // Read data until end-of-file.
64  // The data is stored in '*out'.
65  // Return false if an error occurs, true otherwise.
66  bool Read(std::string* out);
67 
68  // Close the FILE* used by InputBuffer.
69  // Return false if an error occurs, true otherwise.
70  bool CloseFile();
71 
72  private:
73  FILE* stream_;
74  int filesize_;
75 };
76 
77 // A class to manipulate Files for writing.
78 class OutputBuffer {
79  public:
80  explicit OutputBuffer(FILE* stream);
81  // 'size' is ignored.
82  OutputBuffer(FILE* stream, size_t size);
83 
84  ~OutputBuffer();
85 
86  // Write string 'str' to the open FILE*.
87  void WriteString(const std::string& str);
88 
89  // Close the FILE* used by InputBuffer.
90  // Return false if an error occurs, true otherwise.
91  bool CloseFile();
92 
93  private:
94  FILE* stream_;
95 };
96 
97 } // namespace tesseract
98 #endif // TESSERACT_TRAINING_FILEIO_H_
FILE * stream_
Definition: fileio.h:94
int filesize_
Definition: fileio.h:74
static void WriteStringToFileOrDie(const std::string &str, const std::string &filename)
Definition: fileio.cpp:53
static bool Delete(const char *pathname)
Definition: fileio.cpp:88
Definition: fileio.h:55
static FILE * OpenOrDie(const std::string &filename, const std::string &mode)
Definition: fileio.cpp:43
Definition: baseapi.cpp:94
Definition: fileio.h:29
static bool Readable(const std::string &filename)
Definition: fileio.cpp:64
static bool ReadFileToString(const std::string &filename, std::string *out)
Definition: fileio.cpp:73
Definition: fileio.h:78
static FILE * Open(const std::string &filename, const std::string &mode)
Definition: fileio.cpp:39
static std::string JoinPath(const std::string &prefix, const std::string &suffix)
Definition: fileio.cpp:82
static bool DeleteMatchingFiles(const char *pattern)
Definition: fileio.cpp:112
FILE * stream_
Definition: fileio.h:73