tesseract  5.0.0-alpha-619-ge9db
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  *
6  * (C) Copyright 2013, Google Inc.
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8  * use this file except in compliance with the License. You may obtain a copy
9  * of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
10  * by applicable law or agreed to in writing, software distributed under the
11  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
12  * OF ANY KIND, either express or implied. See the License for the specific
13  * language governing permissions and limitations under the License.
14  *
15  **********************************************************************/
16 #ifndef TESSERACT_TRAINING_FILEIO_H_
17 #define TESSERACT_TRAINING_FILEIO_H_
18 
19 #include <cstddef>
20 #include <cstdio>
21 #include <string>
22 
23 #include <tesseract/genericvector.h> // for GenericVector
24 #include <tesseract/platform.h>
25 #include <tesseract/strngs.h> // for STRING
26 
27 namespace tesseract {
28 
29 // Reads a file as a vector of STRING.
30 // TODO: Use std::vector and std::string for LoadFileLinesToStrings.
31 inline bool LoadFileLinesToStrings(const char* filename,
32  GenericVector<STRING>* lines) {
34  if (!LoadDataFromFile(filename, &data)) {
35  return false;
36  }
37  STRING lines_str(&data[0], data.size());
38  lines_str.split('\n', lines);
39  return true;
40 }
41 
42 // A class to manipulate FILE*s.
43 class File {
44  public:
45  // Try to open the file 'filename' in mode 'mode'.
46  // Stop the program if it cannot open it.
47  static FILE* OpenOrDie(const std::string& filename, const std::string& mode);
48  static FILE* Open(const std::string& filename, const std::string& mode);
49 
50  // Try to open the file 'filename' and to write 'str' in it.
51  // Stop the program if it fails.
52  static void WriteStringToFileOrDie(const std::string& str, const std::string& filename);
53 
54  // Return true if the file 'filename' is readable.
55  static bool Readable(const std::string& filename);
56 
57  static bool ReadFileToString(const std::string& filename, std::string* out);
58 
59  // Helper methods
60 
61  // Concatenate file paths removing any extra intervening '/' symbols.
62  static std::string JoinPath(const std::string& prefix, const std::string& suffix);
63  // Delete a filename or all filenames matching a glob pattern.
64  static bool Delete(const char* pathname);
65  static bool DeleteMatchingFiles(const char* pattern);
66 };
67 
68 // A class to manipulate Files for reading.
69 class InputBuffer {
70  public:
71  explicit InputBuffer(FILE* stream);
72  // 'size' is ignored.
73  InputBuffer(FILE* stream, size_t size);
74 
75  ~InputBuffer();
76 
77  // Read data until end-of-file.
78  // The data is stored in '*out'.
79  // Return false if an error occurs, true otherwise.
80  bool Read(std::string* out);
81 
82  // Close the FILE* used by InputBuffer.
83  // Return false if an error occurs, true otherwise.
84  bool CloseFile();
85 
86  private:
87  FILE* stream_;
88 };
89 
90 // A class to manipulate Files for writing.
91 class OutputBuffer {
92  public:
93  explicit OutputBuffer(FILE* stream);
94  // 'size' is ignored.
95  OutputBuffer(FILE* stream, size_t size);
96 
97  ~OutputBuffer();
98 
99  // Write string 'str' to the open FILE*.
100  void WriteString(const std::string& str);
101 
102  // Close the FILE* used by InputBuffer.
103  // Return false if an error occurs, true otherwise.
104  bool CloseFile();
105 
106  private:
107  FILE* stream_;
108 };
109 
110 } // namespace tesseract
111 #endif // TESSERACT_TRAINING_FILEIO_H_
string
std::string string
Definition: equationdetect_test.cc:21
strngs.h
tesseract::OutputBuffer::WriteString
void WriteString(const std::string &str)
Definition: fileio.cpp:202
tesseract::File::Delete
static bool Delete(const char *pathname)
Definition: fileio.cpp:104
tesseract::InputBuffer::InputBuffer
InputBuffer(FILE *stream)
Definition: fileio.cpp:150
tesseract::File::Readable
static bool Readable(const std::string &filename)
Definition: fileio.cpp:80
tesseract::OutputBuffer
Definition: fileio.h:103
tesseract::File::Open
static FILE * Open(const std::string &filename, const std::string &mode)
Definition: fileio.cpp:54
tesseract::LoadDataFromFile
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)
Definition: genericvector.h:341
platform.h
STRING
Definition: strngs.h:45
tesseract::InputBuffer
Definition: fileio.h:81
tesseract::InputBuffer::CloseFile
bool CloseFile()
Definition: fileio.cpp:178
tesseract::File::JoinPath
static std::string JoinPath(const std::string &prefix, const std::string &suffix)
Definition: fileio.cpp:98
genericvector.h
tesseract::File::OpenOrDie
static FILE * OpenOrDie(const std::string &filename, const std::string &mode)
Definition: fileio.cpp:58
tesseract::OutputBuffer::~OutputBuffer
~OutputBuffer()
Definition: fileio.cpp:196
tesseract::LoadFileLinesToStrings
bool LoadFileLinesToStrings(const char *filename, GenericVector< STRING > *lines)
Definition: fileio.h:43
tesseract::File::WriteStringToFileOrDie
static void WriteStringToFileOrDie(const std::string &str, const std::string &filename)
Definition: fileio.cpp:68
tesseract
Definition: baseapi.h:65
GenericVector< STRING >
tesseract::File::ReadFileToString
static bool ReadFileToString(const std::string &filename, std::string *out)
Definition: fileio.cpp:89
tesseract::OutputBuffer::CloseFile
bool CloseFile()
Definition: fileio.cpp:206
tesseract::File
Definition: fileio.h:55
tesseract::InputBuffer::~InputBuffer
~InputBuffer()
Definition: fileio.cpp:158
tesseract::File::DeleteMatchingFiles
static bool DeleteMatchingFiles(const char *pattern)
Definition: fileio.cpp:133
tesseract::OutputBuffer::OutputBuffer
OutputBuffer(FILE *stream)
Definition: fileio.cpp:188
GenericVector::size
int size() const
Definition: genericvector.h:71
tesseract::InputBuffer::Read
bool Read(std::string *out)
Definition: fileio.cpp:164
STRING::split
void split(char c, GenericVector< STRING > *splited)
Definition: strngs.cpp:275