/*********************************************************************************
source of "nwriter", a basic text-editor for the Nspire; last modified 21.04.11
Copyright (C) 2011 Olivier Thill alias shrear
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 3 of the License, or
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, see .
*********************************************************************************/
#include
#include "store.h"
int store_char(char character, char* store, int index)
{
//if at the end of text append character
if ( store[index] == 0 )
{
//store char
store[index] = character ;
index++ ;
}
//if in the text move later characters one up
else
{
//local variables
char character_temp ;
char character_store ;
int for_control ;
//character "\0" is delete
if ( character == 0 )
for ( for_control = index ; store[for_control] != 0 ; for_control++ )
store[for_control] = store[for_control+1] ;
else
{
character_temp = character;
for ( for_control = index ; character_temp != 0 ; for_control++ )
{
character_store = store[for_control] ;
store[for_control] = character_temp ;
character_temp = character_store ;
}
index++ ;
}
}
return index ;
}
void save_file(char* store, int index_max, char* file_path)
{
//local variables
FILE* file ;
file = fopen(file_path, "wb") ;
fwrite (store, 1, index_max, file) ;
fclose(file) ;
return ;
}
int load_file(char* store, int store_size, char* file_path)
{
//local variables
FILE* file ;
int index ;
//reset target store
memset(store, 0, store_size) ;
//read file
file = fopen(file_path, "rb") ;
fread(store, 1, store_size, file) ;
fclose(file) ;
//get lenght of new data
index = strlen(store) ;
return index ;
}