Merge branch 'master' of git://git.ucc.asn.au/tpg/acess2
[tpg/acess2.git] / Usermode / Applications / gui_ate_src / edit.c
1 /*
2  * Acess Text Editor (ATE)
3  * - By John Hodge (thePowersGang)
4  *
5  * edit.c
6  * - File Loading / Manipulation
7  */
8 #include <stdio.h>
9 #include "include/file.h"
10 #include "include/syntax.h"
11 #include <assert.h>
12
13 static const int MAXLINE = 1024;
14 static const int LINEALLOCSIZE = 64;
15
16 // === CODE ===
17 tFile *File_New(void)
18 {
19         tFile *ret = calloc(1, sizeof(tFile) + 1);
20         ret->nLines = 0;
21         ret->FirstLine = NULL;
22         ret->NameOfs = 0;
23         ret->Path[0] = 0;
24         return ret;
25 }
26
27 tFile *File_Load(const char *Path)
28 {
29         FILE    *fp = fopen(Path, "r");
30         if( !fp ) {
31                 return NULL;
32         }
33
34         // Create file structure
35         tFile   *ret = calloc(1, sizeof(tFile) + strlen(Path) + 1);
36         assert(ret);
37
38         const char *lastslash = strrchr(Path, '/');
39         ret->NameOfs = (lastslash ? lastslash - Path + 1 : 0);
40         strcpy(ret->Path, Path);
41
42         // Read in lines
43         int nLines = 0;
44         char    tmpbuf[MAXLINE];
45         tFileLine       *lastline = NULL;
46         while( fgets(tmpbuf, MAXLINE, fp) )
47         {
48                 tFileLine       *new = malloc(sizeof(tFileLine));
49                 assert(new);
50                 new->Prev = lastline;
51                 new->Next = NULL;
52                 if(lastline)    lastline->Next = new;
53                 new->Length = strlen(tmpbuf);
54                 new->Space = (new->Length + LINEALLOCSIZE-1) & ~(LINEALLOCSIZE-1);
55                 
56                 new->Data = malloc(new->Space);
57                 assert(new->Data);
58                 memcpy(new->Data, tmpbuf, new->Length);
59                 nLines ++;
60         }
61         ret->nLines = nLines;
62
63         return ret;
64 }
65
66 int File_Save(tFile *File)
67 {
68         
69         //file->bIsDirty = 0;
70         return -1;
71 }
72
73 int File_Close(tFile *File, int bDiscard)
74 {
75         if( file->bIsDirty && !bDiscard )
76                 return 1;
77         if( file->Handle )
78                 fclose(File->Handle);
79
80         while( file->FirstLine )
81         {
82                 tFileLine       *next = file->FirstLine->Next;
83                 // TODO: Highlighting free
84                 free(file->FirstLine->Data);
85                 free(file->FirstLine);
86                 file->FirstLine = next;
87         }
88         
89         free(file);
90         
91         return 0;
92 }
93
94 int File_InsertBytes(tFile *File, void *Buffer, size_t Bytes)
95 {
96         
97 }
98
99 int File_Delete(tFile *File, enum eFile_DeleteType Type)
100 {
101 }
102
103 /**
104  * Amt = INT_MAX : End of file
105  * Amt = INT_MIN : Start of file
106  */
107 int File_CursorDown(tFile *File, int Amount)
108 {
109 }
110
111 /**
112  * |Amt| = 1 : Single character
113  * |Amt| = 2 : Word
114  * |Amt| = 3 : Start/End of line
115  */
116 int File_CursorRight(tFile *File, int Amount)
117 {
118 }
119
120 void *File_GetAbsLine(tFile *File, unsigned int LineNum)
121 {
122         tFileLine       *line = File->FirstLine;
123         while( LineNum-- && line )
124                 line = line->Next;
125         return line;
126 }
127
128 void *File_GetRelLine(tFile *File, unsigned int LinesBeforeCurrent)
129 {
130         tFileLine       *line = File->CurrentLine;
131         while(LinesBeforeCurrent -- && line->Prev)
132                 line = line->Prev;
133         return line;
134 }
135
136 void *File_GetRenderedData(tFile *File, void *Handle, size_t const char **StringPtr)
137 {
138         if( !Handle )
139                 return NULL;
140         tFileLine       *line = Handle;
141         if( StringPtr )
142                 *StringPtr = (line->Rendered ? line->Rendered : line->Data);
143         return line->Next;
144 }
145

UCC git Repository :: git.ucc.asn.au