Something went wrong. Try again.
Reactos
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
// This file is part of hhpcomp, a free HTML Help Project (*.hhp) compiler.// Copyright (C) 2015 Benedikt Freisen//// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.//// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// Lesser General Public License for more details.//// You should have received a copy of the GNU Lesser General Public// License along with this library; if not, write to the Free Software// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include <iostream>#include <fstream>#include <stdexcept>
#include <stdlib.h>
#include "hhp_reader.h"#include "utils.h"
using namespace std;
string hhp_section::get_name(){ return name;}
void hhp_section::set_name(string name){ this->name = name;}
hhp_pair::hhp_pair(string key, bool has_default_value, string default_value){ this->key = key; this->has_default_value = has_default_value; this->default_value = default_value; value_has_been_set = false;}
void hhp_pair::set_value(string value){ this->value = value; value_has_been_set = true;}
string hhp_pair::get_value(){ if (value_has_been_set) return value; else { if (has_default_value) return default_value; else throw domain_error("pair '" + key + "' does not have a default value"); }}
string hhp_pair::get_key(){ return key;}
void hhp_key_value_section::process_line(string line){ int pos_equals_sign = line.find_first_of('='); if (pos_equals_sign == string::npos) throw runtime_error("key-value pair does not contain an equals sign"); string key = to_upper(line.substr(0, pos_equals_sign)); string value = line.substr(pos_equals_sign + 1); if (key.length() == 0) throw runtime_error("key has length zero");
entries.find(key)->second->set_value(value);}
void hhp_key_value_section::add_entry(hhp_pair* entry){ string upper_case_key = to_upper(entry->get_key()); if (entries.count(upper_case_key) != 0) throw logic_error("trying to redundantly add key '" + upper_case_key + "'"); entries.insert(pair<string, hhp_pair*>(upper_case_key, entry));}
hhp_options_section::hhp_options_section(){ set_name("OPTIONS");
add_entry(binary_TOC = new hhp_pair("Binary TOC", true, "No")); add_entry(binary_index = new hhp_pair("Binary Index", true, "Yes")); add_entry(compiled_file = new hhp_pair("Compiled File", false)); add_entry(contents_file = new hhp_pair("Contents File", true, "")); add_entry(index_file = new hhp_pair("Index File", true, "")); add_entry(autoindex = new hhp_pair("AutoIndex", true, "No")); add_entry(defaultwindow = new hhp_pair("DefaultWindow", true, ""));//? add_entry(default_topic = new hhp_pair("Default Topic", true, "Index.htm"));//? add_entry(defaultfont = new hhp_pair("DefaultFont", true, "")); add_entry(language = new hhp_pair("Language", true, "0x409 English (US)"));//? add_entry(title = new hhp_pair("Title", true, ""));//? add_entry(createchifile = new hhp_pair("CreateCHIFile", true, "No")); add_entry(compatibility = new hhp_pair("Compatibility", true, "1.1")); add_entry(errorlogfile = new hhp_pair("ErrorLogFile", true, "Compiler.log"));//? add_entry(full_text_search = new hhp_pair("Full-text search", true, "Yes"));//? add_entry(display_compile_progress = new hhp_pair("Display compile progress", true, "Yes"));//? add_entry(display_compile_note = new hhp_pair("Display compile note", true, "Yes"));//? add_entry(flat = new hhp_pair("Flat", true, "No")); add_entry(full_text_search_stop_list_file = new hhp_pair("Full text search stop list file", true, ""));}
hhp_options_section::~hhp_options_section(){ delete binary_TOC; delete binary_index; delete compiled_file; delete contents_file; delete index_file; delete autoindex; delete defaultwindow; delete default_topic; delete defaultfont; delete language; delete title; delete createchifile; delete compatibility; delete errorlogfile; delete full_text_search; delete display_compile_progress; delete display_compile_note; delete flat; delete full_text_search_stop_list_file;}
hhp_files_section::hhp_files_section(){ set_name("FILES");}
void hhp_files_section::process_line(string line){ filenames.push_back(line);}
hhp_reader::hhp_reader(string filename){ this->filename = filename;
options = new hhp_options_section(); add_section(options); files = new hhp_files_section(); add_section(files);
read(); compute_unique_file_pathes_set();}
hhp_reader::~hhp_reader(){ delete options; delete files;}
void hhp_reader::add_section(hhp_section* section){ string upper_case_name = to_upper(section->get_name()); if (sections.count(upper_case_name) != 0) throw logic_error("trying to redundantly add section '" + upper_case_name + "'"); sections.insert(pair<string, hhp_section*>(upper_case_name, section));}
void hhp_reader::read(){ ifstream hhp_file; hhp_file.open(filename.c_str());
string line; int line_number = 0; hhp_section* section = NULL; while (hhp_file.good()) { getline(hhp_file, line); line_number++; if (line[line.length() - 1] == '\015') // delete CR character if present line = line.substr(0, line.length() - 1); if (line[0] == '[' && line[line.length() - 1] == ']') { string name = to_upper(line.substr(1, line.length() - 2)); if (sections.count(name)) { section = sections.find(name)->second; clog << section->get_name() << endl; } else { clog << "unknown section: " << name << endl; } } else if (line[0] != ';' && !line.empty()) { if (section) section->process_line(line); } }
hhp_file.close();}
void hhp_reader::compute_unique_file_pathes_set(){ for (list<string>::iterator it = files->filenames.begin(); it != files->filenames.end(); ++it) { unique_file_pathes.insert(replace_backslashes(real_path(it->c_str()))); }}
string hhp_reader::get_title_string(){ return options->title->get_value();}
string hhp_reader::get_contents_file_string(){ return options->contents_file->get_value();}
string hhp_reader::get_index_file_string(){ return options->index_file->get_value();}
string hhp_reader::get_default_topic_string(){ return options->default_topic->get_value();}
unsigned int hhp_reader::get_language_code(){ return strtoul(options->language->get_value().c_str(), NULL, 0);}
string hhp_reader::get_compiled_file_string(){ return options->compiled_file->get_value();}
set<string>::iterator hhp_reader::get_file_pathes_iterator_begin(){ return unique_file_pathes.begin();}
set<string>::iterator hhp_reader::get_file_pathes_iterator_end(){ return unique_file_pathes.end();}