blob: 2e6b977f2d72321919354a7d14d71925e1c68f2b (
plain)
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
148
149
150
151
152
153
154
155
156
157
158
|
// class html implementation.
#ifndef HTML_CXX
#define HTML_CXX
#include <fstream>
#include "html.h"
#include "CHAT.h"
#include "MUTX.h"
using namespace std;
html::html( )
{
#ifdef VERBOSE
pthread_mutex_lock ( &MUTX::get().mut_stdout );
cout << "html::html()" << endl;
pthread_mutex_unlock( &MUTX::get().mut_stdout );
#endif
set_name( CONF::get().get_val( "HTMLTEMP" ) );
pthread_mutex_init( &mut_map_vals, NULL );
}
html::~html( )
{
#ifdef VERBOSE
pthread_mutex_lock ( &MUTX::get().mut_stdout );
cout << "html::~html()" << endl;
pthread_mutex_unlock( &MUTX::get().mut_stdout );
#endif
pthread_mutex_destroy( &mut_map_vals );
}
void
html::clear_cache( )
{
pthread_mutex_lock ( &mut_map_vals );
clear_vals();
pthread_mutex_unlock( &mut_map_vals );
}
string
html::parse( map_string &map_params )
{
#ifdef VERBOSE_
pthread_mutex_lock ( &MUTX::get().mut_stdout );
cout << "html::parse( map_string& )" << endl;
pthread_mutex_unlock( &MUTX::get().mut_stdout );
#endif
string s_file = map_params["request"];
// check if s_file is in the container.
pthread_mutex_lock ( &mut_map_vals );
string s_templ = get_val( s_file );
pthread_mutex_unlock( &mut_map_vals );
// if not, read file.
if ( s_templ.empty() )
{
auto string s_path = get_name();
auto ifstream fs_templ( s_path.append( s_file ).c_str() );
if ( ! fs_templ )
{
map_params["request"] = CONF::get().get_val( "NOTFOUND" );
return parse( map_params );
}
auto char c_buf[READBUF];
while( fs_templ.getline( c_buf, READBUF ) )
s_templ.append( string( c_buf ).append( "\n" ) );
fs_templ.close();
#ifdef _VERBOSE
pthread_mutex_lock ( &MUTX::get().mut_stdout );
cout << TECACHE << s_path << endl;
pthread_mutex_unlock( &MUTX::get().mut_stdout );
#endif
// cache file.
pthread_mutex_lock ( &mut_map_vals );
map_vals[ s_file ] = s_templ;
pthread_mutex_unlock( &mut_map_vals );
}
// find %%KEY%% token and substituate those.
auto unsigned int pos[2];
pos[0] = pos[1] = 0;
do
{
pos[0] = s_templ.find( "%%", pos[1] );
if ( pos[0] == string::npos )
break;
pos[0] += 2;
pos[1] = s_templ.find( "%%", pos[0] );
if ( pos[0] == string::npos )
break;
// get key and val.
auto string s_key = s_templ.substr( pos[0], pos[1]-pos[0] );
auto string s_val = CONF::get().get_val( s_key );
// if s_val is empty use map_params.
if ( s_val.empty() )
s_val = map_params[ s_key ];
// substituate key with val.
s_templ.replace( pos[0]-2, pos[1]-pos[0]+4, s_val );
// calculate the string displacement.
auto int i_dif = s_val.length() - ( pos[1] - pos[0] + 4);
pos[1] += 2 + i_dif;
}
while( true );
return s_templ;
}
void
html::online_list( user *p_user, map_string &map_params )
{
#ifdef VERBOSE_
pthread_mutex_lock ( &MUTX::get().mut_stdout );
cout << "html::online_list( user*, map_string& )" << endl;
pthread_mutex_unlock( &MUTX::get().mut_stdout );
#endif
// prepare user_list.
string s_list ( "" );
string s_seperator( "<br>" );
p_user->get_p_room()->get_user_list( s_list, s_seperator );
// use the collected data as a message in html-templates.
map_params["MESSAGE"] = s_list;
// renew the timestamp.
p_user->renew_stamp();
// send a ping to the client chat stream.
p_user->msg_post( new string("\n") );
}
#endif
|