I'm doing a server application in C++, and it provides an HTML page as response to HTTP requests.
The problem is that, currently, my webpage is written as a constant string in my code, and I insert other strings using << operator and std::stringstream, still during the writing of the string itself. See the example to get it clearer:
std::string first("foo");
std::string second("bar");
std::string third("foobar");
std::stringstream ss;
ss << "<html>\n"
"<head>\n"
"<title>Bitches Brew</title>\n"
"</head>\n"
"<body>\n"
"First string: "
<< first << "\n"
"Second string: "
<< second << "\n"
"Third string: "
<< third << "\n"
"</body>\n"
"</html>";
Happens though I cannot simply stuff the contents in a file, because the data mixed with the HTML structure will change during the execution. This means I can't simply write the entire page in a file, with the string values of first, second, and third, because these values will change dynamically.
For the first request I'd send the page with first = "foo";, whereas in the second request I'd have first = "anything else".
Also, I could simply go back to sscanf/sprintf from stdio.h and insert the text I want -- I'd just have to replace the string gaps with the proper format (%s), read the HTML structure from a file, and insert whatever I wanted.
I'd like to do this in C++, without C library functions, but I couldn't figure out what to use to do this. What would be the C++ standard solution for this?
Standard C++ doesn't have a direct equivalent to (s)printf-like formatting other than (s)printf itself. However, there are plenty of formatting libraries that provide this functionality, like the cppformat library that includes a C++ implementation of Python's str.format and safe printf.
That said, I'd recommend using a template engine instead, see
C++ HTML template framework, templatizing library, HTML generator library .
Or you can always reinvent the wheel and write your own template engine by reading a file and replacing some placeholders with arguments.
What about:
void RenderWebPage(std::stringstream& ss, std::string& first, std::string& second, std::string& third)
{
ss << "<html>\n"
"<head>\n"
"<title>Bitches Brew</title>\n"
"</head>\n"
"<body>\n"
"First string: "
<< first << "\n"
"Second string: "
<< second << "\n"
"Third string: "
<< third << "\n"
"</body>\n"
"</html>";
}
And you can call it like this:
std::stringstream ss;
std::string first("foo");
std::string second("bar");
std::string third("foobar");
RenderWebPage(ss, first, second, third);
first = "anything else";
RenderWebPage(ss, first, second, third);
second = "seconds too";
RenderWebPage(ss, first, second, third);
You can get the desired result like this:
Store your static HTML in a file, with placeholders for the dynamic text
Read the HTML file into a std::string
For each piece of dynamic text, locate its placeholder in the string (std::string::find) and replace the placeholder with the dynamic text (std::string::replace).
Write the modified string to the final destination.
If you don't want to use a framework as other answers (correctly) suggest, I guess you can take inspiration from this little program:
#include <iostream>
#include <map>
using namespace std;
string instantiate_html(string const& templateHTML, map<string, string> const& replacements)
{
string outputHTML = templateHTML;
for (auto& entry : replacements)
{
string placeholder = "<$" + entry.first + "$>";
size_t it = outputHTML.find(placeholder);
if (it != string::npos)
{
outputHTML.replace(it, placeholder.size(), entry.second);
}
}
return outputHTML;
}
int main()
{
map<string, string> replacements;
replacements["name"] = "Mark";
replacements["surname"] = "Brown";
// Normally you would read this string from your template file
string templateHTML = "<html><body><$name$><$surname$></body></html>";
string outputHTML = instantiate_html(templateHTML, replacements);
cout << outputHTML;
return 0;
}
Related
What is the "proper way" to do the following? (Note, I don't want to output the message to the screen (yet), the data needs to be stored in a variable.)
std::cout << "Enter a letter: ";
char input;
std::cin >> input;
std::string message = "Today's program was brought to you by the letter '" + input + "'.";
The code gives me the error message invalid operands of types const char* and const char [3] to binary operator+.
I understand why this message is occurring. When Googling for a solution, the results that come up recommend casting each item into a string in turn. However, this becomes impractical if you have to concatenate a dozen items:
std::string("x:") + x + std::string(", y:") + y + std::string(", width:") + width + std::string(", height:") + height + ...;
What is the "proper way" in c++ to concatenate strings, chars, char arrays, and any other data that is convertible, into a single string? Is there anything like Python's beautiful string formatting features in c++?
What you are trying to do won't work because C++ views your concatenation as an attempt to add several char pointers. If you explicitly cast the first element in the series to an std::string it should work.
Change your original code
string message = "Today's program was brought to you by the letter '" + input + "'.";
to this:
string message = std::string("Today's program was brought to you by the letter '")
+ input + "'.";
q.v. this SO post which discusses this problem in greater detail (though I don't know why it got closed as not being a real question).
There's several ways to do this, but one way that's a good balance between simplicity of implementation and convenience is to use a "formatter" class which wraps std::stringstream like so:
string message = formatter() << "Today's program was brought to you by the letter '" << input << "'.";
Where formatter can be defined very simply in a header file as follows:
#include <sstream>
class formatter {
public:
template <typename T>
formatter & operator<<(const T & o) {
stream_ << o;
return *this;
}
const std::string str() const { return stream_.str(); }
operator std::string() {
return stream_.str();
}
private:
std::ostringstream stream_;
};
What's going on there: If you try to use a temporary std::stringstream() instead of formatter() above, it doesn't work because
std::stringstream is not implicitly convertible to std::string
You can't even do it like this
std::string message = (std::stringstream() << "foo" << input << "bar").str(); because, std::stringstream returns std::ostream & from its stream operations (rather than std::stringstream &), and you cannot convert an ostream to a string in general.
The formatter class just lets you construct and use a stringstream all in one line with a minimum of boiler plate.
I am trying to input data from a text file:
The line format is as follows...
String|String|int double
Example:
Bob|oranges|10 .89
I can get the line in as a string using
Getline(infile, line)
I don't understand how to break the line into the distinct variables from the string variable.
Thanks
for a start you could write some good old fashioned c code using strchr.
Or use string.find / find_first_of if you are using std::String
http://www.cplusplus.com/reference/string/string/find_first_of/
You marked this as C++. So perhaps you should try to use formatted extractors ...
Here is a 'ram' file (works just like a disk file)
std::stringstream ss("Bob|oranges|10 .89");
// this ^^^^^^^^^^^^^^^^^^ puts one line in file
I would use getline for the two strings, with bar terminator
do {
std::string cust;
(void)std::getline(ss, cust, '|'); // read to 1st bar
std::string fruit;
(void)std::getline(ss, fruit, '|'); // read to 2nd bar
Then read the int and float directly:
int count = 0;
float cost;
ss >> count >> cost; // the space char is ignored by formatted extraction
std::cout << "\ncust: " << cust << "\n"
<< " " << count << " " << fruit
<< " at $" << cost
<< " Totals: " << (float(count) * cost) << std::endl;
if(ss.eof()) break;
}while(0);
If you are to handle more lines, you need to find the eoln, and repeat for every record of the above style.
This approach is extremely fragile (any change in format will force a change in your code).
This is just to get your started. It has been my experience that using std::string find and rfind is much less fragile.
Good luck.
How can I assign a variable from my C++ code a value from a structured .txt file for example?
If I have this following input.txt structured like this:
<Name> "John Washington"
<Age> "24"
<ID> "19702408447417"
<Alive Status> "Deceased"
In my c++ code if I have
ifstream read("input.txt",ios::in);
char name[64];
read>>name;//name will point to John Washington string
int age;
read>>age;// cout<<age will return 24;
int ID;
read>>ID;// cout<<ID will return 19702408447417
char Alivestatus[32];
read>>Alivestatus;//Alivestatus will point to Deceased string;
How can I make it work like above?
As #πάντα ῥεῖ mentioned in the comments, you will need to implement a parser that can interpret the <> tags within your file. Additionally, I would recommend reconsidering the data types.
Specifically, given that there's no special reason that you're using char [], please switch to std::string. I don't know the use case of your code, but if the input.txt happens to contain data thats larger than the size of the arrays, or even worse if the input is user-controlled, this can easily lead to Buffer Overflows and unwanted exploits. std::string also has the benefit of being standardized, optimized, much more friendly than char arrays, and has a variety of useful algorithms and functions readily available for use.
With regards to text file parsing, you can perhaps implement the following:
#include <fstream>
#include <iostream>
#include <string>
int main()
{
std::ifstream input_file("input.txt");
std::string name_delimeter("<Name> ");
std::string age_delimeter("<Age> ");
std::string id_delimeter("<ID> ");
std::string alive_delimeter("<Alive Status> ");
std::string line;
std::getline(input_file,line);
std::string name(line,line.find(name_delimeter) + name_delimeter.size()); // string for the reasons described above
std::getline(input_file,line);
int age = std::atoi(line.substr(line.find(age_delimeter) + age_delimeter.size()).c_str());
std::getline(input_file,line);
std::string id(line,line.find(id_delimeter) + id_delimeter.size()); // the example ID overflows 32-bit integer
// maybe representing is a string is more appropriate
std::getline(input_file,line);
std::string alive_status(line,line.find(alive_delimeter) + alive_delimeter.size()); // string for the same reason as name
std::cout << "Name = " << name << std::endl << "Age = " << age << std::endl << "ID = " << id << std::endl << "Alive? " << alive_status << std::endl;
}
The basis of the code is just to read the file as it is structured and construct the appropriate data types from them. In fact, because I used std::string for most of the data types, it was easy to build the correct output by means of std::string's constructors and available functions.
Maybe you are performing this in a loop, or the file has several structures. To approach this problem, you can make a Record class that overloads operator >> and reads in the data as required.
Since std::stringstream is a stzream, and according to the documention here, you can perform any operation a stream supports.
So I expected the following sample to work, but it seems it doesn't. I'm using MingW with gcc 4.8.3.
Variant A:
std::string s;
std::stringstream doc;
doc << "Test " << "String ";
doc << "AnotherString";
doc >> s;
std::cout << s << std::endl;
Variant B:
std::string s;
std::stringstream doc;
doc << "Test ";
doc << "AnotherString";
doc >> s;
std::cout << s << std::endl;
The output of this is only
Test
While I expected that it would concatenate the individual strings until I read from the stream back what I put there.
So what is the approperiate way to concatenate strings? Do I really have to read out each one individually and concatenate them manually, which seems quite awkward to me in C++.
It is putting each of the strings into doc, so that its content is:
Test String AnotherString
Then when you extract using doc >> s, it only reads up to the first whitespace. If you want to get the entire stream as a string, you can call str:
std::cout << doc.str() << std::endl;
It will only read one word till a white-space by using stream >> s. Besides #JosephMansfield's answer of using str(), alternatively you can use getline() (works perfectly if you the string doesn't contains new lines):
getline(doc, s);
I am using boost::split to parse a data file. The data file contains lines such as the following.
data.txt
1:1~15 ASTKGPSVFPLAPSS SVFPLAPSS -12.6 98.3
The white space between the items are tabs. The code I have to split the above line is as follows.
std::string buf;
/*Assign the line from the file to buf*/
std::vector<std::string> dataLine;
boost::split( dataLine, buf , boost::is_any_of("\t "), boost::token_compress_on); //Split data line
cout << dataLine.size() << endl;
For the above line of code I should get a print out of 5, but I get 6. I have tried to read through the documentation and this solution seems as though it should do what I want, clearly I am missing something. Thanks!
Edit:
Running a forloop as follows on dataLine you get the following.
cout << "****" << endl;
for(int i = 0 ; i < dataLine.size() ; i ++) cout << dataLine[i] << endl;
cout << "****" << endl;
****
1:1~15
ASTKGPSVFPLAPSS
SVFPLAPSS
-12.6
98.3
****
Even though "adjacent separators are merged together", it seems like the trailing delimeters make the problem, since even when they are treated as one, it still is one delimeter.
So your problem cannot be solved with split() alone. But luckily Boost String Algo has trim() and trim_if(), which strip whitespace or delimeters from beginning and end of a string. So just call trim() on buf, like this:
std::string buf = "1:1~15 ASTKGPSVFPLAPSS SVFPLAPSS -12.6 98.3 ";
std::vector<std::string> dataLine;
boost::trim_if(buf, boost::is_any_of("\t ")); // could also use plain boost::trim
boost::split(dataLine, buf, boost::is_any_of("\t "), boost::token_compress_on);
std::cout << out.size() << std::endl;
This question was already asked: boost::split leaves empty tokens at the beginning and end of string - is this desired behaviour?
I would recommend using C++ String Toolkit Library. This library is much faster than Boost in my opinion. I used to use Boost to split (aka tokenize) a line of text but found this library to be much more in line with what I want.
One of the great things about strtk::parse is its conversion of tokens into their final value and checking the number of elements.
you could use it as so:
std::vector<std::string> tokens;
// multiple delimiters should be treated as one
if( !strtk::parse( dataLine, "\t", tokens ) )
{
std::cout << "failed" << std::endl;
}
--- another version
std::string token1;
std::string token2;
std::string token3:
float value1;
float value2;
if( !strtk::parse( dataLine, "\t", token1, token2, token3, value1, value2) )
{
std::cout << "failed" << std::endl;
// fails if the number of elements is not what you want
}
Online documentation for the library: String Tokenizer Documentation
Link to the source code: C++ String Toolkit Library
Leading and trailing whitespace is intentionally left alone by boost::split because it does not know if it is significant or not. The solution is to use boost::trim before calling boost::split.
#include <boost/algorithm/string/trim.hpp>
....
boost::trim(buf);