liblcf
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
writer_lcf.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 liblcf authors
3  * This file is released under the MIT License
4  * http://opensource.org/licenses/MIT
5  */
6 
7 #include "writer_lcf.h"
8 
9 LcfWriter::LcfWriter(const char* filename, std::string encoding) :
10  filename(filename),
11  encoding(encoding),
12  stream(fopen(filename, "wb"))
13 {
14 }
15 
16 LcfWriter::LcfWriter(const std::string& filename, std::string encoding) :
17  filename(filename),
18  encoding(encoding),
19  stream(fopen(filename.c_str(), "wb"))
20 {
21 }
22 
24  Close();
25 }
26 
28  if (stream != NULL)
29  fclose(stream);
30  stream = NULL;
31 }
32 
33 void LcfWriter::Write(const void *ptr, size_t size, size_t nmemb) {
34 #ifdef NDEBUG
35  fwrite(ptr, size, nmemb, stream);
36 #else
37  assert(fwrite(ptr, size, nmemb, stream) == nmemb);
38 #endif
39 }
40 
41 template <>
42 void LcfWriter::Write<uint8_t>(uint8_t val) {
43  Write(&val, 1, 1);
44 }
45 
46 template <>
47 void LcfWriter::Write<int16_t>(int16_t val) {
48  SwapByteOrder(val);
49  Write(&val, 2, 1);
50 }
51 
52 template <>
53 void LcfWriter::Write<uint32_t>(uint32_t val) {
54  SwapByteOrder(val);
55  Write(&val, 4, 1);
56 }
57 
58 void LcfWriter::WriteInt(int val) {
59  uint32_t value = (uint32_t) val;
60  for (int i = 28; i >= 0; i -= 7)
61  if (value >= (1U << i) || i == 0)
62  Write<uint8_t>((uint8_t)(((value >> i) & 0x7F) | (i > 0 ? 0x80 : 0)));
63 }
64 
65 template <>
66 void LcfWriter::Write<int>(int val) {
67  WriteInt(val);
68 }
69 
70 template <>
71 void LcfWriter::Write<bool>(bool val) {
72  uint8_t x = val ? 1 : 0;
73  Write(x);
74 }
75 
76 template <>
77 void LcfWriter::Write<double>(double val) {
78  SwapByteOrder(val);
79  Write(&val, 8, 1);
80 }
81 
82 template <>
83 void LcfWriter::Write<bool>(const std::vector<bool>& buffer) {
84  std::vector<bool>::const_iterator it;
85  for (it = buffer.begin(); it != buffer.end(); it++) {
86  uint8_t val = *it ? 1 : 0;
87  Write(val);
88  }
89 }
90 
91 template <>
92 void LcfWriter::Write<uint8_t>(const std::vector<uint8_t>& buffer) {
93  Write(&buffer.front(), 1, buffer.size());
94 }
95 
96 template <>
97 void LcfWriter::Write<int16_t>(const std::vector<int16_t>& buffer) {
98  std::vector<int16_t>::const_iterator it;
99  for (it = buffer.begin(); it != buffer.end(); it++)
100  Write(*it);
101 }
102 
103 template <>
104 void LcfWriter::Write<uint32_t>(const std::vector<uint32_t>& buffer) {
105  std::vector<uint32_t>::const_iterator it;
106  for (it = buffer.begin(); it != buffer.end(); it++)
107  Write(*it);
108 }
109 
110 void LcfWriter::Write(const std::string& _str) {
111  std::string str = Decode(_str);
112  if (!str.empty()) {
113  Write(&*str.begin(), 1, str.size());
114  }
115 }
116 
117 bool LcfWriter::IsOk() const {
118  return (stream != NULL && !ferror(stream));
119 }
120 
121 std::string LcfWriter::Decode(const std::string& str_to_encode) {
122  return ReaderUtil::Recode(str_to_encode, "UTF-8", encoding);
123 }
124 
125 #ifdef WORDS_BIGENDIAN
126 void LcfWriter::SwapByteOrder(uint16_t& us)
127 {
128  us = (us >> 8) |
129  (us << 8);
130 }
131 
132 void LcfWriter::SwapByteOrder(uint32_t& ui)
133 {
134  ui = (ui >> 24) |
135  ((ui<<8) & 0x00FF0000) |
136  ((ui>>8) & 0x0000FF00) |
137  (ui << 24);
138 }
139 
140 void LcfWriter::SwapByteOrder(double& d)
141 {
142  uint32_t *p = reinterpret_cast<uint32_t *>(&d);
143  SwapByteOrder(p[0]);
144  SwapByteOrder(p[1]);
145  uint32_t tmp = p[0];
146  p[0] = p[1];
147  p[1] = tmp;
148 }
149 #else
150 void LcfWriter::SwapByteOrder(uint16_t& /* us */) {}
151 void LcfWriter::SwapByteOrder(uint32_t& /* ui */) {}
152 void LcfWriter::SwapByteOrder(double& /* d */) {}
153 #endif
154 
155 void LcfWriter::SwapByteOrder(int16_t& s)
156 {
157  SwapByteOrder((uint16_t&) s);
158 }
std::string Decode(const std::string &str_to_encode)
Definition: writer_lcf.cpp:121
void WriteInt(int val)
Definition: writer_lcf.cpp:58
std::string Recode(const std::string &str_to_encode, const std::string &source_encoding)
std::string encoding
Definition: writer_lcf.h:112
bool IsOk() const
Definition: writer_lcf.cpp:117
void Write(const void *ptr, size_t size, size_t nmemb)
Definition: writer_lcf.cpp:33
LcfWriter(const char *filename, std::string encoding="")
Definition: writer_lcf.cpp:9
void Close()
Definition: writer_lcf.cpp:27
static void SwapByteOrder(int16_t &us)
Definition: writer_lcf.cpp:155
FILE * stream
Definition: writer_lcf.h:114