Apply GPLv3 to all sources
[Yara.git] / lib / Yara / Template.cpp
bloba169ffd51e14a63faffed2197cc36b66d3e7bfc1
1 /*
2 * Yara: Yet Another RSS Aggregator
3 * Copyright (C) 2007 Ronald Landheer-Cieslak
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "Template.h"
18 #include <fstream>
19 #include <iterator>
20 #include <vector>
21 #include <boost/filesystem/path.hpp>
23 namespace Yara
25 Template::Template()
26 { /* no-op */ }
28 Template::Template(const boost::filesystem::path & filename)
30 std::ifstream in_file(filename.string().c_str(), std::ios::binary);
31 in_file.exceptions(std::ios::badbit | std::ios::failbit);
32 TemplateHeader template_header;
34 char * p(reinterpret_cast<char *>(&template_header));
35 in_file.read(p, sizeof(TemplateHeader));
38 std::vector< char > buffer(template_header.header_size_);
39 in_file.seekg(template_header.header_offset_);
40 in_file.read(&(buffer[0]), template_header.header_size_);
41 header_ = std::string(buffer.begin(), buffer.end());
44 std::vector< char > buffer(template_header.footer_size_);
45 in_file.seekg(template_header.footer_offset_);
46 in_file.read(&(buffer[0]), template_header.footer_size_);
47 footer_ = std::string(buffer.begin(), buffer.end());
50 std::vector< char > buffer(template_header.item_size_);
51 in_file.seekg(template_header.item_offset_);
52 in_file.read(&(buffer[0]), template_header.item_size_);
53 item_ = std::string(buffer.begin(), buffer.end());
57 Template::~Template()
58 { /* no-op */ }
60 void Template::compile(const boost::filesystem::path & filename, const std::string & header, const std::string & footer, const std::string & item)
62 TemplateHeader template_header;
63 template_header.header_offset_ = sizeof(TemplateHeader);
64 template_header.header_size_ = static_cast< unsigned int >(header.size());
65 template_header.footer_offset_ = template_header.header_offset_ + template_header.header_size_;
66 template_header.footer_size_ = static_cast< unsigned int >(footer.size());
67 template_header.item_offset_ = template_header.footer_offset_ + template_header.footer_size_;
68 template_header.item_size_ = static_cast< unsigned int >(item.size());
70 std::ofstream out_file(filename.string().c_str(), std::ios::binary);
72 std::vector< char > buffer;
73 char * p(reinterpret_cast<char *>(&template_header));
74 std::copy(p, p + sizeof(TemplateHeader), std::back_inserter(buffer));
75 out_file.write(&(buffer[0]), static_cast< std::streamsize >(buffer.size()));
77 out_file.write(&(header[0]), static_cast< std::streamsize >(header.size()));
78 out_file.write(&(footer[0]), static_cast< std::streamsize >(footer.size()));
79 out_file.write(&(item[0]), static_cast< std::streamsize >(item.size()));