ooxml_crypt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (264) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +58 -0
  5. data/Rakefile +12 -0
  6. data/bin/console +15 -0
  7. data/bin/setup +8 -0
  8. data/ext/ooxml_crypt/extconf.rb +18 -0
  9. data/ext/ooxml_crypt/ooxml_crypt.c +27 -0
  10. data/ext/ooxml_crypt/ooxml_crypt.h +7 -0
  11. data/lib/ooxml_crypt/version.rb +5 -0
  12. data/lib/ooxml_crypt.rb +75 -0
  13. data/vendor/cybozulib/.github/workflows/main.yml +12 -0
  14. data/vendor/cybozulib/.gitignore +5 -0
  15. data/vendor/cybozulib/CMakeLists.txt +6 -0
  16. data/vendor/cybozulib/COPYRIGHT +27 -0
  17. data/vendor/cybozulib/Makefile +26 -0
  18. data/vendor/cybozulib/bin/libeay32.dll +0 -0
  19. data/vendor/cybozulib/bin/libmecab.dll +0 -0
  20. data/vendor/cybozulib/bin/ssleay32.dll +0 -0
  21. data/vendor/cybozulib/common.mk +116 -0
  22. data/vendor/cybozulib/common.props +25 -0
  23. data/vendor/cybozulib/cybozulib.sln +286 -0
  24. data/vendor/cybozulib/debug.props +14 -0
  25. data/vendor/cybozulib/include/cybozu/array.hpp +197 -0
  26. data/vendor/cybozulib/include/cybozu/atoi.hpp +238 -0
  27. data/vendor/cybozulib/include/cybozu/atomic.hpp +146 -0
  28. data/vendor/cybozulib/include/cybozu/base64.hpp +210 -0
  29. data/vendor/cybozulib/include/cybozu/benchmark.hpp +212 -0
  30. data/vendor/cybozulib/include/cybozu/bfd.hpp +105 -0
  31. data/vendor/cybozulib/include/cybozu/bit_operation.hpp +139 -0
  32. data/vendor/cybozulib/include/cybozu/bitvector.hpp +358 -0
  33. data/vendor/cybozulib/include/cybozu/condition_variable.hpp +113 -0
  34. data/vendor/cybozulib/include/cybozu/condition_variable_cs.hpp +74 -0
  35. data/vendor/cybozulib/include/cybozu/config.hpp +392 -0
  36. data/vendor/cybozulib/include/cybozu/critical_section.hpp +60 -0
  37. data/vendor/cybozulib/include/cybozu/crypto.hpp +321 -0
  38. data/vendor/cybozulib/include/cybozu/csucvector.hpp +624 -0
  39. data/vendor/cybozulib/include/cybozu/csv.hpp +294 -0
  40. data/vendor/cybozulib/include/cybozu/data_type.hpp +27 -0
  41. data/vendor/cybozulib/include/cybozu/endian.hpp +224 -0
  42. data/vendor/cybozulib/include/cybozu/env.hpp +63 -0
  43. data/vendor/cybozulib/include/cybozu/event.hpp +122 -0
  44. data/vendor/cybozulib/include/cybozu/exception.hpp +253 -0
  45. data/vendor/cybozulib/include/cybozu/file.hpp +626 -0
  46. data/vendor/cybozulib/include/cybozu/fmindex.hpp +291 -0
  47. data/vendor/cybozulib/include/cybozu/format.hpp +93 -0
  48. data/vendor/cybozulib/include/cybozu/frequency.hpp +264 -0
  49. data/vendor/cybozulib/include/cybozu/hash.hpp +67 -0
  50. data/vendor/cybozulib/include/cybozu/inttype.hpp +174 -0
  51. data/vendor/cybozulib/include/cybozu/itoa.hpp +336 -0
  52. data/vendor/cybozulib/include/cybozu/json.hpp +120 -0
  53. data/vendor/cybozulib/include/cybozu/line_stream.hpp +149 -0
  54. data/vendor/cybozulib/include/cybozu/link_libeay32.hpp +21 -0
  55. data/vendor/cybozulib/include/cybozu/link_mpir.hpp +18 -0
  56. data/vendor/cybozulib/include/cybozu/link_ssleay32.hpp +19 -0
  57. data/vendor/cybozulib/include/cybozu/log.hpp +237 -0
  58. data/vendor/cybozulib/include/cybozu/minixml.hpp +452 -0
  59. data/vendor/cybozulib/include/cybozu/mmap.hpp +143 -0
  60. data/vendor/cybozulib/include/cybozu/mutex.hpp +144 -0
  61. data/vendor/cybozulib/include/cybozu/nlp/mecab.hpp +96 -0
  62. data/vendor/cybozulib/include/cybozu/nlp/plsi.hpp +315 -0
  63. data/vendor/cybozulib/include/cybozu/nlp/random.hpp +74 -0
  64. data/vendor/cybozulib/include/cybozu/nlp/sparse.hpp +529 -0
  65. data/vendor/cybozulib/include/cybozu/nlp/svd.hpp +486 -0
  66. data/vendor/cybozulib/include/cybozu/nlp/tfidf.hpp +226 -0
  67. data/vendor/cybozulib/include/cybozu/nlp/top_score.hpp +75 -0
  68. data/vendor/cybozulib/include/cybozu/option.hpp +743 -0
  69. data/vendor/cybozulib/include/cybozu/parallel.hpp +88 -0
  70. data/vendor/cybozulib/include/cybozu/pcg.hpp +72 -0
  71. data/vendor/cybozulib/include/cybozu/process.hpp +324 -0
  72. data/vendor/cybozulib/include/cybozu/quit_signal_handler.hpp +66 -0
  73. data/vendor/cybozulib/include/cybozu/random_generator.hpp +144 -0
  74. data/vendor/cybozulib/include/cybozu/regex.hpp +463 -0
  75. data/vendor/cybozulib/include/cybozu/select8.hpp +279 -0
  76. data/vendor/cybozulib/include/cybozu/serializer.hpp +363 -0
  77. data/vendor/cybozulib/include/cybozu/sha1.hpp +209 -0
  78. data/vendor/cybozulib/include/cybozu/sha2.hpp +506 -0
  79. data/vendor/cybozulib/include/cybozu/siphash.hpp +105 -0
  80. data/vendor/cybozulib/include/cybozu/socket.hpp +785 -0
  81. data/vendor/cybozulib/include/cybozu/ssl.hpp +203 -0
  82. data/vendor/cybozulib/include/cybozu/stacktrace.hpp +291 -0
  83. data/vendor/cybozulib/include/cybozu/stream.hpp +269 -0
  84. data/vendor/cybozulib/include/cybozu/string.hpp +1746 -0
  85. data/vendor/cybozulib/include/cybozu/string_operation.hpp +365 -0
  86. data/vendor/cybozulib/include/cybozu/sucvector.hpp +378 -0
  87. data/vendor/cybozulib/include/cybozu/test.hpp +373 -0
  88. data/vendor/cybozulib/include/cybozu/thread.hpp +229 -0
  89. data/vendor/cybozulib/include/cybozu/time.hpp +281 -0
  90. data/vendor/cybozulib/include/cybozu/tls.hpp +115 -0
  91. data/vendor/cybozulib/include/cybozu/unordered_map.hpp +13 -0
  92. data/vendor/cybozulib/include/cybozu/unordered_set.hpp +13 -0
  93. data/vendor/cybozulib/include/cybozu/v128.hpp +376 -0
  94. data/vendor/cybozulib/include/cybozu/wavelet_matrix.hpp +345 -0
  95. data/vendor/cybozulib/include/cybozu/xorshift.hpp +189 -0
  96. data/vendor/cybozulib/include/cybozu/zlib.hpp +325 -0
  97. data/vendor/cybozulib/include/sais.hxx +364 -0
  98. data/vendor/cybozulib/misc/make_select8tbl.cpp +26 -0
  99. data/vendor/cybozulib/mk.bat +37 -0
  100. data/vendor/cybozulib/readme.md +29 -0
  101. data/vendor/cybozulib/release.props +12 -0
  102. data/vendor/cybozulib/sample/Makefile +30 -0
  103. data/vendor/cybozulib/sample/csucvector_smpl.cpp +42 -0
  104. data/vendor/cybozulib/sample/data/svd/org/test1.S +4 -0
  105. data/vendor/cybozulib/sample/data/svd/org/test1.U +4 -0
  106. data/vendor/cybozulib/sample/data/svd/org/test1.V +6 -0
  107. data/vendor/cybozulib/sample/data/svd/test1 +4 -0
  108. data/vendor/cybozulib/sample/data/svd/test2 +4 -0
  109. data/vendor/cybozulib/sample/desymbol.cpp +127 -0
  110. data/vendor/cybozulib/sample/exception_smpl.cpp +46 -0
  111. data/vendor/cybozulib/sample/fmindex_smpl.cpp +231 -0
  112. data/vendor/cybozulib/sample/log_smpl.cpp +19 -0
  113. data/vendor/cybozulib/sample/mecab_smpl.cpp +37 -0
  114. data/vendor/cybozulib/sample/option2_smpl.cpp +68 -0
  115. data/vendor/cybozulib/sample/option_smpl.cpp +42 -0
  116. data/vendor/cybozulib/sample/plsi_smpl.cpp +207 -0
  117. data/vendor/cybozulib/sample/proj/exception_smpl.vcproj +184 -0
  118. data/vendor/cybozulib/sample/proj/mecab_smpl.vcproj +184 -0
  119. data/vendor/cybozulib/sample/proj/ssl_smpl/ssl_smpl.vcxproj +85 -0
  120. data/vendor/cybozulib/sample/proj/ssl_smpl.vcproj +347 -0
  121. data/vendor/cybozulib/sample/proj/stacktrace_smpl/stacktrace_smpl.vcxproj +85 -0
  122. data/vendor/cybozulib/sample/proj/svd_smpl.vcproj +184 -0
  123. data/vendor/cybozulib/sample/quit_signal_handler.cpp +30 -0
  124. data/vendor/cybozulib/sample/serializer_smpl.cpp +196 -0
  125. data/vendor/cybozulib/sample/socket_smpl.cpp +82 -0
  126. data/vendor/cybozulib/sample/ssl_smpl.cpp +39 -0
  127. data/vendor/cybozulib/sample/stacktrace_smpl.cpp +52 -0
  128. data/vendor/cybozulib/sample/svd_bench_smpl.cpp +143 -0
  129. data/vendor/cybozulib/sample/svd_smpl.cpp +94 -0
  130. data/vendor/cybozulib/sample/wm_bench_smpl.cpp +182 -0
  131. data/vendor/cybozulib/sample/zlib_smpl.cpp +41 -0
  132. data/vendor/cybozulib/src/Makefile +8 -0
  133. data/vendor/cybozulib/src/base/Makefile +19 -0
  134. data/vendor/cybozulib/test/Makefile +12 -0
  135. data/vendor/cybozulib/test/base/Makefile +37 -0
  136. data/vendor/cybozulib/test/base/array_test.cpp +173 -0
  137. data/vendor/cybozulib/test/base/atoi_test.cpp +774 -0
  138. data/vendor/cybozulib/test/base/atomic_test.cpp +49 -0
  139. data/vendor/cybozulib/test/base/base64_test.cpp +113 -0
  140. data/vendor/cybozulib/test/base/bit_operation_test.cpp +134 -0
  141. data/vendor/cybozulib/test/base/bitvector_test.cpp +204 -0
  142. data/vendor/cybozulib/test/base/condition_variable_cs_test.cpp +92 -0
  143. data/vendor/cybozulib/test/base/condition_variable_test.cpp +88 -0
  144. data/vendor/cybozulib/test/base/config_test.cpp +236 -0
  145. data/vendor/cybozulib/test/base/crypto_test.cpp +122 -0
  146. data/vendor/cybozulib/test/base/csucvector_test.cpp +63 -0
  147. data/vendor/cybozulib/test/base/csv_test.cpp +182 -0
  148. data/vendor/cybozulib/test/base/data/a.xml +26 -0
  149. data/vendor/cybozulib/test/base/endian_test.cpp +56 -0
  150. data/vendor/cybozulib/test/base/env_test.cpp +22 -0
  151. data/vendor/cybozulib/test/base/event_test.cpp +41 -0
  152. data/vendor/cybozulib/test/base/file_test.cpp +233 -0
  153. data/vendor/cybozulib/test/base/fmindex_test.cpp +118 -0
  154. data/vendor/cybozulib/test/base/format_test.cpp +12 -0
  155. data/vendor/cybozulib/test/base/frequency_test.cpp +104 -0
  156. data/vendor/cybozulib/test/base/itoa_test.cpp +522 -0
  157. data/vendor/cybozulib/test/base/line_stream_test.cpp +208 -0
  158. data/vendor/cybozulib/test/base/mecab_test.cpp +41 -0
  159. data/vendor/cybozulib/test/base/minixml_test.cpp +103 -0
  160. data/vendor/cybozulib/test/base/mmap_test.cpp +15 -0
  161. data/vendor/cybozulib/test/base/option_test.cpp +487 -0
  162. data/vendor/cybozulib/test/base/parallel_test.cpp +48 -0
  163. data/vendor/cybozulib/test/base/proj/array_test/array_test.vcxproj +86 -0
  164. data/vendor/cybozulib/test/base/proj/atoi_test/atoi_test.vcxproj +86 -0
  165. data/vendor/cybozulib/test/base/proj/atomic_test/atomic_test.vcxproj +86 -0
  166. data/vendor/cybozulib/test/base/proj/base64_test/base64_test.vcxproj +86 -0
  167. data/vendor/cybozulib/test/base/proj/condition_variable_cs_test/condition_variable_cs_test.vcxproj +86 -0
  168. data/vendor/cybozulib/test/base/proj/condition_variable_test/condition_variable_test.vcxproj +86 -0
  169. data/vendor/cybozulib/test/base/proj/config_test/config_test.vcxproj +86 -0
  170. data/vendor/cybozulib/test/base/proj/csv_test/csv_test.vcxproj +86 -0
  171. data/vendor/cybozulib/test/base/proj/endian_test/endian_test.vcxproj +86 -0
  172. data/vendor/cybozulib/test/base/proj/env_test/env_test.vcxproj +86 -0
  173. data/vendor/cybozulib/test/base/proj/event_test/event_test.vcxproj +86 -0
  174. data/vendor/cybozulib/test/base/proj/file_test/file_test.vcxproj +86 -0
  175. data/vendor/cybozulib/test/base/proj/itoa_test/itoa_test.vcxproj +86 -0
  176. data/vendor/cybozulib/test/base/proj/mecab_test/mecab_test.vcxproj +88 -0
  177. data/vendor/cybozulib/test/base/proj/minixml_test/minixml_test.vcxproj +86 -0
  178. data/vendor/cybozulib/test/base/proj/mmap_test/mmap_test.vcxproj +86 -0
  179. data/vendor/cybozulib/test/base/proj/serializer_test/serializer_test.vcxproj +86 -0
  180. data/vendor/cybozulib/test/base/proj/sha1_test/sha1_test.vcxproj +86 -0
  181. data/vendor/cybozulib/test/base/proj/stream_test/stream_test.vcxproj +86 -0
  182. data/vendor/cybozulib/test/base/proj/string_operation_test/string_operation_test.vcxproj +86 -0
  183. data/vendor/cybozulib/test/base/proj/string_test/string_test.vcxproj +86 -0
  184. data/vendor/cybozulib/test/base/proj/thread_test/thread_test.vcxproj +86 -0
  185. data/vendor/cybozulib/test/base/proj/time_test/time_test.vcxproj +86 -0
  186. data/vendor/cybozulib/test/base/proj/tls_test/tls_test.vcxproj +86 -0
  187. data/vendor/cybozulib/test/base/proj/zlib_test/zlib_test.vcxproj +86 -0
  188. data/vendor/cybozulib/test/base/random_generator_test.cpp +28 -0
  189. data/vendor/cybozulib/test/base/regex_test.cpp +74 -0
  190. data/vendor/cybozulib/test/base/serializer_test.cpp +483 -0
  191. data/vendor/cybozulib/test/base/sha1_test.cpp +61 -0
  192. data/vendor/cybozulib/test/base/sha2_test.cpp +191 -0
  193. data/vendor/cybozulib/test/base/siphash_test.cpp +33 -0
  194. data/vendor/cybozulib/test/base/socket_test.cpp +76 -0
  195. data/vendor/cybozulib/test/base/stream_test.cpp +101 -0
  196. data/vendor/cybozulib/test/base/string_operation_test.cpp +340 -0
  197. data/vendor/cybozulib/test/base/string_test.cpp +1705 -0
  198. data/vendor/cybozulib/test/base/sucvector_test.cpp +312 -0
  199. data/vendor/cybozulib/test/base/thread_test.cpp +62 -0
  200. data/vendor/cybozulib/test/base/time_test.cpp +164 -0
  201. data/vendor/cybozulib/test/base/tls_test.cpp +50 -0
  202. data/vendor/cybozulib/test/base/wavelet_matrix_test.cpp +145 -0
  203. data/vendor/cybozulib/test/base/zlib_test.cpp +371 -0
  204. data/vendor/cybozulib/test/nlp/Makefile +27 -0
  205. data/vendor/cybozulib/test/nlp/proj/random_test.vcproj +184 -0
  206. data/vendor/cybozulib/test/nlp/proj/sparse_test.vcproj +184 -0
  207. data/vendor/cybozulib/test/nlp/proj/svd_test.vcproj +184 -0
  208. data/vendor/cybozulib/test/nlp/random_test.cpp +62 -0
  209. data/vendor/cybozulib/test/nlp/sparse_test.cpp +347 -0
  210. data/vendor/cybozulib/test/nlp/svd_test.cpp +234 -0
  211. data/vendor/cybozulib/test/nlp/top_score_test.cpp +40 -0
  212. data/vendor/cybozulib/tool/create_vcproj.py +186 -0
  213. data/vendor/cybozulib/tool/vcproj_tmpl.py +185 -0
  214. data/vendor/msoffice/COPYRIGHT +27 -0
  215. data/vendor/msoffice/Makefile +29 -0
  216. data/vendor/msoffice/bin/64/msoc.dll +0 -0
  217. data/vendor/msoffice/bin/64/msocsample.exe +0 -0
  218. data/vendor/msoffice/bin/64/msoffice-crypt.exe +0 -0
  219. data/vendor/msoffice/bin/msoc.dll +0 -0
  220. data/vendor/msoffice/bin/msocsample.exe +0 -0
  221. data/vendor/msoffice/bin/msoffice-crypt.exe +0 -0
  222. data/vendor/msoffice/common.mk +71 -0
  223. data/vendor/msoffice/common.props +26 -0
  224. data/vendor/msoffice/debug.props +14 -0
  225. data/vendor/msoffice/include/attack.hpp +211 -0
  226. data/vendor/msoffice/include/cfb.hpp +777 -0
  227. data/vendor/msoffice/include/crypto_util.hpp +450 -0
  228. data/vendor/msoffice/include/custom_sha1.hpp +342 -0
  229. data/vendor/msoffice/include/decode.hpp +240 -0
  230. data/vendor/msoffice/include/encode.hpp +221 -0
  231. data/vendor/msoffice/include/make_dataspace.hpp +316 -0
  232. data/vendor/msoffice/include/msoc.h +129 -0
  233. data/vendor/msoffice/include/resource.hpp +7 -0
  234. data/vendor/msoffice/include/standard_encryption.hpp +145 -0
  235. data/vendor/msoffice/include/uint32vec.hpp +179 -0
  236. data/vendor/msoffice/include/util.hpp +212 -0
  237. data/vendor/msoffice/lib/.emptydir +0 -0
  238. data/vendor/msoffice/misc/decrypt-xls.vbs +46 -0
  239. data/vendor/msoffice/mk.bat +1 -0
  240. data/vendor/msoffice/mkdll.bat +3 -0
  241. data/vendor/msoffice/msoc.def +13 -0
  242. data/vendor/msoffice/msocsample.py +178 -0
  243. data/vendor/msoffice/msoffice12.sln +31 -0
  244. data/vendor/msoffice/readme.md +110 -0
  245. data/vendor/msoffice/release.props +28 -0
  246. data/vendor/msoffice/src/Makefile +19 -0
  247. data/vendor/msoffice/src/attack.cpp +124 -0
  248. data/vendor/msoffice/src/cfb_test.cpp +77 -0
  249. data/vendor/msoffice/src/minisample.c +54 -0
  250. data/vendor/msoffice/src/msocdll.cpp +276 -0
  251. data/vendor/msoffice/src/msocsample.c +136 -0
  252. data/vendor/msoffice/src/msoffice-crypt.cpp +219 -0
  253. data/vendor/msoffice/src/proj/attack/attack.vcxproj +88 -0
  254. data/vendor/msoffice/src/proj/main/msoffice-crypt.vcxproj +88 -0
  255. data/vendor/msoffice/src/sha1.cpp +234 -0
  256. data/vendor/msoffice/test/Makefile +20 -0
  257. data/vendor/msoffice/test/cfb_test.cpp +74 -0
  258. data/vendor/msoffice/test/hash_test.cpp +59 -0
  259. data/vendor/msoffice/test/proj/cfb/cfb_test.vcxproj +90 -0
  260. data/vendor/msoffice/test/proj/hash/hash_test.vcxproj +90 -0
  261. data/vendor/msoffice/test/sampl.bat +8 -0
  262. data/vendor/msoffice/test_all.py +46 -0
  263. data/vendor/update +4 -0
  264. metadata +351 -0
