X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FApplications%2Fgui_ate_src%2Fedit.c;h=554b7ccec2228c79ff232a854f3ec589f8d0ec2d;hb=98bd9c0c8985c50c42231c116a4e18fedd47761e;hp=8e1f01ee30e28a14dc9eecf3b535655344351ab1;hpb=04a050f42807686dc119838c82372409246d55bb;p=tpg%2Facess2.git diff --git a/Usermode/Applications/gui_ate_src/edit.c b/Usermode/Applications/gui_ate_src/edit.c index 8e1f01ee..554b7cce 100644 --- a/Usermode/Applications/gui_ate_src/edit.c +++ b/Usermode/Applications/gui_ate_src/edit.c @@ -8,14 +8,17 @@ #include #include "include/file.h" #include "include/syntax.h" +#include + +static const int MAXLINE = 1024; +static const int LINEALLOCSIZE = 64; // === CODE === tFile *File_New(void) { - tFile *ret = malloc(sizeof(tFile) + 1); - ret->Handle = NULL; + tFile *ret = calloc(1, sizeof(tFile) + 1); ret->nLines = 0; - ret->Lines = NULL; + ret->FirstLine = NULL; ret->NameOfs = 0; ret->Path[0] = 0; return ret; @@ -23,7 +26,41 @@ tFile *File_New(void) tFile *File_Load(const char *Path) { - return NULL; + FILE *fp = fopen(Path, "r"); + if( !fp ) { + return NULL; + } + + // Create file structure + tFile *ret = calloc(1, sizeof(tFile) + strlen(Path) + 1); + assert(ret); + + const char *lastslash = strrchr(Path, '/'); + ret->NameOfs = (lastslash ? lastslash - Path + 1 : 0); + strcpy(ret->Path, Path); + + // Read in lines + int nLines = 0; + char tmpbuf[MAXLINE]; + tFileLine *lastline = NULL; + while( fgets(tmpbuf, MAXLINE, fp) ) + { + tFileLine *new = malloc(sizeof(tFileLine)); + assert(new); + new->Prev = lastline; + new->Next = NULL; + if(lastline) lastline->Next = new; + new->Length = strlen(tmpbuf); + new->Space = (new->Length + LINEALLOCSIZE-1) & ~(LINEALLOCSIZE-1); + + new->Data = malloc(new->Space); + assert(new->Data); + memcpy(new->Data, tmpbuf, new->Length); + nLines ++; + } + ret->nLines = nLines; + + return ret; } int File_Save(tFile *File) @@ -35,11 +72,74 @@ int File_Save(tFile *File) int File_Close(tFile *File, int bDiscard) { - //if( file->bIsDirty && !bDiscard ) - // return 1; + if( file->bIsDirty && !bDiscard ) + return 1; if( file->Handle ) fclose(File->Handle); + + while( file->FirstLine ) + { + tFileLine *next = file->FirstLine->Next; + // TODO: Highlighting free + free(file->FirstLine->Data); + free(file->FirstLine); + file->FirstLine = next; + } + + free(file); return 0; } +int File_InsertBytes(tFile *File, void *Buffer, size_t Bytes) +{ + +} + +int File_Delete(tFile *File, enum eFile_DeleteType Type) +{ +} + +/** + * Amt = INT_MAX : End of file + * Amt = INT_MIN : Start of file + */ +int File_CursorDown(tFile *File, int Amount) +{ +} + +/** + * |Amt| = 1 : Single character + * |Amt| = 2 : Word + * |Amt| = 3 : Start/End of line + */ +int File_CursorRight(tFile *File, int Amount) +{ +} + +void *File_GetAbsLine(tFile *File, unsigned int LineNum) +{ + tFileLine *line = File->FirstLine; + while( LineNum-- && line ) + line = line->Next; + return line; +} + +void *File_GetRelLine(tFile *File, unsigned int LinesBeforeCurrent) +{ + tFileLine *line = File->CurrentLine; + while(LinesBeforeCurrent -- && line->Prev) + line = line->Prev; + return line; +} + +void *File_GetRenderedData(tFile *File, void *Handle, size_t const char **StringPtr) +{ + if( !Handle ) + return NULL; + tFileLine *line = Handle; + if( StringPtr ) + *StringPtr = (line->Rendered ? line->Rendered : line->Data); + return line->Next; +} +