4
4
#include < fstream>
5
5
#include < string>
6
6
#include " Converter.h"
7
+ #include < bit_string.h>
7
8
8
9
namespace BinaryIO {
9
10
10
- bool doesFileExist (const std::string &filename){
11
- std::ifstream input (filename , std::ios::in | std::ios::binary);
11
+ static const uint32_t BYTE = 8 ;
12
+
13
+ bool doesFileExist (const std::string& filename) {
14
+ std::ifstream input (filename, std::ios::in | std::ios::binary);
12
15
return input.good ();
13
16
}
14
17
15
- int getFileSize (std::ifstream & input){
16
- input.seekg (0 , std::ios::end);
18
+ int getFileSize (std::ifstream& input) {
19
+ input.seekg (0 , std::ios::end);
17
20
return input.tellg ();
18
21
}
19
22
20
- int getFileSize (const std::string & filename){
23
+ int getFileSize (const std::string& filename) {
21
24
std::ifstream input (filename);
22
25
return getFileSize (input);
23
26
}
@@ -42,6 +45,26 @@ namespace BinaryIO {
42
45
return readString (filename, 0 , getFileSize (filename));
43
46
}
44
47
48
+ bit_string readBitString (const std::string& filename, int startPosition, int length) {
49
+ std::ifstream input (filename, std::ios::in | std::ios::binary);
50
+ bit_string fileData;
51
+ if (input) {
52
+ input.seekg (startPosition);
53
+ fileData.resize (length * BYTE);
54
+ input.read ((char *) fileData.data (), fileData.length_in_bytes ());
55
+ input.close ();
56
+ }
57
+ return fileData;
58
+ }
59
+
60
+ bit_string readBitString (const std::string& filename, int startPosition) {
61
+ return readBitString (filename, startPosition, getFileSize (filename) - startPosition);
62
+ }
63
+
64
+ bit_string readBitString (const std::string& filename) {
65
+ return readBitString (filename, 0 , getFileSize (filename));
66
+ }
67
+
45
68
46
69
// Append binaryData to the end of the file for strings
47
70
void write (const std::string& filename, const std::string& binaryData) {
@@ -53,6 +76,13 @@ namespace BinaryIO {
53
76
output.close ();
54
77
}
55
78
79
+ // Append binaryData to the end of the file for strings
80
+ void write (const std::string& filename, const bit_string& binaryData) {
81
+ std::ofstream output (filename, std::ios::out | std::ios::binary | std::ios::app);
82
+ output.write ((char *) binaryData.data (), binaryData.length_in_bytes ());
83
+ output.close ();
84
+ }
85
+
56
86
// Append binaryData to the end of the file for integers
57
87
void write (const std::string& filename, int binaryData) {
58
88
0 commit comments