@@ -0,0 +1,294 @@
1
+ #pragma once
2
+ /**
3
+ @file
4
+ @brief csv reader and write class
5
+
6
+ @author MITSUNARI Shigeo(@herumi)
7
+ */
8
+
9
+ #include <string>
10
+ #include <list>
11
+ #include <fstream>
12
+ #include <cybozu/stream.hpp>
13
+
14
+ namespace cybozu {
15
+
16
+ namespace csv_local {
17
+
18
+ inline bool isValidSeparator(char c)
19
+ {
20
+ return c == ',' || c == '\t' || c == ';' || c == ' ';
21
+ }
22
+
23
+ } // csv_local
24
+ /**
25
+ CSV reader class
26
+ InputStream must have ssize_t read(char *str, size_t size);
27
+ */
28
+ template<class InputStream, size_t MAX_LINE_SIZE = 10 * 1024 * 1024>
29
+ class CsvReaderT {
30
+ CsvReaderT(const CsvReaderT&);
31
+ void operator=(const CsvReaderT&);
32
+ typedef cybozu::InputStreamTag<InputStream> In;
33
+ public:
34
+ /**
35
+ @param is [in] input stream
36
+ @param sep [in] separator character(comma, tab, semicolon, space)
37
+ */
38
+ CsvReaderT(InputStream& is, char sep = ',')
39
+ : sep_(sep)
40
+ , is_(is)
41
+ , line_(0)
42
+ , lineSize_(0)
43
+ , pos_(0)
44
+ , bufSize_(0)
45
+ , eof_(false)
46
+ {
47
+ if (!csv_local::isValidSeparator(sep)) {
48
+ throw cybozu::Exception("csv:CsvReaderT:invalid separator") << sep;
49
+ }
50
+ }
51
+ /**
52
+ get one line from InputStream and push_back() into out
53
+ @return false if no data(eof)
54
+ @note out must have push_back(std::string)
55
+ @note string must be UTF-8
56
+ ignore all \x0d
57
+ */
58
+ template<class Container>
59
+ bool read(Container& out)
60
+ {
61
+ if (eof_) return false;
62
+ line_++;
63
+ out.clear();
64
+ enum {
65
+ Top,
66
+ InQuote,
67
+ SearchSep,
68
+ NeedSepOrQuote
69
+ } state = Top;
70
+
71
+ const char CR = '\x0d';
72
+ const char LF = '\x0a';
73
+ const char quote = '"';
74
+
75
+ std::string str;
76
+ for (;;) {
77
+ int c = my_getchar();
78
+ if (c == EOF && str.empty()) return false;
79
+ if (c == CR) continue;
80
+ switch (state) {
81
+ case Top:
82
+ if (c == EOF || c == LF) {
83
+ if (!str.empty()) {
84
+ appendAndClear(out, str);
85
+ }
86
+ return true;
87
+ }
88
+ if (c == quote) {
89
+ state = InQuote;
90
+ } else if (c == sep_) {
91
+ appendAndClear(out, str);
92
+ } else {
93
+ addChar(str, c);
94
+ state = SearchSep;
95
+ }
96
+ break;
97
+ case InQuote:
98
+ if (c == EOF) {
99
+ throw cybozu::Exception("csv:read:quote is necessary") << line_ << str;
100
+ }
101
+ if (c == quote) {
102
+ state = NeedSepOrQuote;
103
+ } else {
104
+ addChar(str, c);
105
+ }
106
+ break;
107
+ case NeedSepOrQuote:
108
+ if (c == EOF || c == LF) {
109
+ appendAndClear(out, str);
110
+ return true;
111
+ }
112
+ if (c == quote) {
113
+ addChar(str, quote);
114
+ state = InQuote;
115
+ } else if (c == sep_) {
116
+ appendAndClear(out, str);
117
+ state = Top;
118
+ } else {
119
+ throw cybozu::Exception("csv:read:bad character after quote") << line_ << str << c;
120
+ }
121
+ break;
122
+ case SearchSep:
123
+ default:
124
+ if (c == EOF || c == LF) {
125
+ appendAndClear(out, str);
126
+ return true;
127
+ }
128
+ if (c == sep_) {
129
+ appendAndClear(out, str);
130
+ state = Top;
131
+ } else {
132
+ addChar(str, c);
133
+ }
134
+ break;
135
+ }
136
+ }
137
+ }
138
+ private:
139
+ void addChar(std::string& str, int c)
140
+ {
141
+ str += static_cast<char>(c);
142
+ lineSize_++;
143
+ if (lineSize_ == MAX_LINE_SIZE) {
144
+ throw cybozu::Exception("csv:addChar:too large size") << line_ << str << MAX_LINE_SIZE;
145
+ }
146
+ }
147
+ template<class Container>
148
+ void appendAndClear(Container& out, std::string& str)
149
+ {
150
+ out.push_back(str);
151
+ str.clear();
152
+ }
153
+ int my_getchar()
154
+ {
155
+ if (pos_ < bufSize_) {
156
+ return buf_[pos_++];
157
+ }
158
+ bufSize_ = cybozu::readSome(buf_, sizeof(buf_), is_);
159
+ if (bufSize_ > 0) {
160
+ pos_ = 1;
161
+ return buf_[0];
162
+ } else {
163
+ pos_ = 0;
164
+ eof_ = true;
165
+ return EOF;
166
+ }
167
+ }
168
+ char sep_;
169
+ InputStream& is_;
170
+ size_t line_;
171
+ size_t lineSize_;
172
+ char buf_[2048];
173
+ size_t pos_;
174
+ size_t bufSize_;
175
+ bool eof_;
176
+ };
177
+
178
+ /**
179
+ CSV writer class
180
+ OutputStream must have ssize_t write(const char *str, size_t size);
181
+ */
182
+ template<class OutputStream>
183
+ class CsvWriterT {
184
+ CsvWriterT(const CsvWriterT&);
185
+ void operator=(const CsvWriterT&);
186
+ typedef cybozu::OutputStreamTag<OutputStream> Out;
187
+ public:
188
+ /**
189
+ @param os [in] output stream
190
+ @param sep [in] separator character(comma, tab, semicolon, space)
191
+ */
192
+ CsvWriterT(OutputStream& os, char sep = ',')
193
+ : os_(os)
194
+ , sep_(sep)
195
+ {
196
+ if (!csv_local::isValidSeparator(sep)) {
197
+ throw cybozu::Exception("csv:CsvWriteT:inavlid separator") << sep;
198
+ }
199
+ }
200
+ /**
201
+ make one line from [begin, end) and write it
202
+ @note type of *begin must be std::string
203
+ @note string must be UTF-8
204
+ */
205
+ template<class Iterator>
206
+ void write(Iterator begin, Iterator end)
207
+ {
208
+ bool isFirst = true;
209
+ while (begin != end) {
210
+ if (isFirst) {
211
+ isFirst = false;
212
+ } else {
213
+ appendSeparator();
214
+ }
215
+ append(*begin);
216
+ ++begin;
217
+ }
218
+ appendEndLine();
219
+ }
220
+ private:
221
+ void append(const std::string& str)
222
+ {
223
+ std::string out = "\"";
224
+ for (size_t i = 0, n = str.size(); i < n; i++) {
225
+ char c = str[i];
226
+ if (c == '"') {
227
+ out += '"';
228
+ }
229
+ out += c;
230
+ }
231
+ out += "\"";
232
+ writeToStream(&out[0], out.size());
233
+ }
234
+ void appendSeparator()
235
+ {
236
+ writeToStream(&sep_, 1);
237
+ }
238
+ void appendEndLine()
239
+ {
240
+ writeToStream("\x0d\x0a", 2);
241
+ }
242
+ void writeToStream(const char *str, size_t size)
243
+ {
244
+ Out::write(os_, str, size);
245
+ }
246
+ OutputStream& os_;
247
+ char sep_;
248
+ };
249
+
250
+ class CsvReader {
251
+ std::ifstream ifs_;
252
+ cybozu::CsvReaderT<std::ifstream> csv_;
253
+ public:
254
+ CsvReader(const std::string& name, char sep = ',')
255
+ : ifs_(name.c_str(), std::ios::binary)
256
+ , csv_(ifs_, sep)
257
+ {
258
+ }
259
+ /**
260
+ get one line from InputStream and push_back() into out
261
+ @return false if no data(eof)
262
+ @note out must have push_back(std::string)
263
+ @note string must be UTF-8
264
+ ignore all \x0d
265
+ */
266
+ template<class Container>
267
+ bool read(Container& out)
268
+ {
269
+ return csv_.read(out);
270
+ }
271
+ };
272
+
273
+ class CsvWriter {
274
+ std::ofstream ofs_;
275
+ cybozu::CsvWriterT<std::ofstream> csv_;
276
+ public:
277
+ CsvWriter(const std::string& name, char sep = ',')
278
+ : ofs_(name.c_str(), std::ios::binary)
279
+ , csv_(ofs_, sep)
280
+ {
281
+ }
282
+ /**
283
+ make one line from [begin, end) and write it
284
+ @note type of *begin must be std::string
285
+ @note string must be UTF-8
286
+ */
287
+ template<class Iterator>
288
+ void write(Iterator begin, Iterator end)
289
+ {
290
+ csv_.write(begin, end);
291
+ }
292
+ };
293
+
294
+ } // cybozu
@@ -0,0 +1,27 @@
1
+ #pragma once
2
+ /**
3
+ @file
4
+ @brief typedef of some STL data type
5
+
6
+ @author MITSUNARI Shigeo(@herumi)
7
+ */
8
+ #include <string>
9
+ #include <map>
10
+ #include <set>
11
+ #include <vector>
12
+ #include <cybozu/inttype.hpp>
13
+
14
+ namespace cybozu {
15
+
16
+ typedef std::vector<int> IntVec;
17
+ typedef std::vector<uint32_t> Uint32Vec;
18
+ typedef std::vector<std::string> StrVec;
19
+ typedef std::map<int, int> Int2Int;
20
+ typedef std::map<int, std::string> Int2Str;
21
+ typedef std::map<std::string, int> Str2Int;
22
+ typedef std::map<std::string, std::string> Str2Str;
23
+ typedef std::set<int> IntSet;
24
+ typedef std::set<uint32_t> Uint32Set;
25
+ typedef std::set<std::string> StrSet;
26
+
27
+ } // cybozu
@@ -0,0 +1,224 @@
1
+ #pragma once
2
+
3
+ /**
4
+ @file
5
+ @brief deal with big and little endian
6
+
7
+ @author MITSUNARI Shigeo(@herumi)
8
+ */
9
+ #include <cybozu/inttype.hpp>
10
+ #include <string.h>
11
+ #include <stdlib.h>
12
+ #include <stdio.h>
13
+
14
+ namespace cybozu {
15
+
16
+ #ifdef _MSC_VER
17
+ inline uint16_t byteSwap(uint16_t x) { return _byteswap_ushort(x); }
18
+ inline uint32_t byteSwap(uint32_t x) { return _byteswap_ulong(x); }
19
+ inline uint64_t byteSwap(uint64_t x) { return _byteswap_uint64(x); }
20
+ #else
21
+ #if (((__GNUC__) << 16) + (__GNUC_MINOR__)) >= ((4 << 16) + 8)
22
+ inline uint16_t byteSwap(uint16_t x) { return __builtin_bswap16(x); }
23
+ #else
24
+ inline uint16_t byteSwap(uint16_t x) { return (x >> 8) | (x << 8); }
25
+ #endif
26
+ inline uint32_t byteSwap(uint32_t x) { return __builtin_bswap32(x); }
27
+ inline uint64_t byteSwap(uint64_t x) { return __builtin_bswap64(x); }
28
+ #endif
29
+
30
+ /**
31
+ get 16bit integer as little endian
32
+ @param src [in] pointer
33
+ */
34
+ inline uint16_t Get16bitAsLE(const void *src)
35
+ {
36
+ #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
37
+ uint16_t x;
38
+ memcpy(&x, src, sizeof(x));
39
+ return x;
40
+ #else
41
+ const uint8_t *p = static_cast<const uint8_t *>(src);
42
+ return p[0] | (p[1] << 8);
43
+ #endif
44
+ }
45
+
46
+ /**
47
+ get 32bit integer as little endian
48
+ @param src [in] pointer
49
+ */
50
+ inline uint32_t Get32bitAsLE(const void *src)
51
+ {
52
+ #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
53
+ uint32_t x;
54
+ memcpy(&x, src, sizeof(x));
55
+ return x;
56
+ #else
57
+ const uint8_t *p = static_cast<const uint8_t *>(src);
58
+ return Get16bitAsLE(p) | (static_cast<uint32_t>(Get16bitAsLE(p + 2)) << 16);
59
+ #endif
60
+ }
61
+
62
+ /**
63
+ get 64bit integer as little endian
64
+ @param src [in] pointer
65
+ */
66
+ inline uint64_t Get64bitAsLE(const void *src)
67
+ {
68
+ #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
69
+ uint64_t x;
70
+ memcpy(&x, src, sizeof(x));
71
+ return x;
72
+ #else
73
+ const uint8_t *p = static_cast<const uint8_t *>(src);
74
+ return Get32bitAsLE(p) | (static_cast<uint64_t>(Get32bitAsLE(p + 4)) << 32);
75
+ #endif
76
+ }
77
+
78
+ /**
79
+ get 16bit integer as bit endian
80
+ @param src [in] pointer
81
+ */
82
+ inline uint16_t Get16bitAsBE(const void *src)
83
+ {
84
+ #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
85
+ uint16_t x;
86
+ memcpy(&x, src, sizeof(x));
87
+ return byteSwap(x);
88
+ #else
89
+ const uint8_t *p = static_cast<const uint8_t *>(src);
90
+ return p[1] | (p[0] << 8);
91
+ #endif
92
+ }
93
+
94
+ /**
95
+ get 32bit integer as bit endian
96
+ @param src [in] pointer
97
+ */
98
+ inline uint32_t Get32bitAsBE(const void *src)
99
+ {
100
+ #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
101
+ uint32_t x;
102
+ memcpy(&x, src, sizeof(x));
103
+ return byteSwap(x);
104
+ #else
105
+ const uint8_t *p = static_cast<const uint8_t *>(src);
106
+ return Get16bitAsBE(p + 2) | (static_cast<uint32_t>(Get16bitAsBE(p)) << 16);
107
+ #endif
108
+ }
109
+
110
+ /**
111
+ get 64bit integer as big endian
112
+ @param src [in] pointer
113
+ */
114
+ inline uint64_t Get64bitAsBE(const void *src)
115
+ {
116
+ #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
117
+ uint64_t x;
118
+ memcpy(&x, src, sizeof(x));
119
+ return byteSwap(x);
120
+ #else
121
+ const uint8_t *p = static_cast<const uint8_t *>(src);
122
+ return Get32bitAsBE(p + 4) | (static_cast<uint64_t>(Get32bitAsBE(p)) << 32);
123
+ #endif
124
+ }
125
+
126
+ /**
127
+ set 16bit integer as little endian
128
+ @param src [out] pointer
129
+ @param x [in] integer
130
+ */
131
+ inline void Set16bitAsLE(void *src, uint16_t x)
132
+ {
133
+ #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
134
+ memcpy(src, &x, sizeof(x));
135
+ #else
136
+ uint8_t *p = static_cast<uint8_t *>(src);
137
+ p[0] = static_cast<uint8_t>(x);
138
+ p[1] = static_cast<uint8_t>(x >> 8);
139
+ #endif
140
+ }
141
+ /**
142
+ set 32bit integer as little endian
143
+ @param src [out] pointer
144
+ @param x [in] integer
145
+ */
146
+ inline void Set32bitAsLE(void *src, uint32_t x)
147
+ {
148
+ #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
149
+ memcpy(src, &x, sizeof(x));
150
+ #else
151
+ uint8_t *p = static_cast<uint8_t *>(src);
152
+ p[0] = static_cast<uint8_t>(x);
153
+ p[1] = static_cast<uint8_t>(x >> 8);
154
+ p[2] = static_cast<uint8_t>(x >> 16);
155
+ p[3] = static_cast<uint8_t>(x >> 24);
156
+ #endif
157
+ }
158
+ /**
159
+ set 64bit integer as little endian
160
+ @param src [out] pointer
161
+ @param x [in] integer
162
+ */
163
+ inline void Set64bitAsLE(void *src, uint64_t x)
164
+ {
165
+ #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
166
+ memcpy(src, &x, sizeof(x));
167
+ #else
168
+ uint8_t *p = static_cast<uint8_t *>(src);
169
+ Set32bitAsLE(p, static_cast<uint32_t>(x));
170
+ Set32bitAsLE(p + 4, static_cast<uint32_t>(x >> 32));
171
+ #endif
172
+ }
173
+ /**
174
+ set 16bit integer as big endian
175
+ @param src [out] pointer
176
+ @param x [in] integer
177
+ */
178
+ inline void Set16bitAsBE(void *src, uint16_t x)
179
+ {
180
+ #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
181
+ x = byteSwap(x);
182
+ memcpy(src, &x, sizeof(x));
183
+ #else
184
+ uint8_t *p = static_cast<uint8_t *>(src);
185
+ p[0] = static_cast<uint8_t>(x >> 8);
186
+ p[1] = static_cast<uint8_t>(x);
187
+ #endif
188
+ }
189
+ /**
190
+ set 32bit integer as big endian
191
+ @param src [out] pointer
192
+ @param x [in] integer
193
+ */
194
+ inline void Set32bitAsBE(void *src, uint32_t x)
195
+ {
196
+ #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
197
+ x = byteSwap(x);
198
+ memcpy(src, &x, sizeof(x));
199
+ #else
200
+ uint8_t *p = static_cast<uint8_t *>(src);
201
+ p[0] = static_cast<uint8_t>(x >> 24);
202
+ p[1] = static_cast<uint8_t>(x >> 16);
203
+ p[2] = static_cast<uint8_t>(x >> 8);
204
+ p[3] = static_cast<uint8_t>(x);
205
+ #endif
206
+ }
207
+ /**
208
+ set 64bit integer as big endian
209
+ @param src [out] pointer
210
+ @param x [in] integer
211
+ */
212
+ inline void Set64bitAsBE(void *src, uint64_t x)
213
+ {
214
+ #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE
215
+ x = byteSwap(x);
216
+ memcpy(src, &x, sizeof(x));
217
+ #else
218
+ uint8_t *p = static_cast<uint8_t *>(src);
219
+ Set32bitAsBE(p, static_cast<uint32_t>(x >> 32));
220
+ Set32bitAsBE(p + 4, static_cast<uint32_t>(x));
221
+ #endif
222
+ }
223
+
224
+ } // cybozu
@@ -0,0 +1,63 @@
1
+ #pragma once
2
+
3
+ /**
4
+ @file
5
+ @brief get env function
6
+
7
+ @author MITSUNARI Shigeo(@herumi)
8
+ */
9
+ #include <cybozu/exception.hpp>
10
+ #include <stdlib.h>
11
+
12
+ namespace cybozu {
13
+
14
+ inline bool QueryEnv(std::string& value, const std::string& key)
15
+ {
16
+ #ifdef _WIN32
17
+ char buf[4096];
18
+ size_t size;
19
+ if (getenv_s(&size, buf, key.c_str()) == 0) {
20
+ if (size > 0) {
21
+ value.assign(buf, size - 1);
22
+ return true;
23
+ }
24
+ }
25
+ return false;
26
+ #else
27
+ const char *p = getenv(key.c_str());
28
+ if (p) {
29
+ value.assign(p);
30
+ return true;
31
+ }
32
+ return false;
33
+ #endif
34
+ }
35
+
36
+ /**
37
+ get env
38
+ @param key [in] key name
39
+ @return value
40
+ @note throw exception if none
41
+ */
42
+ inline std::string GetEnv(const std::string& key)
43
+ {
44
+ std::string value;
45
+ if (!QueryEnv(value, key)) throw cybozu::Exception("env:GetEnv") << key;
46
+ return value;
47
+ }
48
+
49
+ /**
50
+ get env
51
+ @param key [in] key name
52
+ @param defaultValue [in] default value
53
+ @return value
54
+ @note return defaultValue if none
55
+ */
56
+ inline std::string GetEnv(const std::string& key, const std::string& defaultValue)
57
+ {
58
+ std::string value;
59
+ if (!QueryEnv(value, key)) return defaultValue;
60
+ return value;
61
+ }
62
+
63
+ } // cybozu
@@ -0,0 +1,122 @@
1
+ #pragma once
2
+ /**
3
+ @file
4
+ @brief event class
5
+
6
+ @author MITSUNARI Shigeo(@herumi)
7
+ */
8
+
9
+ #ifdef _WIN32
10
+ #ifndef WIN32_LEAN_AND_MEAN
11
+ #define WIN32_LEAN_AND_MEAN
12
+ #endif
13
+ #include <windows.h>
14
+ #else
15
+ #include <unistd.h>
16
+ #endif
17
+ #include <cybozu/exception.hpp>
18
+ #if CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11
19
+ #include <mutex>
20
+ #include <condition_variable>
21
+ #endif
22
+
23
+ namespace cybozu {
24
+
25
+ struct EventException : cybozu::Exception {
26
+ EventException() : cybozu::Exception("event") { }
27
+ };
28
+
29
+ #ifdef _WIN32
30
+ class Event {
31
+ HANDLE event_;
32
+ Event(const Event&);
33
+ void operator=(const Event&);
34
+ public:
35
+ Event()
36
+ {
37
+ event_ = ::CreateEvent(NULL, FALSE, FALSE, NULL);
38
+ if (event_ == 0) {
39
+ cybozu::EventException e;
40
+ e << "CreateEvent";
41
+ throw e;
42
+ }
43
+ }
44
+ ~Event()
45
+ {
46
+ ::CloseHandle(event_);
47
+ }
48
+ void wakeup()
49
+ {
50
+ ::SetEvent(event_);
51
+ }
52
+ void wait()
53
+ {
54
+ DWORD msec = INFINITE;
55
+ if (WaitForSingleObject(event_, msec) != WAIT_OBJECT_0) {
56
+ cybozu::EventException e;
57
+ e << "wait";
58
+ throw e;
59
+ }
60
+ }
61
+ };
62
+ #else
63
+ #if CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11
64
+
65
+ class Event {
66
+ bool isSignaled_;
67
+ std::mutex m_;
68
+ std::condition_variable cv_;
69
+ Event(const Event&) = delete;
70
+ void operator=(const Event&) = delete;
71
+ public:
72
+ Event() : isSignaled_(false) {}
73
+ void wakeup()
74
+ {
75
+ std::unique_lock<std::mutex> lk(m_);
76
+ isSignaled_ = true;
77
+ cv_.notify_one();
78
+ }
79
+ void wait()
80
+ {
81
+ std::unique_lock<std::mutex> lk(m_);
82
+ cv_.wait(lk, [this] { return isSignaled_; });
83
+ isSignaled_ = false;
84
+ }
85
+ };
86
+
87
+ #else
88
+ class Event {
89
+ int pipefd_[2];
90
+ Event(const Event&);
91
+ void operator=(const Event&);
92
+ public:
93
+ Event()
94
+ {
95
+ if (::pipe(pipefd_) < 0) {
96
+ cybozu::EventException e;
97
+ e << "pipe";
98
+ throw e;
99
+ }
100
+ }
101
+ ~Event()
102
+ {
103
+ ::close(pipefd_[0]);
104
+ ::close(pipefd_[1]);
105
+ }
106
+ void wakeup()
107
+ {
108
+ char c = 'a';
109
+ ssize_t size = ::write(pipefd_[1], &c, 1);
110
+ cybozu::disable_warning_unused_variable(size);
111
+ }
112
+ void wait()
113
+ {
114
+ char c;
115
+ ssize_t size = ::read(pipefd_[0], &c, 1);
116
+ cybozu::disable_warning_unused_variable(size);
117
+ }
118
+ };
119
+ #endif
120
+ #endif
121
+
122
+ } // cybozu