ad284b5c9c2f83561e5687312c2329b72568e1f5
[sped.git] / fileutils.asm
1
2 extern printf
3 extern malloc
4 extern realloc
5 extern free
6 extern memset
7
8 global readFile
9 global readLine
10
11 section .data
12     wrongfile_str db `unable to open file, error code: %i\n`, 0x00
13
14 section .text
15
16 ; reads file line by line
17 ; args: filename
18 ; return:
19 ;    eax - pointer to mem
20 ;    ecx - lines read
21 readFile:
22     %define _FILE_NAME  8
23     %define FILE_HANDLE 4
24     %define IS_EOF      8
25     %define LINES_READ  12
26     %define BUF_PTR     16 ; malloced array of strings
27
28     push ebp
29     mov ebp, esp
30     
31     ; allocate vars
32     sub esp, 16
33     mov DWORD [ebp-FILE_HANDLE], 0x00
34     mov DWORD [ebp-IS_EOF], 0x00
35     mov DWORD [ebp-LINES_READ], 0x00
36
37     push 0
38     call malloc 
39     mov [ebp-BUF_PTR], eax
40
41     ; open existing file
42     mov eax, 5
43     mov ebx, [ebp+_FILE_NAME]
44     mov ecx, 0
45     mov edx, 0777
46     int 0x80
47     mov [ebp-FILE_HANDLE], eax
48
49     ; check if file was open successfully
50     cmp eax, 0
51     jge _readFile_loop
52     push eax
53     push wrongfile_str
54     call printf
55     jmp _readFile_exit
56
57     _readFile_loop:
58
59     ; check if eof was reached
60     cmp DWORD [ebp-IS_EOF], 1
61     je _readFile_exit
62
63     push DWORD [ebp-FILE_HANDLE]
64     call readLine 
65
66     mov esi, eax
67     mov [ebp-IS_EOF], ebx
68     
69     ; push esi
70     ; call printf
71
72     ; make string buffer bigger
73     mov eax, DWORD [ebp-LINES_READ]
74     add eax, 1
75     mov ecx, 4
76     mul ecx
77     push eax
78     push DWORD [ebp-BUF_PTR]
79     call realloc
80     mov DWORD [ebp-BUF_PTR], eax
81
82     ; write string to buffer
83     mov eax, [ebp-LINES_READ]
84     mov ecx, 4
85     mul ecx
86     add eax, DWORD [ebp-BUF_PTR]
87     mov [eax], esi
88
89     ; push DWORD [eax]
90     ; call printf
91
92     add DWORD [ebp-LINES_READ], 1
93
94     jmp _readFile_loop
95
96     _readFile_exit:
97     ; close file
98     mov eax, 6
99     mov ebx, [ebp-FILE_HANDLE]
100     int 0x80
101
102     mov eax, [ebp-BUF_PTR]
103     mov ecx, [ebp-LINES_READ]
104
105     %undef _FILE_NAME
106     %undef FILE_HANDLE
107     %undef IS_EOF
108     %undef LINES_READ
109     %undef BUF_PTR
110
111     mov esp, ebp
112     pop ebp
113     ret
114
115
116 ; reads a line until newline character is reached
117 ; args: file_handle
118 ; return:
119 ;   eax: location to buffer
120 ;   ebx: contains eof
121 ;   ecx: number of chars read
122 readLine:
123     %define _FILE_HANDLE 8
124     %define CHAR_COUNT   4   ; count number of characters read
125     %define BLOCK_COUNT  8   ; number of 64 blocks we've read
126     %define STR_PTR      12  ; malloced buffer to store read string
127
128     push ebp
129     mov ebp, esp
130     
131     ; allocate vars
132     sub esp, 12
133     mov DWORD [ebp-CHAR_COUNT], 0x00
134     mov DWORD [ebp-BLOCK_COUNT], 0x00
135
136     push 64
137     call malloc
138     mov [ebp-STR_PTR], eax
139
140     push DWORD [ebp-STR_PTR]
141     push 0x00
142     push 64
143
144     _readLine_loop:
145     ; if buffer is full
146     cmp BYTE [ebp-CHAR_COUNT], 63 ; leave one byte for null byte
147     jne _readLine_notfull
148     jmp _readLine_exit
149
150     _readLine_notfull:
151     ; read a single character
152     mov eax, 3
153     mov ebx, [ebp+_FILE_HANDLE]
154     mov ecx, [ebp-STR_PTR]
155     add ecx, [ebp-CHAR_COUNT]
156     mov edx, 1
157     int 0x80
158
159     ; mov eax, 4
160     ; mov ebx, 1
161     ; mov ecx, [ebp-STR_PTR]
162     ; add ecx, [ebp-CHAR_COUNT]
163     ; mov edx, 1
164     ; int 0x80
165     
166     ; check for eof
167     cmp eax, 0 ; eax has zero on eof
168     jne _readLine_not_eof
169     mov ebx, 1
170     jmp _readLine_exit 
171     _readLine_not_eof:
172
173     ; check for newline
174     mov eax, [ebp-STR_PTR]
175     add eax, [ebp-CHAR_COUNT]
176     cmp DWORD [eax], 0x0a
177     jne _readLine_not_newline
178     mov ebx, 0
179     jmp _readLine_exit
180     _readLine_not_newline:
181
182     add DWORD [ebp-CHAR_COUNT], 1
183     jmp _readLine_loop
184
185     _readLine_exit:
186
187     mov eax, [ebp-BLOCK_COUNT]
188     mov ecx, 63
189     mul ecx
190     add eax, [ebp-CHAR_COUNT]
191     mov ecx,eax
192
193     mov eax, DWORD [ebp-STR_PTR]
194
195     %undef _FILE_HANDLE
196     %undef CHAR_COUNT
197     %undef BLOCK_COUNT 
198     %undef STR_PTR
199
200     mov esp, ebp
201     pop ebp
202     ret
203