// Uppgift: Skapa en html-tabell över alla exempelfiler.
// Nyckelord: ifstream, kommandoradsargument, std::string, std::vector, struct, typedef
#include
#include
#include
using namespace std;
typedef struct
{
string file;
string task;
string keywords;
} example;
// " " in the string to avoid matching those lines when the program is run
// Compiler will see it as two strings to concatenate.
const string TASK = "// Up" "pgift: ";
const string KEYW = "// Nyck" "elord: ";
// Convert UTF8 characters to ISO-8859-1
string iso(string const& s);
void add_example(vector& v, string filename)
{
ifstream in(filename);
if ( ! in)
return;
example ex;
string line;
while (getline(in, line))
{
size_t task = line.find(TASK);
size_t keywords = line.find(KEYW);
if ( task != string::npos )
ex.task = iso(line.substr(task + TASK.length()));
if ( keywords != string::npos )
ex.keywords = iso(line.substr(keywords + KEYW.length()));
}
if (ex.task != "" || ex.keywords != "")
{
ex.file = iso(filename);
v.push_back(ex);
}
}
int main(int argc, char* argv[])
{
vector table;
for (int i = 1; i < argc; ++i)
{
add_example(table, argv[i]);
}
cout << "" << endl;
cout << "" << endl
<< "" << "Exempelfil" << " | "
<< "" << "Uppgift" << " | "
<< "" << "Nyckelord" << " | " << endl
<< "
" << endl;
bool odd = true;
for (auto& i : table)
{
cout << "" << endl
<< "" << i.file << " | "
<< "" << i.task << " | "
<< "" << i.keywords << " | " << endl
<< "
" << endl;
odd = !odd;
}
cout << "
" << endl;
return 0;
}
string iso(string const& s)
{
string iso;
unsigned i = 0;
while ( i < s.size() )
{
if ( (s.at(i) & 0xE0) == 0xC0 )
{
iso += char((s.at(i) << 6) | (s.at(i+1) & 0x3F));
i += 2;
}
else
{
iso += s.at(i);
i += 1;
}
}
return iso;
}