161 lines
3.6 KiB
C++
161 lines
3.6 KiB
C++
#include "TXT_Manager.h"
|
|
|
|
|
|
|
|
/* This function gets the text between two positions in a text file */
|
|
bool getText(char* filename, long startPos, long endPos, char* output)
|
|
{
|
|
FILE* file = CC_fopen(filename,"rb");
|
|
if(!file) return false;
|
|
fseek(file, startPos, SEEK_SET);
|
|
fread(output, 1, endPos-startPos, file);
|
|
output[endPos-startPos] = '\0';
|
|
CC_fclose(file);
|
|
return true;
|
|
}
|
|
|
|
/* This function gets the position of a text in a text file */
|
|
long findText(char* filename, long from, char* text)
|
|
{
|
|
FILE* file = CC_fopen(filename,"rb");
|
|
if(!file)
|
|
return -1;
|
|
|
|
int nbBr = 0;
|
|
char* ptrText;
|
|
ptrText = strpbrk(text, "\n");
|
|
while(ptrText)
|
|
{
|
|
nbBr++;
|
|
ptrText = strpbrk(ptrText+1, "\n");
|
|
}
|
|
|
|
long pos = -1;
|
|
int neededSizeForBuffer = strlen(text)+1;
|
|
char* buffer = (char*)ZeroAlloc(neededSizeForBuffer);
|
|
fseek(file, from, SEEK_SET);
|
|
fgets(buffer, neededSizeForBuffer, file);
|
|
if(strcmp(text, buffer)==0)
|
|
pos = ftell(file)-strlen(text)-nbBr;
|
|
else
|
|
{
|
|
int c;
|
|
while((c=fgetc(file))!= EOF)
|
|
{
|
|
//permutation
|
|
for(int i=0; i< strlen(text)-1; i++)
|
|
buffer[i] = buffer[i+1];
|
|
buffer[neededSizeForBuffer-2] = (char)c;
|
|
buffer[neededSizeForBuffer-1] = '\0';
|
|
//vérification de l'égalité des 2 chaines
|
|
if(strcmp(text, buffer)==0)
|
|
{
|
|
pos = ftell(file)-strlen(text)-nbBr;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
CC_free(buffer);
|
|
CC_fclose(file);
|
|
return pos;
|
|
}
|
|
|
|
/* This function inserts a text in a text file */
|
|
bool insertText(char* filename, long pos, char* text)
|
|
{
|
|
remove("tmp.txt");
|
|
FILE* tmp = CC_fopen("tmp.txt","wb");
|
|
FILE* file = CC_fopen(filename,"rb");
|
|
if(!file) return false;
|
|
|
|
char buffer[1024];
|
|
long currentBytesRead;
|
|
long totalBytesRead = 0;
|
|
//copie du début du fichier
|
|
fseek(file, 0, SEEK_SET);
|
|
do
|
|
{
|
|
currentBytesRead = fread(buffer, 1, min(pos-totalBytesRead,1024), file);
|
|
fwrite(buffer, 1, currentBytesRead, tmp);
|
|
totalBytesRead += currentBytesRead;
|
|
}
|
|
while(currentBytesRead!=0 && totalBytesRead!=pos);
|
|
//insertion
|
|
fwrite(text, 1, strlen(text), tmp);
|
|
//copie de la fin du fichier
|
|
fseek(file, pos, SEEK_SET);
|
|
do
|
|
{
|
|
currentBytesRead = fread(buffer, 1, 1024, file);
|
|
fwrite(buffer, 1, currentBytesRead, tmp);
|
|
totalBytesRead += currentBytesRead;
|
|
}
|
|
while(currentBytesRead!=0);
|
|
CC_fclose(file);
|
|
CC_fclose(tmp);
|
|
|
|
remove(filename);
|
|
rename("tmp.txt", filename);
|
|
return true;
|
|
}
|
|
|
|
/* This function deletes a text in a text file */
|
|
bool deleteText(char* filename, long startPos, long endPos)
|
|
{
|
|
//précaution
|
|
if(startPos==-1 || endPos==-1)
|
|
return false;
|
|
|
|
remove("tmp.txt");
|
|
FILE* tmp = CC_fopen("tmp.txt","wb");
|
|
FILE* file = CC_fopen(filename,"rb");
|
|
if(!file) return false;
|
|
|
|
char buffer[1024];
|
|
long currentBytesRead;
|
|
long totalBytesRead = 0;
|
|
do
|
|
{
|
|
currentBytesRead = fread(buffer, 1, min(startPos-totalBytesRead,1024), file);
|
|
fwrite(buffer, 1, currentBytesRead, tmp);
|
|
totalBytesRead += currentBytesRead;
|
|
}
|
|
while(currentBytesRead!=0 && totalBytesRead!=startPos);
|
|
fseek(file, endPos, SEEK_SET);
|
|
do
|
|
{
|
|
currentBytesRead = fread(buffer, 1, 1024, file);
|
|
fwrite(buffer, 1, currentBytesRead, tmp);
|
|
totalBytesRead += currentBytesRead;
|
|
}
|
|
while(currentBytesRead!=0);
|
|
CC_fclose(file);
|
|
CC_fclose(tmp);
|
|
|
|
remove(filename);
|
|
rename("tmp.txt", filename);
|
|
return true;
|
|
}
|
|
|
|
/* This function gets the length of a text file */
|
|
long lengthOfFile(char* filename)
|
|
{
|
|
FILE* file = CC_fopen(filename, "rb");
|
|
if(!file) return -1;
|
|
|
|
long length = -1;
|
|
fseek(file, 0L, SEEK_END);
|
|
length = ftell(file);
|
|
CC_fclose(file);
|
|
return length;
|
|
}
|
|
|
|
/* This function says if a text file exists */
|
|
bool fileExists(char* filename)
|
|
{
|
|
FILE* file = CC_fopen(filename, "rb");
|
|
if(!file) return false;
|
|
CC_fclose(file);
|
|
return true;
|
|
}
|