2 * Acess Text Editor (ATE)
3 * - By John Hodge (thePowersGang)
6 * - File Loading / Manipulation
9 #include "include/file.h"
10 #include "include/syntax.h"
13 static const int MAXLINE = 1024;
14 static const int LINEALLOCSIZE = 64;
19 tFile *ret = calloc(1, sizeof(tFile) + 1);
21 ret->FirstLine = NULL;
27 tFile *File_Load(const char *Path)
29 FILE *fp = fopen(Path, "r");
34 // Create file structure
35 tFile *ret = calloc(1, sizeof(tFile) + strlen(Path) + 1);
38 const char *lastslash = strrchr(Path, '/');
39 ret->NameOfs = (lastslash ? lastslash - Path + 1 : 0);
40 strcpy(ret->Path, Path);
45 tFileLine *lastline = NULL;
46 while( fgets(tmpbuf, MAXLINE, fp) )
48 tFileLine *new = malloc(sizeof(tFileLine));
52 if(lastline) lastline->Next = new;
53 new->Length = strlen(tmpbuf);
54 new->Space = (new->Length + LINEALLOCSIZE-1) & ~(LINEALLOCSIZE-1);
56 new->Data = malloc(new->Space);
58 memcpy(new->Data, tmpbuf, new->Length);
66 int File_Save(tFile *File)
73 int File_Close(tFile *File, int bDiscard)
75 if( file->bIsDirty && !bDiscard )
80 while( file->FirstLine )
82 tFileLine *next = file->FirstLine->Next;
83 // TODO: Highlighting free
84 free(file->FirstLine->Data);
85 free(file->FirstLine);
86 file->FirstLine = next;
94 int File_InsertBytes(tFile *File, void *Buffer, size_t Bytes)
99 int File_Delete(tFile *File, enum eFile_DeleteType Type)
104 * Amt = INT_MAX : End of file
105 * Amt = INT_MIN : Start of file
107 int File_CursorDown(tFile *File, int Amount)
112 * |Amt| = 1 : Single character
114 * |Amt| = 3 : Start/End of line
116 int File_CursorRight(tFile *File, int Amount)
120 void *File_GetAbsLine(tFile *File, unsigned int LineNum)
122 tFileLine *line = File->FirstLine;
123 while( LineNum-- && line )
128 void *File_GetRelLine(tFile *File, unsigned int LinesBeforeCurrent)
130 tFileLine *line = File->CurrentLine;
131 while(LinesBeforeCurrent -- && line->Prev)
136 void *File_GetRenderedData(tFile *File, void *Handle, size_t const char **StringPtr)
140 tFileLine *line = Handle;
142 *StringPtr = (line->Rendered ? line->Rendered : line->Data);