1 | //============================================================================ |
2 | // |
3 | // SSSS tt lll lll |
4 | // SS SS tt ll ll |
5 | // SS tttttt eeee ll ll aaaa |
6 | // SSSS tt ee ee ll ll aa |
7 | // SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" |
8 | // SS SS tt ee ll ll aa aa |
9 | // SSSS ttt eeeee llll llll aaaaa |
10 | // |
11 | // Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony |
12 | // and the Stella Team |
13 | // |
14 | // See the file "License.txt" for information on usage and redistribution of |
15 | // this file, and for a DISCLAIMER OF ALL WARRANTIES. |
16 | //============================================================================ |
17 | |
18 | #ifndef STRING_PARSER_HXX |
19 | #define STRING_PARSER_HXX |
20 | |
21 | #include "bspf.hxx" |
22 | |
23 | /** |
24 | This class converts a string into a StringList by splitting on a delimiter |
25 | and size. |
26 | |
27 | @author Stephen Anthony |
28 | */ |
29 | class StringParser |
30 | { |
31 | public: |
32 | /** |
33 | Split the given string based on the newline character. |
34 | |
35 | @param str The string to split |
36 | */ |
37 | explicit StringParser(const string& str) |
38 | { |
39 | istringstream buf(str); |
40 | string line; |
41 | |
42 | while(std::getline(buf, line, '\n')) |
43 | myStringList.push_back(line); |
44 | } |
45 | |
46 | /** |
47 | Split the given string based on the newline character, making sure that |
48 | no string is longer than maximum string length. |
49 | |
50 | @param str The string to split |
51 | @param maxlen The maximum length of string to generate |
52 | */ |
53 | StringParser(const string& str, uInt32 maxlen) |
54 | { |
55 | istringstream buf(str); |
56 | string line; |
57 | |
58 | while(std::getline(buf, line, '\n')) |
59 | { |
60 | size_t len = maxlen, size = line.size(); |
61 | |
62 | if(size <= len) |
63 | myStringList.push_back(line); |
64 | else |
65 | { |
66 | size_t beg = 0; |
67 | while((beg + maxlen) < size) |
68 | { |
69 | size_t spos = line.find_last_of(' ', beg + len); |
70 | if(spos != string::npos && spos > beg) |
71 | len = spos - beg; |
72 | |
73 | myStringList.push_back(line.substr(beg, len)); |
74 | beg += len + 1; |
75 | len = maxlen; |
76 | } |
77 | myStringList.push_back(line.substr(beg)); |
78 | } |
79 | } |
80 | } |
81 | |
82 | const StringList& stringList() const { return myStringList; } |
83 | |
84 | private: |
85 | StringList myStringList; |
86 | |
87 | private: |
88 | // Following constructors and assignment operators not supported |
89 | StringParser() = delete; |
90 | StringParser(const StringParser&) = delete; |
91 | StringParser(StringParser&&) = delete; |
92 | StringParser& operator=(const StringParser&) = delete; |
93 | StringParser& operator=(StringParser&&) = delete; |
94 | }; |
95 | |
96 | #endif |
97 | |