Reactos
1
2// This file is part of hhpcomp, a free HTML Help Project (*.hhp) compiler.
3// Copyright (C) 2015 Benedikt Freisen
4//
5// This library is free software; you can redistribute it and/or
6// modify it under the terms of the GNU Lesser General Public
7// License as published by the Free Software Foundation; either
8// version 2.1 of the License, or (at your option) any later version.
9//
10// This library is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13// Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public
16// License along with this library; if not, write to the Free Software
17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19
20#include <iostream>
21#include <string>
22#include <set>
23#include <stdexcept>
24
25#include <sys/stat.h>
26
27#include "hhp_reader.h"
28#include "utils.h"
29
30#if defined(_WIN32)
31 #include <direct.h>
32#else
33 #include <unistd.h>
34#endif
35
36extern "C" {
37#include "chmc/chmc.h"
38#include "chmc/err.h"
39}
40
41extern "C" struct chmcTreeNode *chmc_add_file(struct chmcFile *chm, const char *filename,
42 UInt16 prefixlen, int sect_id, UChar *buf,
43 UInt64 len);
44
45
46using namespace std;
47
48int main(int argc, char** argv)
49{
50 if (argc != 2)
51 {
52 cerr << "Usage: hhpcomp <input.hhp>" << endl;
53 exit(0);
54 }
55
56 string absolute_name = replace_backslashes(real_path(argv[1]));
57 int prefixlen = absolute_name.find_last_of('/');
58 clog << prefixlen << endl;
59 if (chdir(absolute_name.substr(0, prefixlen).c_str()) == -1) // change to the project file's directory
60 {
61 cerr << "chdir: working directory couldn't be changed" << endl;
62 exit(0);
63 }
64 hhp_reader project_file(absolute_name);
65
66 struct chmcFile chm;
67 struct chmcConfig chm_config;
68
69 memset(&chm, 0, sizeof(struct chmcFile));
70 memset(&chm_config, 0, sizeof(struct chmcConfig));
71 chm_config.title = project_file.get_title_string().c_str();
72 chm_config.hhc = project_file.get_contents_file_string().c_str();
73 chm_config.hhk = project_file.get_index_file_string().c_str();
74 chm_config.deftopic = project_file.get_default_topic_string().c_str();
75 chm_config.language = project_file.get_language_code();
76 chm_config.tmpdir = ".";
77
78 int err;
79 err = chmc_init(&chm, replace_backslashes(project_file.get_compiled_file_string()).c_str(), &chm_config);
80 if (err)
81 {
82 cerr << "could not initialize chmc" << endl;
83 exit(EXIT_FAILURE);
84 }
85
86 for (set<string>::iterator it = project_file.get_file_pathes_iterator_begin();
87 it != project_file.get_file_pathes_iterator_end(); ++it)
88 {
89 clog << "File: " << *it << endl;
90 struct stat buf;
91 stat(it->c_str(), &buf);
92 if ((chmc_add_file(&chm, it->c_str(), prefixlen, 1, NULL, buf.st_size)) ? chmcerr_code() : CHMC_NOERR)
93 {
94 cerr << "could not add file: " << *it << endl;
95 exit(EXIT_FAILURE);
96 }
97 }
98
99 chmc_tree_done(&chm);
100 chmc_term(&chm);
101
102}