Source

lrc / src / example / ExtractText.cxx

Full commit
//      ExtractTexts.cxx
//
//      Copyright 2011, 2012 Andreas Tscharner <andy@vis.ethz.ch>
//
//      This program 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 3 of the
//      License, or (at your option) any later version.
//
//      This program 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 General Public License for more details.
//
//      You should have received a copy of the GNU Lesser General Public
//      License along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.


#include <iostream>
#include <cstdio>
#include <cstring>
#include "../include/ResourceManager.hxx"

#ifdef HAVE_CONFIG_H
#include "../lrc_config.h"
#endif


using namespace lrc;

int main(int argc, char **argv)
{
	const unsigned char PWD[] = "MyVeryVerySecret$%734Password\n";

	ResourceManager *texts;
	Resource *singleText;
	char **textList;
	int numTexts, fileNameLen, writtenBytes;
	FILE *txtFile;
	char *filename;
#ifdef NO_ENCRYPTION
	lrc::EncryptionType encText = lrc::NoneEncryption;
#else
	lrc::EncryptionType encText = lrc::SerpentEncryption;
#endif

	try {
		texts = new ResourceManager("texts.rdf", lrc::bz2LibCompression, encText, PWD);
	} catch (lrcFileNotFoundException const &fnfEx) {
		std::cout << fnfEx.what() << std::endl;
		return -1;
	};

	textList = texts->get_resource_ids(numTexts);
	if (numTexts < 1) {
		std::cout << "No text resources found" << std::endl;
		delete texts;
		return -1;
	};

	for (int i=0; i<numTexts; i++) {
		fileNameLen = strlen(textList[i]) + 4 + 1;
		filename = new char[fileNameLen];
		memset(filename, 0, fileNameLen);
		snprintf(filename, fileNameLen, "%s.txt", textList[i]);

		singleText = texts->get_resource(textList[i]);
		txtFile = fopen(filename, "w");
		fwrite(singleText->get_res_data(), sizeof(unsigned char), singleText->get_res_size(), txtFile);
		fclose(txtFile);
		delete singleText;
	};

	for (int i=numTexts-1; i>=0; i--)
		delete[] textList[i];
	delete[] textList;

	delete texts;


	return 0;
}