Webdar 1.0.0
Web user interface to libdar
webdar_tools.hpp
1/*********************************************************************/
2// webdar - a web server and interface program to libdar
3// Copyright (C) 2013-2025 Denis Corbin
4//
5// This file is part of Webdar
6//
7// Webdar is free software: you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// Webdar is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with Webdar. If not, see <http://www.gnu.org/licenses/>
19//
20//----
21// to contact the author: dar.linux@free.fr
22/*********************************************************************/
23
24#ifndef WEBDAR_TOOLS_HPP
25#define WEBDAR_TOOLS_HPP
26
27#include "my_config.h"
28
29 // C++ system header files
30#include <sstream>
31#include <vector>
32#include <deque>
33#include <string>
34
35 // webdar headers
36#include "uri.hpp"
37#include "webdar_tools.hpp"
38
39
40template <class T> std::string webdar_tools_convert_to_string(T val)
41{
42 std::stringstream tmp;
43 tmp << val;
44 return tmp.str();
45}
46
54template <class T> bool webdar_tools_semi_less_than(T a, T b, bool & result)
55{
56 if(a < b)
57 {
58 result = true;
59 return true;
60 }
61 if(b < a)
62 {
63 result = false;
64 return true;
65 }
66 return false;
67}
68
69template <class T> void webdar_tools_concat_vectors(std::vector<T> & op1,
70 const std::vector<T> & op2)
71{
72 for(unsigned int i = 0; i < op2.size(); ++i)
73 op1.push_back(op2[i]);
74}
75
76template <class T> T webdar_tools_convert_from_infinint(const libdar::infinint & val, const std::string & conv_err_msg)
77{
78 T ret = 0;
79 libdar::infinint tmp(val);
80
81 tmp.unstack(ret);
82 if(! tmp.is_zero())
83 {
84 if(conv_err_msg.empty())
85 throw WEBDAR_BUG;
86 else
87 throw exception_range(conv_err_msg);
88 }
89
90 return ret;
91}
92
94
97struct troncon
98{
99 std::string::const_iterator begin;
100 std::string::const_iterator end;
101
102 troncon() {}; // needed to use troncon in std::map
103 troncon(std::string::const_iterator b, std::string::const_iterator e) { begin = b; end = e; };
104 troncon(const std::string & ref) { begin = ref.begin(); end = ref.end(); };
105
106 bool operator < (const troncon & ref) const;
107};
108
109extern int webdar_tools_convert_to_int(const std::string & ref);
110extern unsigned int webdar_tools_convert_hexa_to_int(const std::string & ref);
111extern void webdar_tools_split_by(char sep, const std::string & aggregate, std::vector<std::string> & splitted);
112extern void webdar_tools_split_in_two(char sep, const std::string &aggregate, std::string & first, std::string & second);
113
115
121extern std::deque<troncon> webdar_tools_split_by_substring(const std::string & substring,
122 const troncon & aggregate);
123
125
127extern std::string::const_iterator webdar_tools_seek_to_substring(const std::string & substring, const troncon & aggregate);
128
129extern std::string webdar_tools_remove_leading_spaces(const std::string & input);
130extern void webdar_tools_init_randomization();
131
136extern std::string webdar_tools_generate_random_string(unsigned int size);
137extern std::string webdar_tools_get_session_ID_from_URI(const uri & url);
138extern std::string webdar_tools_to_canonical_case(const std::string & ch);
139extern std::string webdar_tools_decode_urlencoded(const std::string & ch);
140extern std::string webdar_tools_get_title(const std::string & sessname, const std::string & status);
141
142extern std::string webdar_tools_html_display(const std::string & arg);
143
144extern bool webdar_tools_exists_and_is_file(const std::string & path, bool follow_symlink);
145extern bool webdar_tools_exists_and_is_dir(const std::string & path, bool follow_symlink);
146
147extern std::string webdar_tools_capitalize_first_letter_of_words(const std::string & source);
148
149#endif
exception used to report out or range value or argument
Definition: exceptions.hpp:109
uri type holds the splitted list of the scheme / hostname / path # anchor
Definition: uri.hpp:44
defines a substring portion of a existing string (to avoid data copy)
Definition: webdar_tools.hpp:98