2 * Copyright 2004 Timo Hirvonen
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 #include <file_load.h>
28 int file_load(const char *filename
,
29 void (*handle_line
)(void *data
, const char *line
), void *data
)
32 size_t buffer_size
= 4096;
36 fd
= open(filename
, O_RDONLY
);
39 buffer
= xnew(char, buffer_size
);
41 int i
, start
, len
, rc
;
43 rc
= read(fd
, buffer
+ pos
, buffer_size
- pos
);
54 /* handle line without '\n' */
56 handle_line(data
, buffer
);
66 if (buffer
[start
+ i
] == '\n') {
67 buffer
[start
+ i
] = 0;
68 handle_line(data
, buffer
+ start
);
77 /* line doesn't fit into the buffer */
79 buffer
= xrenew(char, buffer
, buffer_size
);
81 /* len - start bytes left */
82 memmove(buffer
, buffer
+ start
, len
);