tesseract  5.0.0
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 "export.h"
20 #include "helpers.h" // for split
21 #include "serialis.h" // for LoadDataFromFile
22 
23 #include <tesseract/export.h>
24 
25 #include <cstddef>
26 #include <cstdio>
27 #include <string>
28 
29 namespace tesseract {
30 
31 // Reads a file as a vector of string.
32 inline bool LoadFileLinesToStrings(const char *filename, std::vector<std::string> *lines) {
33  std::vector<char> data;
34  if (!LoadDataFromFile(filename, &data)) {
35  return false;
36  }
37  // TODO: optimize.
38  std::string lines_str(&data[0], data.size());
39  *lines = split(lines_str, '\n');
40  return true;
41 }
42 
43 // A class to manipulate FILE*s.
44 class TESS_UNICHARSET_TRAINING_API File {
45 public:
46  // Try to open the file 'filename' in mode 'mode'.
47  // Stop the program if it cannot open it.
48  static FILE *OpenOrDie(const std::string &filename, const std::string &mode);
49  static FILE *Open(const std::string &filename, const std::string &mode);
50 
51  // Try to open the file 'filename' and to write 'str' in it.
52  // Stop the program if it fails.
53  static void WriteStringToFileOrDie(const std::string &str, const std::string &filename);
54 
55  // Return true if the file 'filename' is readable.
56  static bool Readable(const std::string &filename);
57 
58  static bool ReadFileToString(const std::string &filename, std::string *out);
59 
60  // Helper methods
61 
62  // Concatenate file paths removing any extra intervening '/' symbols.
63  static std::string JoinPath(const std::string &prefix, const std::string &suffix);
64  // Delete a filename or all filenames matching a glob pattern.
65  static bool Delete(const char *pathname);
66  static bool DeleteMatchingFiles(const char *pattern);
67 };
68 
69 // A class to manipulate Files for reading.
70 class TESS_UNICHARSET_TRAINING_API InputBuffer {
71 public:
72  explicit InputBuffer(FILE *stream);
73  // 'size' is ignored.
74  InputBuffer(FILE *stream, size_t size);
75 
76  ~InputBuffer();
77 
78  // Read data until end-of-file.
79  // The data is stored in '*out'.
80  // Return false if an error occurs, true otherwise.
81  bool Read(std::string *out);
82 
83  // Close the FILE* used by InputBuffer.
84  // Return false if an error occurs, true otherwise.
85  bool CloseFile();
86 
87 private:
88  FILE *stream_;
89 };
90 
91 // A class to manipulate Files for writing.
92 class TESS_UNICHARSET_TRAINING_API OutputBuffer {
93 public:
94  explicit OutputBuffer(FILE *stream);
95  // 'size' is ignored.
96  OutputBuffer(FILE *stream, size_t size);
97 
98  ~OutputBuffer();
99 
100  // Write string 'str' to the open FILE*.
101  void WriteString(const std::string &str);
102 
103  // Close the FILE* used by InputBuffer.
104  // Return false if an error occurs, true otherwise.
105  bool CloseFile();
106 
107 private:
108  FILE *stream_;
109 };
110 
111 } // namespace tesseract
112 #endif // TESSERACT_TRAINING_FILEIO_H_
const std::vector< std::string > split(const std::string &s, char c)
Definition: helpers.h:41
bool LoadFileLinesToStrings(const char *filename, std::vector< std::string > *lines)
Definition: fileio.h:32
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)