source: trunk/src/astrometry/util/ioutils.h @ 12099

Revision 12099, 5.6 KB checked in by dstn, 15 months ago (diff)

add pad-file

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