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