First real upload of the files used for developing
git-svn-id: https://clonekeenplus.svn.sourceforge.net/svnroot/clonekeenplus/cgenius/trunk@5 4df4b0f3-56ce-47cb-b001-ed939b7d65a6
This commit is contained in:
224
src/fileio/CParser.cpp
Normal file
224
src/fileio/CParser.cpp
Normal file
@@ -0,0 +1,224 @@
|
||||
/*
|
||||
* CParser.cpp
|
||||
*
|
||||
* Created on: 22.05.2009
|
||||
* Author: gerstrong
|
||||
*/
|
||||
|
||||
#include "CParser.h"
|
||||
#include "../CLogFile.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define CONFIGFILENAME "genius.cfg"
|
||||
#define MAX_STRING_LENGTH 256
|
||||
|
||||
|
||||
CParser::CParser() {
|
||||
m_isOpen = false;
|
||||
}
|
||||
|
||||
CParser::~CParser() {
|
||||
|
||||
while(!m_filebuffer.empty())
|
||||
{
|
||||
free(m_filebuffer.front());
|
||||
m_filebuffer.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
// replaced by sscanf, since that function is slower and leaks memory
|
||||
void CParser::parseline(char *p_input,char *p_output, int pos, int size)
|
||||
{
|
||||
char buf;
|
||||
int i=0;
|
||||
|
||||
while( (pos+i) < size )
|
||||
{
|
||||
buf = p_input[pos+i];
|
||||
if(buf == '\n')
|
||||
break;
|
||||
else
|
||||
p_output[i]=buf;
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Opens the text which can be parsed. This will read all the stuff in to the memory.
|
||||
bool CParser::loadParseFile(void) // Open, read the list and close the file
|
||||
{
|
||||
FILE *fp;
|
||||
|
||||
if((fp=fopen(CONFIGFILENAME,"rt")))
|
||||
{
|
||||
char *line;
|
||||
|
||||
while(!feof(fp))
|
||||
{
|
||||
line = (char*) calloc(256,sizeof(char));
|
||||
fgets(line,256,fp);
|
||||
//fscanf(fp,"%s\n",line);
|
||||
m_filebuffer.push_back(line);
|
||||
m_isOpen = true;
|
||||
}
|
||||
fclose(fp);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_pLogFile->ftextOut(GREEN,"Parser : The file has not been found. When CKP is trying to save the file it will create a new one.");
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Close the text which was parsed. This will also copy all stuff of the memory to the file.
|
||||
bool CParser::saveParseFile(void) // open, write on the file and close
|
||||
{
|
||||
FILE *fp;
|
||||
|
||||
if((fp=fopen(CONFIGFILENAME,"wt")))
|
||||
{
|
||||
list<char*>::iterator i;
|
||||
|
||||
for(i=m_filebuffer.begin() ; i != m_filebuffer.end() ; ++i )
|
||||
fprintf(fp,"%s",*i);
|
||||
|
||||
fclose(fp);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_pLogFile->ftextOut(RED,"Parser : Error opening the file for write operations. Check your permissions or if the disk is full");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// read the value of the to be seeked keyword and returns it as an int.
|
||||
// If no value was detected, it returns -1;
|
||||
// If something was detected, the file is also rewinded!
|
||||
int CParser::getIntValue(const char *keyword, const char *category)
|
||||
{
|
||||
// The getter will search for category and than for keyword. After that, read the value and return it!
|
||||
list<char*>::iterator i;
|
||||
|
||||
char *line;
|
||||
|
||||
for(i=m_filebuffer.begin() ; i != m_filebuffer.end() ; ++i )
|
||||
{
|
||||
line = *i;
|
||||
if(line[0]=='[') // found category
|
||||
{
|
||||
if(strncmp(line+1,category,strlen(category)) == 0) // is it the category we are looking for?
|
||||
{
|
||||
++i;
|
||||
line = *i;
|
||||
|
||||
// category found !! let's see if the keyword exists
|
||||
while(line[0]!='[')
|
||||
{
|
||||
if(strncmp(line,keyword,strlen(keyword)) == 0)
|
||||
{
|
||||
int value;
|
||||
// keyword also found!! set the new value!! case 3
|
||||
sscanf(line+strlen(keyword)+2,"%d",&value);
|
||||
return value;
|
||||
}
|
||||
|
||||
++i;
|
||||
|
||||
if(i == m_filebuffer.end())
|
||||
break;
|
||||
|
||||
line = *i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// This function saves the value of a keyword. If the value already exists in the file
|
||||
// it will be overwritten.
|
||||
// If something was written the file is also rewinded!
|
||||
void CParser::saveIntValue(const char *keyword, const char *category,int value)
|
||||
{
|
||||
// Three cases:
|
||||
// 1.- category doesn't exist
|
||||
// 2.- category exists, but keyword not
|
||||
// 3.- category and keyword exist, only the value must be changed
|
||||
|
||||
list<char*>::iterator i;
|
||||
|
||||
|
||||
char *line;
|
||||
|
||||
for(i=m_filebuffer.begin() ; i != m_filebuffer.end() ; ++i )
|
||||
{
|
||||
line = *i;
|
||||
if(line[0]=='[') // found category
|
||||
{
|
||||
if(strncmp(line+1,category,strlen(category)) == 0) // is it the category we are looking for?
|
||||
{
|
||||
++i;
|
||||
line = *i;
|
||||
|
||||
// category found !! let's see if, the keyword exists
|
||||
while(line[0]!='[')
|
||||
{
|
||||
if(strncmp(line,keyword,strlen(keyword)) == 0)
|
||||
{
|
||||
// keyword also found!! set the new value!! case 3
|
||||
i = m_filebuffer.erase(i);
|
||||
char *newline;
|
||||
newline = (char*) calloc(256,sizeof(char));
|
||||
sprintf(newline,"%s = %d\n",keyword,value);
|
||||
m_filebuffer.insert(i,newline);
|
||||
return;
|
||||
}
|
||||
++i;
|
||||
|
||||
if(i == m_filebuffer.end())
|
||||
break;
|
||||
|
||||
line = *i;
|
||||
|
||||
}
|
||||
|
||||
// not found! case 2: category found, but no keyword
|
||||
char *newline;
|
||||
newline = (char*) calloc(256,sizeof(char));
|
||||
sprintf(newline,"%s = %d\n",keyword,value);
|
||||
|
||||
if(i != m_filebuffer.end())
|
||||
{
|
||||
--i;
|
||||
m_filebuffer.insert(i,newline);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_filebuffer.push_back(newline);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
// First case: Category doesn't exist! Create a new one, and as the keyword also cannot exist, create it too!
|
||||
char *newline;
|
||||
newline = (char*) calloc(256,sizeof(char));
|
||||
sprintf(newline,"\n");
|
||||
m_filebuffer.insert(m_filebuffer.end(),newline);
|
||||
newline = (char*) calloc(256,sizeof(char));
|
||||
sprintf(newline,"[%s]\n",category);
|
||||
m_filebuffer.insert(m_filebuffer.end(),newline);
|
||||
newline = (char*) calloc(256,sizeof(char));
|
||||
sprintf(newline,"%s = %d\n",keyword,value);
|
||||
m_filebuffer.insert(m_filebuffer.end(),newline);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
50
src/fileio/CParser.h
Normal file
50
src/fileio/CParser.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* CParser.h
|
||||
*
|
||||
* Created on: 22.05.2009
|
||||
* Author: gerstrong
|
||||
*/
|
||||
|
||||
#ifndef CPARSER_H_
|
||||
#define CPARSER_H_
|
||||
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
using namespace std;
|
||||
|
||||
|
||||
class CParser {
|
||||
public:
|
||||
CParser();
|
||||
virtual ~CParser();
|
||||
|
||||
|
||||
// replaced by sscanf, since that function is slower and leaks memory
|
||||
void parseline(char *p_input,char *p_output, int pos, int size);
|
||||
|
||||
// Opena the text which can be parsed. This will read all the stuff in to the memory.
|
||||
bool loadParseFile(void);
|
||||
|
||||
// Close the text which was parsed. This will also copy all stuff of the memory to the file.
|
||||
bool saveParseFile(void);
|
||||
|
||||
// read the value of the to be seeked keyword and returns it as an int.
|
||||
// If no value was detected, it returns -1;
|
||||
// If something was detected, the file is also rewinded!
|
||||
int getIntValue(const char *keyword, const char *category);
|
||||
|
||||
// This function saves the value of a keyword. If the value already exists in the file
|
||||
// it will be overwritten.
|
||||
// If something was written the file is also rewinded!
|
||||
void saveIntValue(const char *keyword, const char *category, int value);
|
||||
|
||||
bool isOpen(void) {return m_isOpen;}
|
||||
|
||||
private:
|
||||
bool m_isOpen;
|
||||
|
||||
list<char*> m_filebuffer;
|
||||
|
||||
};
|
||||
|
||||
#endif /* CPARSER_H_ */
|
||||
150
src/fileio/lzexe.cpp
Normal file
150
src/fileio/lzexe.cpp
Normal file
@@ -0,0 +1,150 @@
|
||||
/*
|
||||
* lzexe.c
|
||||
*
|
||||
* Created on: 24.01.2009
|
||||
* Author: gerstrong
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int get_bit(int *p_bit_count, FILE **fin)
|
||||
{
|
||||
static unsigned short bits;
|
||||
int bit;
|
||||
|
||||
bit = bits & 1;
|
||||
(*p_bit_count)--;
|
||||
|
||||
if ((*p_bit_count) <= 0)
|
||||
{
|
||||
bits = getc(*fin) | getc(*fin) << 8;
|
||||
|
||||
if ((*p_bit_count) == -1) /* special case for first bit word */
|
||||
{
|
||||
bit = bits & 1;
|
||||
bits >>= 1;
|
||||
}
|
||||
|
||||
(*p_bit_count) += 16;
|
||||
}
|
||||
else
|
||||
bits >>= 1;
|
||||
|
||||
return bit;
|
||||
}
|
||||
|
||||
int getEXEVersion(int episode, int bufsize)
|
||||
{
|
||||
switch (bufsize)
|
||||
{
|
||||
case 99762:
|
||||
if(episode != 1)
|
||||
return -1;
|
||||
else
|
||||
return 110;
|
||||
case 99972:
|
||||
if(episode != 1)
|
||||
return -1;
|
||||
else
|
||||
return 131;
|
||||
|
||||
case 398:
|
||||
if(episode != 1)
|
||||
return -1;
|
||||
else
|
||||
return 134;
|
||||
|
||||
case 118114:
|
||||
if(episode != 2)
|
||||
return -1;
|
||||
else
|
||||
return 100;
|
||||
|
||||
case 118160:
|
||||
|
||||
if(episode != 2)
|
||||
return -1;
|
||||
else
|
||||
return 131;
|
||||
|
||||
case 127086:
|
||||
|
||||
if(episode != 3)
|
||||
return -1;
|
||||
else
|
||||
return 100;
|
||||
|
||||
case 127104:
|
||||
if(episode != 3)
|
||||
return -1;
|
||||
else
|
||||
return 131;
|
||||
|
||||
default: return -2;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// return how much was unpacked or zero if nothing was unpacked
|
||||
int unlzexe(FILE *fin, unsigned char *outbuffer)
|
||||
{
|
||||
int bit_count;
|
||||
short offset;
|
||||
int pos, repeat;
|
||||
|
||||
pos = 0;
|
||||
bit_count = 0;
|
||||
|
||||
/* skip header */
|
||||
fseek(fin, 32, SEEK_SET);
|
||||
|
||||
while (1)
|
||||
{
|
||||
|
||||
if (get_bit(&bit_count, &fin))
|
||||
{
|
||||
outbuffer[pos++] = getc(fin);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (get_bit(&bit_count, &fin))
|
||||
{
|
||||
unsigned char tmp[2];
|
||||
fread(tmp, 1, 2, fin);
|
||||
repeat = (tmp[1] & 0x07);
|
||||
|
||||
offset = ((tmp[1] & ~0x07) << 5) | tmp[0] | 0xE000;
|
||||
|
||||
if (repeat == 0)
|
||||
{
|
||||
repeat = getc (fin);
|
||||
|
||||
|
||||
if (repeat == 0)
|
||||
break;
|
||||
else if (repeat == 1)
|
||||
continue;
|
||||
else
|
||||
repeat++;
|
||||
}
|
||||
else
|
||||
repeat += 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
repeat = ((get_bit(&bit_count, &fin) << 1) | get_bit(&bit_count, &fin)) + 2;
|
||||
offset = getc(fin) | 0xFF00;
|
||||
}
|
||||
|
||||
while (repeat > 0)
|
||||
{
|
||||
outbuffer[pos] = outbuffer[pos + offset];
|
||||
pos++;
|
||||
repeat--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return pos;
|
||||
}
|
||||
76
src/fileio/rle.cpp
Normal file
76
src/fileio/rle.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* rle.c
|
||||
*
|
||||
* Created on: 29.01.2009
|
||||
* Author: gerstrong
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "../funcdefs.h"
|
||||
|
||||
|
||||
int unRLEW(FILE *fp, unsigned int *filebuf)
|
||||
{
|
||||
|
||||
int t,i, howmany, cursize, finsize;
|
||||
/*
|
||||
1.) If implemented, get the first dword in the file, [Final Length]
|
||||
2.) If [Length of data so far] < [Final Length] then:
|
||||
3.) Get a word
|
||||
2.) Is this word $FEFE?
|
||||
-> If yes;
|
||||
Get the next two words (Word1 and Word2)
|
||||
Copy Word1 [Word2] times
|
||||
Move forward three words and got to 2.)
|
||||
-> If no;
|
||||
Copy the word
|
||||
Move forward a word and go to 2.)
|
||||
*/
|
||||
|
||||
cursize = 0;
|
||||
|
||||
rewind(fp);
|
||||
|
||||
|
||||
while(!feof(fp)) // Detect, if the file is really RLEW compressed!
|
||||
{
|
||||
t = fgeti(fp);
|
||||
if(t == 0xFEFE)
|
||||
{
|
||||
cursize = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(cursize == 0)
|
||||
{
|
||||
return -1; // This file is not RLEW compressed!
|
||||
}
|
||||
rewind(fp);
|
||||
|
||||
finsize = fgeti(fp);
|
||||
|
||||
|
||||
while( cursize < finsize )
|
||||
{
|
||||
t = fgeti(fp);
|
||||
if (t == 0xFEFE)
|
||||
{
|
||||
howmany = fgeti(fp);
|
||||
t = fgeti(fp);
|
||||
for(i=0;i<howmany;i++)
|
||||
{
|
||||
filebuf[cursize] = t;
|
||||
cursize++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
filebuf[cursize] = t;
|
||||
cursize++;
|
||||
}
|
||||
}
|
||||
|
||||
return cursize;
|
||||
}
|
||||
110
src/fileio/story.cpp
Normal file
110
src/fileio/story.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* story.c
|
||||
*
|
||||
* Created on: 07.02.2009
|
||||
* Author: gerstrong
|
||||
*
|
||||
* This file handles the database that has be loaded in order to get show
|
||||
* the story. It is only a textfile.. though
|
||||
*/
|
||||
|
||||
#include "../keen.h"
|
||||
#include "../include/fileio/lzexe.h"
|
||||
#include "../include/fileio.h"
|
||||
#include "../CLogFile.h"
|
||||
|
||||
int readStoryText(char **ptext, int episode, char *path)
|
||||
{
|
||||
FILE *fp;
|
||||
char buf[256];
|
||||
char buf2[256];
|
||||
|
||||
memset(buf,0,256*sizeof(char));
|
||||
|
||||
formatPathString(buf2,path);
|
||||
|
||||
sprintf(buf,"%sstorytxt.ck%d",buf2,episode);
|
||||
|
||||
if((fp=fopen(buf,"rt"))==NULL)
|
||||
{
|
||||
sprintf(buf,"%skeen%d.exe",buf2,episode);
|
||||
|
||||
if((fp=fopen(buf,"rb"))!=NULL)
|
||||
{
|
||||
unsigned char *filebuf;
|
||||
int bufsize;
|
||||
|
||||
filebuf = (unsigned char*) malloc(500000*sizeof(unsigned char));
|
||||
|
||||
bufsize = unlzexe(fp, filebuf);
|
||||
|
||||
rewind(fp);
|
||||
|
||||
if ( bufsize == 0 ) // Program was not unpacked, so read it normally
|
||||
{
|
||||
while(!feof(fp))
|
||||
{
|
||||
filebuf[bufsize] = getc(fp);
|
||||
bufsize++;
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
int startflag=0, endflag=0, version=0; // where story begins and ends!
|
||||
|
||||
version = getEXEVersion(episode, bufsize);
|
||||
|
||||
if(episode == 2)
|
||||
{
|
||||
startflag = 92864;
|
||||
endflag = 96088;
|
||||
}
|
||||
if(episode == 3)
|
||||
{
|
||||
startflag = 101328;
|
||||
endflag = 104435;
|
||||
}
|
||||
|
||||
if(startflag == 0 || endflag == 0)
|
||||
{
|
||||
g_pLogFile->textOut(PURPLE,"Sorry, but your exe-file is not compatible for reading the story.<br>");
|
||||
}
|
||||
else
|
||||
{
|
||||
*ptext = (char*) malloc((endflag-startflag)*sizeof(char));
|
||||
strncpy((*ptext),(char*)filebuf+startflag,(endflag-startflag)*sizeof(char));
|
||||
}
|
||||
|
||||
free(filebuf);
|
||||
|
||||
return (endflag-startflag);
|
||||
}
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
int pos;
|
||||
int filesize;
|
||||
|
||||
pos = ftell (fp);
|
||||
fseek (fp, 0, SEEK_END);
|
||||
filesize = ftell (fp);
|
||||
fseek (fp, pos, SEEK_SET);
|
||||
|
||||
pos = 0;
|
||||
|
||||
*ptext = (char*) malloc(filesize*sizeof(char));
|
||||
|
||||
while(!feof(fp))
|
||||
{
|
||||
(*ptext)[pos] = fgetc(fp);
|
||||
pos++;
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return filesize;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user