| 1 | /* |
| 2 | * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ |
| 3 | * |
| 4 | * Permission to use, copy, modify, and/or distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
| 17 | /** |
| 18 | * @file |
| 19 | * |
| 20 | * File access utils. |
| 21 | */ |
| 22 | #ifndef _USUAL_FILEUTIL_H_ |
| 23 | #define _USUAL_FILEUTIL_H_ |
| 24 | |
| 25 | #include <usual/base.h> |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | |
| 29 | /** Info about mapped file */ |
| 30 | struct MappedFile { |
| 31 | int fd; |
| 32 | unsigned len; |
| 33 | void *ptr; |
| 34 | }; |
| 35 | |
| 36 | /** Signature for per-line callback */ |
| 37 | typedef bool (*procline_cb)(void *arg, const char *line, ssize_t len); |
| 38 | |
| 39 | /** Read file into memory */ |
| 40 | void *load_file(const char *fn, size_t *len_p); |
| 41 | |
| 42 | /** Loop over lines in file */ |
| 43 | bool foreach_line(const char *fn, procline_cb proc_line, void *arg); |
| 44 | |
| 45 | /** Get file size */ |
| 46 | ssize_t file_size(const char *fn); |
| 47 | |
| 48 | /** Map file into memory */ |
| 49 | int map_file(struct MappedFile *m, const char *fname, int rw) _MUSTCHECK; |
| 50 | |
| 51 | /** Unmap previously mapped file */ |
| 52 | void unmap_file(struct MappedFile *m); |
| 53 | |
| 54 | #if !defined(HAVE_GETLINE) |
| 55 | #define getline(a,b,c) compat_getline(a,b,c) |
| 56 | |
| 57 | /** |
| 58 | * Compat: Read line from file |
| 59 | */ |
| 60 | int getline(char **line_p, size_t *size_p, void *f); |
| 61 | |
| 62 | #endif |
| 63 | |
| 64 | |
| 65 | #endif |