xtd - Reference Guide  0.1.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
Loading...
Searching...
No Matches
css_reader.h
Go to the documentation of this file.
1
4#pragma once
5#include "selector_map.h"
6#include "../../argument_exception.h"
7#include "../../format_exception.h"
8#include "../../object.h"
9#include "../..//io/stream_reader.h"
10
12namespace xtd {
14 namespace web {
15 namespace css {
16 class css_reader : public object {
17 public:
18 css_reader(std::istream& stream) {parse_text(xtd::io::stream_reader(stream).read_to_end());}
19 css_reader(xtd::io::text_reader& text_reader) {parse_text(text_reader.read_to_end());}
20 css_reader(const xtd::ustring& text) {parse_text(text);}
21
22 const xtd::web::css::selector_map& selectors() const {return selectors_;}
23
24 private:
25 void parse_text(xtd::ustring text) {
26 enum class parse_status {
28 key,
29 value
30 };
31 parse_status status = parse_status::selector;
32 size_t start_index = 0;
33 xtd::web::css::selector current_selector;
34 xtd::ustring current_selector_name;
35 xtd::ustring current_key;
36 for (size_t index = 0; index < text.size(); index++) {
37 if (text[index] == '/' && text[index+1] == '*') {
38 // Skip comments...
39 index = text.index_of("*/", index+2);
40 if (index == text.npos) throw xtd::format_exception("expected end comment", current_stack_frame_);
41 index++;
42 start_index = index + 1;
43 continue;
44 } else if (status == parse_status::selector && text[index] == '{') {
45 current_selector_name = text.substring(start_index, index - start_index).trim();
46 current_selector.name(text.substring(start_index, index - start_index).trim());
47 start_index = index + 1;
48 status = parse_status::key;
49 } else if (status == parse_status::key && text[index] == '}') {
50 selectors_[current_selector_name] = current_selector;
51 current_selector = xtd::web::css::selector();
52 start_index = index + 1;
53 status = parse_status::selector;
54 } else if (status == parse_status::key && text[index] == ':') {
55 current_key = text.substring(start_index, index - start_index).trim();
56 if (current_key.empty()) throw xtd::format_exception("key cannot be empty", current_stack_frame_);
57 start_index = index + 1;
58 status = parse_status::value;
59 } else if (status == parse_status::value && text[index] == ';') {
60 auto value = text.substring(start_index, index - start_index).trim();
61 if (value.empty()) throw xtd::format_exception("value cannot be empty", current_stack_frame_);
62 start_index = index + 1;
63 current_selector.properties()[current_key] = property(value);
64 status = parse_status::key;
65 }
66 }
67 }
68
69 xtd::web::css::selector_map selectors_;
70 };
71 }
72 }
73}
The exception that is thrown when the format of an argument does not meet the parameter specification...
Definition: format_exception.h:18
Implements a xtd::io::text_reader that reads characters from a byte stream.
Definition: stream_reader.h:20
Represents a reader that can read a sequential series of characters.
Definition: text_reader.h:29
virtual xtd::ustring read_to_end()
Reads all characters from the current position to the end of the text_reader and returns them as one ...
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition: object.h:26
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:48
ustring trim() const noexcept
Removes all leading and trailing occurrences of white-space characters from the specified String.
size_t index_of(value_type value) const noexcept
Reports the index of the first occurrence of the specified character in this string.
ustring substring(size_t start_index) const noexcept
Retrieves a substring from this instance. The substring starts at a specified character position and ...
Definition: css_reader.h:16
Definition: property.h:13
Definition: selector.h:12
#define current_stack_frame_
Provides information about the current stack frame.
Definition: stack_frame.h:201
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition: system_report.h:17
Contains xtd::web::css::selector_map typedef.