cell_iterator.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // Copyright (c) 2014-2021 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #include <xlnt/cell/cell.hpp>
  24. #include <xlnt/cell/cell_reference.hpp>
  25. #include <xlnt/worksheet/cell_iterator.hpp>
  26. #include <xlnt/worksheet/major_order.hpp>
  27. namespace xlnt {
  28. cell_iterator::cell_iterator(worksheet ws, const cell_reference &cursor,
  29. const range_reference &bounds, major_order order, bool skip_null, bool wrap)
  30. : skip_null_(skip_null),
  31. wrap_(wrap),
  32. order_(order),
  33. ws_(ws),
  34. cursor_(cursor),
  35. bounds_(bounds)
  36. {
  37. if (skip_null && !ws.has_cell(cursor_))
  38. {
  39. (*this)++; // move to the next non-empty cell or one past the end if none exists
  40. }
  41. }
  42. const_cell_iterator::const_cell_iterator(worksheet ws, const cell_reference &cursor,
  43. const range_reference &bounds, major_order order, bool skip_null, bool wrap)
  44. : skip_null_(skip_null),
  45. wrap_(wrap),
  46. order_(order),
  47. ws_(ws),
  48. cursor_(cursor),
  49. bounds_(bounds)
  50. {
  51. if (skip_null && !ws.has_cell(cursor_))
  52. {
  53. (*this)++; // move to the next non-empty cell or one past the end if none exists
  54. }
  55. }
  56. bool cell_iterator::operator==(const cell_iterator &other) const
  57. {
  58. return ws_ == other.ws_
  59. && cursor_ == other.cursor_
  60. && bounds_ == other.bounds_
  61. && order_ == other.order_
  62. && skip_null_ == other.skip_null_
  63. && wrap_ == other.wrap_;
  64. }
  65. bool const_cell_iterator::operator==(const const_cell_iterator &other) const
  66. {
  67. return ws_ == other.ws_
  68. && cursor_ == other.cursor_
  69. && bounds_ == other.bounds_
  70. && order_ == other.order_
  71. && skip_null_ == other.skip_null_
  72. && wrap_ == other.wrap_;
  73. }
  74. bool cell_iterator::operator!=(const cell_iterator &other) const
  75. {
  76. return !(*this == other);
  77. }
  78. bool const_cell_iterator::operator!=(const const_cell_iterator &other) const
  79. {
  80. return !(*this == other);
  81. }
  82. cell_iterator &cell_iterator::operator--()
  83. {
  84. if (order_ == major_order::row)
  85. {
  86. if (cursor_.column() > bounds_.top_left().column())
  87. {
  88. cursor_.column_index(cursor_.column_index() - 1);
  89. }
  90. if (skip_null_)
  91. {
  92. while (!ws_.has_cell(cursor_) && cursor_.column() > bounds_.top_left().column())
  93. {
  94. cursor_.column_index(cursor_.column_index() - 1);
  95. }
  96. }
  97. }
  98. else
  99. {
  100. if (cursor_.row() > bounds_.top_left().row())
  101. {
  102. cursor_.row(cursor_.row() - 1);
  103. }
  104. if (skip_null_)
  105. {
  106. while (!ws_.has_cell(cursor_) && cursor_.row() > bounds_.top_left().row())
  107. {
  108. cursor_.row(cursor_.row() - 1);
  109. }
  110. }
  111. }
  112. return *this;
  113. }
  114. const_cell_iterator &const_cell_iterator::operator--()
  115. {
  116. if (order_ == major_order::row)
  117. {
  118. if (cursor_.column() > bounds_.top_left().column())
  119. {
  120. cursor_.column_index(cursor_.column_index() - 1);
  121. }
  122. if (skip_null_)
  123. {
  124. while (!ws_.has_cell(cursor_) && cursor_.column() > bounds_.top_left().column())
  125. {
  126. cursor_.column_index(cursor_.column_index() - 1);
  127. }
  128. }
  129. }
  130. else
  131. {
  132. if (cursor_.row() > bounds_.top_left().row())
  133. {
  134. cursor_.row(cursor_.row() - 1);
  135. }
  136. if (skip_null_)
  137. {
  138. while (!ws_.has_cell(cursor_) && cursor_.row() > bounds_.top_left().row())
  139. {
  140. cursor_.row(cursor_.row() - 1);
  141. }
  142. }
  143. }
  144. return *this;
  145. }
  146. cell_iterator cell_iterator::operator--(int)
  147. {
  148. cell_iterator old = *this;
  149. --*this;
  150. return old;
  151. }
  152. const_cell_iterator const_cell_iterator::operator--(int)
  153. {
  154. const_cell_iterator old = *this;
  155. --*this;
  156. return old;
  157. }
  158. cell_iterator &cell_iterator::operator++()
  159. {
  160. if (order_ == major_order::row)
  161. {
  162. if (cursor_.column() <= bounds_.bottom_right().column())
  163. {
  164. cursor_.column_index(cursor_.column_index() + 1);
  165. }
  166. if (skip_null_)
  167. {
  168. while (!ws_.has_cell(cursor_) && cursor_.column() <= bounds_.bottom_right().column())
  169. {
  170. cursor_.column_index(cursor_.column_index() + 1);
  171. }
  172. }
  173. }
  174. else
  175. {
  176. if (cursor_.row() <= bounds_.bottom_right().row())
  177. {
  178. cursor_.row(cursor_.row() + 1);
  179. }
  180. if (skip_null_)
  181. {
  182. while (!ws_.has_cell(cursor_) && cursor_.row() <= bounds_.bottom_right().row())
  183. {
  184. cursor_.row(cursor_.row() + 1);
  185. }
  186. }
  187. }
  188. return *this;
  189. }
  190. const_cell_iterator &const_cell_iterator::operator++()
  191. {
  192. if (order_ == major_order::row)
  193. {
  194. if (cursor_.column() <= bounds_.bottom_right().column())
  195. {
  196. cursor_.column_index(cursor_.column_index() + 1);
  197. }
  198. if (skip_null_)
  199. {
  200. while (!ws_.has_cell(cursor_) && cursor_.column() <= bounds_.bottom_right().column())
  201. {
  202. cursor_.column_index(cursor_.column_index() + 1);
  203. }
  204. }
  205. }
  206. else
  207. {
  208. if (cursor_.row() <= bounds_.bottom_right().row())
  209. {
  210. cursor_.row(cursor_.row() + 1);
  211. }
  212. if (skip_null_)
  213. {
  214. while (!ws_.has_cell(cursor_) && cursor_.row() <= bounds_.bottom_right().row())
  215. {
  216. cursor_.row(cursor_.row() + 1);
  217. }
  218. }
  219. }
  220. return *this;
  221. }
  222. cell_iterator cell_iterator::operator++(int)
  223. {
  224. cell_iterator old = *this;
  225. ++*this;
  226. return old;
  227. }
  228. const_cell_iterator const_cell_iterator::operator++(int)
  229. {
  230. const_cell_iterator old = *this;
  231. ++*this;
  232. return old;
  233. }
  234. cell_iterator::reference cell_iterator::operator*()
  235. {
  236. return ws_.cell(cursor_);
  237. }
  238. const cell_iterator::reference cell_iterator::operator*() const
  239. {
  240. return ws_.cell(cursor_);
  241. }
  242. const const_cell_iterator::reference const_cell_iterator::operator*() const
  243. {
  244. return ws_.cell(cursor_);
  245. }
  246. bool cell_iterator::has_value() const{
  247. return ws_.has_cell(cursor_);
  248. }
  249. bool const_cell_iterator::has_value() const{
  250. return ws_.has_cell(cursor_);
  251. }
  252. } // namespace xlnt