| 1 | /* |
|---|
| 2 | This file is part of the Astrometry.net suite. |
|---|
| 3 | Copyright 2006, 2007 Dustin Lang, Keir Mierle and Sam Roweis. |
|---|
| 4 | |
|---|
| 5 | The Astrometry.net suite is free software; you can redistribute |
|---|
| 6 | it and/or modify it under the terms of the GNU General Public License |
|---|
| 7 | as published by the Free Software Foundation, version 2. |
|---|
| 8 | |
|---|
| 9 | The Astrometry.net suite is distributed in the hope that it will be |
|---|
| 10 | useful, but 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. |
|---|
| 13 | |
|---|
| 14 | You should have received a copy of the GNU General Public License |
|---|
| 15 | along with the Astrometry.net suite ; if not, write to the Free Software |
|---|
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #ifndef IOUTILS_H |
|---|
| 20 | #define IOUTILS_H |
|---|
| 21 | |
|---|
| 22 | #include <stdio.h> |
|---|
| 23 | #include <stdint.h> |
|---|
| 24 | #include <sys/types.h> |
|---|
| 25 | |
|---|
| 26 | #include "an-bool.h" |
|---|
| 27 | #include "bl.h" |
|---|
| 28 | #include "keywords.h" |
|---|
| 29 | |
|---|
| 30 | extern uint32_t ENDIAN_DETECTOR; |
|---|
| 31 | |
|---|
| 32 | /* |
|---|
| 33 | The "basename" function may overwrite its arg and may return a pointer |
|---|
| 34 | to static memory... both undesirable. This replacement returns a newly- |
|---|
| 35 | allocated string containing the result. |
|---|
| 36 | */ |
|---|
| 37 | Malloc |
|---|
| 38 | char* basename_safe(const char* path); |
|---|
| 39 | |
|---|
| 40 | // Returns (system + user) CPU time, in seconds. |
|---|
| 41 | float get_cpu_usage(); |
|---|
| 42 | |
|---|
| 43 | /* |
|---|
| 44 | Searches for the given "filename" in the given set of directories. |
|---|
| 45 | Returns a newly allocated string "dir/filename", or NULL if none of |
|---|
| 46 | the paths exists and is readable. |
|---|
| 47 | */ |
|---|
| 48 | char* find_file_in_dirs(const char** dirs, int ndirs, const char* filename, bool allow_absolute); |
|---|
| 49 | |
|---|
| 50 | /* |
|---|
| 51 | Removes '.' and '..' references from a path. |
|---|
| 52 | Collapses '//' to '/'. |
|---|
| 53 | Does NOT care whether the file actually exists. |
|---|
| 54 | Does NOT resolve symlinks. |
|---|
| 55 | Assumes '/' is the path separator. |
|---|
| 56 | |
|---|
| 57 | Returns a newly-allocated string which should be freed with free(). |
|---|
| 58 | */ |
|---|
| 59 | char* an_canonicalize_file_name(const char* fn); |
|---|
| 60 | |
|---|
| 61 | // Are strings s1 and s2 equal? |
|---|
| 62 | bool streq(const char* s1, const char* s2); |
|---|
| 63 | |
|---|
| 64 | /* |
|---|
| 65 | Copy data from "fin" to "fout", starting at offset "offset" |
|---|
| 66 | (from the beginning of "fin"), and length "length". |
|---|
| 67 | Returns 0 on success. |
|---|
| 68 | */ |
|---|
| 69 | int pipe_file_offset(FILE* fin, int offset, int length, FILE* fout); |
|---|
| 70 | |
|---|
| 71 | /* |
|---|
| 72 | It's not really _safe_ as such, it just prints an error message if it fails... |
|---|
| 73 | */ |
|---|
| 74 | void |
|---|
| 75 | ATTRIB_FORMAT(printf,2,3) |
|---|
| 76 | asprintf_safe(char** strp, const char* format, ...); |
|---|
| 77 | |
|---|
| 78 | int run_command_get_outputs(const char* cmd, sl** outlines, sl** errlines); |
|---|
| 79 | |
|---|
| 80 | void get_mmap_size(int start, int size, off_t* mapstart, size_t* mapsize, int* pgap); |
|---|
| 81 | |
|---|
| 82 | char* resolve_path(const char* filename, const char* basedir); |
|---|
| 83 | |
|---|
| 84 | char* find_executable(const char* progname, const char* sibling); |
|---|
| 85 | |
|---|
| 86 | char* create_temp_file(const char* fn, const char* dir); |
|---|
| 87 | |
|---|
| 88 | char* create_temp_dir(const char* name, const char* dir); |
|---|
| 89 | |
|---|
| 90 | char* shell_escape(const char* str); |
|---|
| 91 | |
|---|
| 92 | int mkdir_p(const char* path); |
|---|
| 93 | |
|---|
| 94 | bool file_exists(const char* fn); |
|---|
| 95 | |
|---|
| 96 | bool file_readable(const char* fn); |
|---|
| 97 | |
|---|
| 98 | bool file_executable(const char* fn); |
|---|
| 99 | |
|---|
| 100 | bool path_is_dir(const char* path); |
|---|
| 101 | |
|---|
| 102 | void* file_get_contents(const char* fn, size_t* len, bool addzero); |
|---|
| 103 | |
|---|
| 104 | char* file_get_contents_offset(const char* fn, int offset, int length); |
|---|
| 105 | |
|---|
| 106 | sl* file_get_lines(const char* fn, bool include_newlines); |
|---|
| 107 | |
|---|
| 108 | sl* fid_get_lines(FILE* fid, bool include_newlines); |
|---|
| 109 | |
|---|
| 110 | sl* dir_get_contents(const char* path, sl* result, bool filesonly, bool recursive); |
|---|
| 111 | |
|---|
| 112 | int file_get_last_modified_string(const char* fn, const char* timeformat, |
|---|
| 113 | bool utc, char* output, size_t outsize); |
|---|
| 114 | |
|---|
| 115 | /** |
|---|
| 116 | If "cmdline" starts with "keyword", returns 1 and places the address of |
|---|
| 117 | the start of the next word in "p_next_word". |
|---|
| 118 | */ |
|---|
| 119 | int is_word(const char* cmdline, const char* keyword, char** p_next_word); |
|---|
| 120 | |
|---|
| 121 | int starts_with(const char* str, const char* prefix); |
|---|
| 122 | |
|---|
| 123 | int ends_with(const char* str, const char* prefix); |
|---|
| 124 | |
|---|
| 125 | char* strdup_safe(const char* str); |
|---|
| 126 | |
|---|
| 127 | void add_sigbus_mmap_warning(); |
|---|
| 128 | void reset_sigbus_mmap_warning(); |
|---|
| 129 | |
|---|
| 130 | int write_u8(FILE* fout, unsigned char val); |
|---|
| 131 | int write_u16(FILE* fout, unsigned int val); |
|---|
| 132 | int write_u32(FILE* fout, unsigned int val); |
|---|
| 133 | int write_uints(FILE* fout, unsigned int* val, int n); |
|---|
| 134 | int write_double(FILE* fout, double val); |
|---|
| 135 | int write_float(FILE* fout, float val); |
|---|
| 136 | int write_double(FILE* fout, double val); |
|---|
| 137 | int write_fixed_length_string(FILE* fout, char* s, int length); |
|---|
| 138 | int write_string(FILE* fout, char* s); |
|---|
| 139 | |
|---|
| 140 | int write_u32_portable(FILE* fout, unsigned int val); |
|---|
| 141 | int write_u32s_portable(FILE* fout, unsigned int* val, int n); |
|---|
| 142 | |
|---|
| 143 | int read_u8(FILE* fin, unsigned char* val); |
|---|
| 144 | int read_u16(FILE* fout, unsigned int* val); |
|---|
| 145 | int read_u32(FILE* fin, unsigned int* val); |
|---|
| 146 | int read_double(FILE* fin, double* val); |
|---|
| 147 | int read_fixed_length_string(FILE* fin, char* s, int length); |
|---|
| 148 | char* read_string(FILE* fin); |
|---|
| 149 | char* read_string_terminated(FILE* fin, const char* terminators, int nterminators, |
|---|
| 150 | bool include_terminator); |
|---|
| 151 | |
|---|
| 152 | int read_u32_portable(FILE* fin, unsigned int* val); |
|---|
| 153 | int read_u32s_portable(FILE* fin, unsigned int* val, int n); |
|---|
| 154 | |
|---|
| 155 | struct buffered_read_data { |
|---|
| 156 | void* buffer; |
|---|
| 157 | int blocksize; |
|---|
| 158 | int elementsize; |
|---|
| 159 | int ntotal; |
|---|
| 160 | int nbuff; |
|---|
| 161 | int off; |
|---|
| 162 | int buffind; |
|---|
| 163 | int (*refill_buffer)(void* userdata, void* buffer, unsigned int offs, unsigned int nelems); |
|---|
| 164 | void* userdata; |
|---|
| 165 | }; |
|---|
| 166 | typedef struct buffered_read_data bread_t; |
|---|
| 167 | |
|---|
| 168 | bread_t* buffered_read_new(int elementsize, int Nbuffer, int Ntotal, |
|---|
| 169 | int (*refill_buffer)(void* userdata, void* buffer, unsigned int offs, unsigned int nelems), |
|---|
| 170 | void* userdata); |
|---|
| 171 | |
|---|
| 172 | void* buffered_read(bread_t* buff); |
|---|
| 173 | |
|---|
| 174 | void buffered_read_pushback(bread_t* br); |
|---|
| 175 | |
|---|
| 176 | void buffered_read_reset(bread_t* br); |
|---|
| 177 | |
|---|
| 178 | void buffered_read_free(bread_t* br); |
|---|
| 179 | |
|---|
| 180 | void buffered_read_resize(bread_t* br, int newsize); |
|---|
| 181 | |
|---|
| 182 | #endif |
|---|