vsg 1.1.13
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 <cstdint>
14#include <istream>
15
16namespace vsg
17{
18
20 template<typename T>
21 uint32_t read_line(std::istream& sstr, T* values, uint32_t maxSize, bool read_to_end_line = true)
22 {
23 uint32_t i = 0;
24 auto c = sstr.peek();
25 while (i < maxSize)
26 {
27 // skip leading spaces
28 while (c == ' ' || c == '\t')
29 {
30 sstr.ignore();
31 c = sstr.peek();
32 }
33
34 if (c == '#') break;
35
36 if (read_to_end_line)
37 {
38 if (c == '\n')
39 {
40 sstr.ignore();
41 return i;
42 }
43 else if (c == '\r')
44 {
45 sstr.ignore();
46 c = sstr.peek();
47 if (c == '\n')
48 {
49 sstr.ignore();
50 }
51 return i;
52 }
53 }
54
55 if (!(sstr >> values[i])) return i;
56
57 ++i;
58
59 // skip trailing spaces/tabs
60 c = sstr.peek();
61
62 while (c == ' ' || c == '\t')
63 {
64 sstr.ignore();
65 c = sstr.peek();
66 }
67
68 // skip a single comma if we have one
69 if (c == ',')
70 {
71 sstr.ignore();
72 c = sstr.peek();
73 }
74 }
75
76 if (read_to_end_line)
77 {
78 while (sstr && (c != '\n') && (c != '\r'))
79 {
80 sstr.ignore();
81 c = sstr.peek();
82 }
83
84 if (c == '\n')
85 {
86 sstr.ignore();
87 }
88 else if (c == '\r')
89 {
90 sstr.ignore();
91 c = sstr.peek();
92 if (c == '\n')
93 {
94 sstr.ignore();
95 }
96 }
97 }
98
99 return i;
100 }
101
102} // namespace vsg