tesseract 4.1.1
Loading...
Searching...
No Matches
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 "genericvector.h" // for GenericVector
24#include "platform.h"
25#include "strngs.h" // for STRING
26
27namespace tesseract {
28
29// Reads a file as a vector of STRING.
30// TODO: Use std::vector and std::string for LoadFileLinesToStrings.
31inline 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.
43class 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.
70 public:
71 explicit InputBuffer(FILE* stream);
72 // 'size' is ignored.
73 InputBuffer(FILE* stream, size_t size);
74
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.
92 public:
93 explicit OutputBuffer(FILE* stream);
94 // 'size' is ignored.
95 OutputBuffer(FILE* stream, size_t size);
96
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_
bool LoadFileLinesToStrings(const char *filename, GenericVector< STRING > *lines)
Definition: fileio.h:31
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)
int size() const
Definition: genericvector.h:72
Definition: strngs.h:45
void split(char c, GenericVector< STRING > *splited)
Definition: strngs.cpp:282
static bool DeleteMatchingFiles(const char *pattern)
Definition: fileio.cpp:121
static FILE * OpenOrDie(const std::string &filename, const std::string &mode)
Definition: fileio.cpp:46
static std::string JoinPath(const std::string &prefix, const std::string &suffix)
Definition: fileio.cpp:86
static bool Delete(const char *pathname)
Definition: fileio.cpp:92
static bool Readable(const std::string &filename)
Definition: fileio.cpp:68
static bool ReadFileToString(const std::string &filename, std::string *out)
Definition: fileio.cpp:77
static void WriteStringToFileOrDie(const std::string &str, const std::string &filename)
Definition: fileio.cpp:56
static FILE * Open(const std::string &filename, const std::string &mode)
Definition: fileio.cpp:42
bool Read(std::string *out)
Definition: fileio.cpp:152
void WriteString(const std::string &str)
Definition: fileio.cpp:190