-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqstring.cpp
147 lines (128 loc) · 3.23 KB
/
qstring.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "qstring.h"
#include <iostream>
#include <random>
#include <stdlib.h>
#include <cstdio>
#include <cstring>
q_string *g_root_string;
q_string_list *g_substitution_list;
std::random_device generator;
q_string::q_string(char *data):
data(data),
next(NULL){}
void q_string::render_list(void)
{
q_string *s = this;
while(s){
s->render();
s=s->next;
}
}
void q_string::verify_list(void)
{
q_string *s = this;
while(s){
s->verify();
s=s->next;
}
}
q_string_copy::q_string_copy(char *data):
q_string(data){}
void q_string_copy::render(void){
std::printf("%s",data);
}
void q_string_copy::verify(void){
}
q_string_ref::q_string_ref(char *data, parse_state *ps, int first_line, int first_column):
q_string(data),
ps(ps),
first_line(first_line),
first_column(first_column){}
void q_string_ref::render(void){
find_and_render();
}
void q_string_ref::verify(void){
q_string_list *list = g_substitution_list;
if(!list){
printf("No substitution lists\n");
exit(EXIT_FAILURE);
}
bool found = false;
while(list){
if(strcmp(list->id,data)==0){
found = true;
break;
}
list = list->next;
}
if(!found){
printf("Reference not found |%s| in file \"%s\" [line:%d,column:%d]\n",
data,
ps->filename,
first_line, first_column
);
exit(EXIT_FAILURE);
}
}
void q_string_ref::find_and_render(void){
q_string_list *list = g_substitution_list;
if(!list){
printf("No substitution lists!\n");
return;
}
do
{
if(strcmp(list->id,data)==0){
// found the list
// find the total weight
long total_weight = 0;
q_string_list_element *element = list->elements;
while(element){
total_weight += element->weight;
element = element->next;
}
// pick a random number
std::uniform_int_distribution<int> distribution(0,total_weight-1);
long x_pick = distribution(generator);
// select the element
long x_base = 0;
element = list->elements;
do {
if(x_pick>=x_base && x_pick<(x_base+element->weight)){
// render it
element->str->render_list();
return;
}
x_base += element->weight;
element = element->next;
} while(element);
return;
}
list = list->next;
}while(list);
}
q_string_list::q_string_list(char *id, q_string_list_element *elements):
id(id),
next(NULL),
elements(elements){}
q_string_list_element::q_string_list_element(long weight, q_string *str):
weight(weight),
str(str),
next(NULL){}
void RenderText( void )
{
g_root_string->render_list();
}
void VerifyText( void )
{
g_root_string->verify_list();
q_string_list *list = g_substitution_list;
while(list){
q_string_list_element *element = list->elements;
while(element){
element->str->verify_list();
element = element->next;
}
list = list->next;
}
}