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

Revision 10420, 5.5 KB checked in by gmaps, 20 months ago (diff)

tidy gmaps python/tilerender interface

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
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 */
37Malloc
38char* basename_safe(const char* path);
39
40// Returns (system + user) CPU time, in seconds.
41float 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 */
48char* 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 */
59char* an_canonicalize_file_name(const char* fn);
60
61// Are strings s1 and s2 equal?
62bool 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 */
69int 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 */
74void
75ATTRIB_FORMAT(printf,2,3)
76asprintf_safe(char** strp, const char* format, ...);
77
78int run_command_get_outputs(const char* cmd, sl** outlines, sl** errlines);
79
80void get_mmap_size(int start, int size, off_t* mapstart, size_t* mapsize, int* pgap);
81
82char* resolve_path(const char* filename, const char* basedir);
83
84char* find_executable(const char* progname, const char* sibling);
85
86char* create_temp_file(const char* fn, const char* dir);
87
88char* create_temp_dir(const char* name, const char* dir);
89
90char* shell_escape(const char* str);
91
92int mkdir_p(const char* path);
93
94bool file_exists(const char* fn);
95
96bool file_readable(const char* fn);
97
98bool file_executable(const char* fn);
99
100bool path_is_dir(const char* path);
101
102void* file_get_contents(const char* fn, size_t* len, bool addzero);
103
104char* file_get_contents_offset(const char* fn, int offset, int length);
105
106sl* file_get_lines(const char* fn, bool include_newlines);
107
108sl* fid_get_lines(FILE* fid, bool include_newlines);
109
110sl* dir_get_contents(const char* path, sl* result, bool filesonly, bool recursive);
111
112int 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 */
119int is_word(const char* cmdline, const char* keyword, char** p_next_word);
120
121int starts_with(const char* str, const char* prefix);
122
123int ends_with(const char* str, const char* prefix);
124
125char* strdup_safe(const char* str);
126
127void add_sigbus_mmap_warning();
128void reset_sigbus_mmap_warning();
129
130int write_u8(FILE* fout, unsigned char val);
131int write_u16(FILE* fout, unsigned int val);
132int write_u32(FILE* fout, unsigned int val);
133int write_uints(FILE* fout, unsigned int* val, int n);
134int write_double(FILE* fout, double val);
135int write_float(FILE* fout, float val);
136int write_double(FILE* fout, double val);
137int write_fixed_length_string(FILE* fout, char* s, int length);
138int write_string(FILE* fout, char* s);
139
140int write_u32_portable(FILE* fout, unsigned int val);
141int write_u32s_portable(FILE* fout, unsigned int* val, int n);
142
143int read_u8(FILE* fin, unsigned char* val);
144int read_u16(FILE* fout, unsigned int* val);
145int read_u32(FILE* fin, unsigned int* val);
146int read_double(FILE* fin, double* val);
147int read_fixed_length_string(FILE* fin, char* s, int length);
148char* read_string(FILE* fin);
149char* read_string_terminated(FILE* fin, const char* terminators, int nterminators,
150                                                         bool include_terminator);
151
152int read_u32_portable(FILE* fin, unsigned int* val);
153int read_u32s_portable(FILE* fin, unsigned int* val, int n);
154
155struct 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};
166typedef struct buffered_read_data bread_t;
167
168bread_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
172void* buffered_read(bread_t* buff);
173
174void buffered_read_pushback(bread_t* br);
175
176void buffered_read_reset(bread_t* br);
177
178void buffered_read_free(bread_t* br);
179
180void buffered_read_resize(bread_t* br, int newsize);
181
182#endif
Note: See TracBrowser for help on using the repository browser.