// ---------------------------------------------------------------------------
// 
//   Lily Engine alpha
// 
//   Copyright (c) 2010 Clifford Jolly
//     http://cliffjolly.com
//     cliff@cliffjolly.com
// 
// ---------------------------------------------------------------------------
// 
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation; either version 2 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 General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
//   USA
// -------------------------- END HEADER -------------------------------------

#include <iostream>
#include <iomanip>
using namespace std;

#include <string.h>

#include "../filesystem/filesystem.h"
#include "compress.h"

void dumpMem(const char *mem, unsigned int length) {
	
	while(length) {
		
		for(int i = 0; i < 16; i++) {
			cout << setw(2) << hex << (int)(unsigned char)(*mem) << " ";
			mem++;
			length--;
			if(!length) break;
		}
		cout << endl;
	}
}

int main(int argc, char *argv[]) {
	
	const char *testStr = "Wharrgarbl! ...........fffffffffffffffffff";
	//const char *testStr = "asdfasdfasdfasdf";
	//const char *testStr = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
	int testLength = strlen(testStr) + 1;
	
	if(argc > 1) {
		testStr = FileSystem::loadFile(argv[1], &testLength);
	}
	
	if(!testStr) {
		cout << "Can't load that." << endl;
		return 1;
	}
	
	unsigned int doneLength = 0;
	unsigned int undoneLength = 0;
	
	char *compressed = compressRLE(testStr, testLength, &doneLength, 4);
	char *uncompressed = decompressRLE(compressed, doneLength, &undoneLength, 4);
	
	cout << "Non-compressed size: " << testLength << endl;
	cout << "Compressed size:     " << doneLength << endl;
	cout << "Uncompressed size:   " << undoneLength << endl;
	
	if(argc < 2) {
		
		cout << "original:     " << testStr << endl;
		cout << "decompressed: " << (uncompressed ? uncompressed : "") << endl;
	
		cout << endl << "Uncompressed dump:" << endl;
		dumpMem(testStr, testLength);
		
		cout << endl << "Compressed dump:" << endl;
		dumpMem(compressed, doneLength);
		
		if(uncompressed) {
			cout << endl << "Decompressed dump:" << endl;
			dumpMem(uncompressed, undoneLength);
		}
	
	} else {
		
		if(uncompressed) {
		
			bool verified = true;
			for(int i = 0; i < testLength; i++) {
				if(uncompressed[i] != testStr[i]) {
					verified = false;
					break;
				}
			}
			
			if(verified) {
				cout << "Verified data" << endl;
			} else {
				cout << "Something went horribly wrong!" << endl;
			}
		}
	}
	
	delete[] compressed;
	delete[] uncompressed;
	
	if(argc > 1) {
		delete[] testStr;
	}
	
	return 0;
}


