vsg 1.1.3
VulkanSceneGraph library
Loading...
Searching...
No Matches
read_line.h
1/* <editor-fold desc="MIT License">
2
3Copyright(c) 2021 Robert Osfield
4
5Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
7The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
9THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10
11</editor-fold> */
12
13#include <istream>
14
15namespace vsg
16{
17
19 template<typename T>
20 uint32_t read_line(std::istream& sstr, T* values, uint32_t maxSize, bool read_to_end_line = true)
21 {
22 uint32_t i = 0;
23 auto c = sstr.peek();
24 while (i < maxSize)
25 {
26 // skip leading spaces
27 while (c == ' ' || c == '\t')
28 {
29 sstr.ignore();
30 c = sstr.peek();
31 }
32
33 if (c == '#') break;
34
35 if (read_to_end_line)
36 {
37 if (c == '\n')
38 {
39 sstr.ignore();
40 return i;
41 }
42 else if (c == '\r')
43 {
44 sstr.ignore();
45 c = sstr.peek();
46 if (c == '\n')
47 {
48 sstr.ignore();
49 }
50 return i;
51 }
52 }
53
54 if (!(sstr >> values[i])) return i;
55
56 ++i;
57
58 // skip trailing spaces/tabs
59 c = sstr.peek();
60
61 while (c == ' ' || c == '\t')
62 {
63 sstr.ignore();
64 c = sstr.peek();
65 }
66
67 // skip a single comma if we have one
68 if (c == ',')
69 {
70 sstr.ignore();
71 c = sstr.peek();
72 }
73 }
74
75 if (read_to_end_line)
76 {
77 while (sstr && (c != '\n') && (c != '\r'))
78 {
79 sstr.ignore();
80 c = sstr.peek();
81 }
82
83 if (c == '\n')
84 {
85 sstr.ignore();
86 }
87 else if (c == '\r')
88 {
89 sstr.ignore();
90 c = sstr.peek();
91 if (c == '\n')
92 {
93 sstr.ignore();
94 }
95 }
96 }
97
98 return i;
99 }
100
101} // namespace vsg