1 /*
2  # This file is part of the Astrometry.net suite.
3  # Licensed under a 3-clause BSD style license - see LICENSE
4  */
5 
6 #ifndef IOUTILS_H
7 #define IOUTILS_H
8 
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <sys/types.h>
12 
13 //# Modified by Robert Lancaster for the StellarSolver Internal Library
14 #ifdef _WIN32
15 //These types are not included on windows
16 typedef unsigned long int ulong;
17 typedef unsigned short int ushort;
18 typedef unsigned int uint;
19 #endif
20 
21 #ifdef _MSC_VER //# Modified by Robert Lancaster for the StellarSolver Internal Library
22 char* basename(const char* path);
23 char* dirname(const char* path);
24 #endif
25 
26 #include <time.h>
27 
28 #include "astrometry/an-bool.h"
29 #include "astrometry/bl.h"
30 #include "astrometry/keywords.h"
31 
32 extern uint32_t ENDIAN_DETECTOR;
33 
34 int copy_file(const char* infn, const char* outfn);
35 
36 int pad_fid(FILE* fid, size_t len, char pad);
37 int pad_file(char* filename, size_t len, char pad);
38 
39 /*
40  The "basename" function may overwrite its arg and may return a pointer
41  to static memory... both undesirable.  This replacement returns a newly-
42  allocated string containing the result.
43  */
44 Malloc
45 char* basename_safe(const char* path);
46 
47 Malloc
48 char* dirname_safe(const char* path);
49 
50 // Returns (system + user) CPU time, in seconds.
51 #ifndef _WIN32 //# Modified by Robert Lancaster for the StellarSolver Internal Library
52 float get_cpu_usage(void);
53 #endif
54 
55 //int log_resource_usage(int loglevel);
56 
57 /*
58  Searches for the given "filename" in the given set of directories.
59  Returns a newly allocated string "dir/filename", or NULL if none of
60  the paths exists and is readable.
61  */
62 char* find_file_in_dirs(const char** dirs, int ndirs, const char* filename, anbool allow_absolute);
63 
64 // Are strings s1 and s2 equal?
65 anbool streq(const char* s1, const char* s2);
66 anbool strcaseeq(const char* s1, const char* s2);
67 
68 /*
69  Copy data from "fin" to "fout", starting at offset "offset"
70  (from the beginning of "fin"), and length "length".
71  Returns 0 on success.
72  */
73 int pipe_file_offset(FILE* fin, off_t offset, off_t length, FILE* fout);
74 
75 int write_file(const char* fn, const char* data, int len);
76 
77 /*
78  It's not really _safe_ as such, it just prints an error message if it fails...
79  */
80 void
81 ATTRIB_FORMAT(printf,2,3)
82     asprintf_safe(char** strp, const char* format, ...);
83 
84 //# Modified by Robert Lancaster for the StellarSolver Internal Library
85 //int run_command_get_outputs(const char* cmd, sl** outlines, sl** errlines);
86 
87 void get_mmap_size(size_t start, size_t size, off_t* mapstart, size_t* mapsize, int* pgap);
88 
89 #ifdef _WIN32 //# Modified by Robert Lancaster for the StellarSolver Internal Library
90 char* mmap_file(int fildes, off_t mapsize);
91 #endif
92 
93 // If "dir" is NULL, create temp file in $TMP, or /tmp if not set.
94 char* create_temp_file(const char* fn, const char* dir);
95 
96 // If "dir" is NULL, create temp file in $TMP, or /tmp if not set.
97 //# Modified by Robert Lancaster for the StellarSolver Internal Library
98 //char* create_temp_dir(const char* name, const char* dir);
99 
100 char* shell_escape(const char* str);
101 
102 int mkdir_p(const char* path);
103 
104 // Returns 0 on error.
105 time_t file_get_last_modified_time(const char* fn);
106 
107 anbool file_exists(const char* fn);
108 
109 anbool file_readable(const char* fn);
110 
111 anbool file_executable(const char* fn);
112 
113 anbool path_is_dir(const char* path);
114 
115 void* file_get_contents(const char* fn, size_t* len, anbool addzero);
116 
117 char* file_get_contents_offset(const char* fn, int offset, int length);
118 
119 sl* fid_add_lines(FILE* fid, anbool include_newlines, sl* list);
120 
121 sl* file_get_lines(const char* fn, anbool include_newlines);
122 
123 sl* fid_get_lines(FILE* fid, anbool include_newlines);
124 
125 //# Modified by Robert Lancaster for the StellarSolver Internal Library
126 //sl* dir_get_contents(const char* path, sl* result, anbool filesonly, anbool recursive);
127 
128 //# Modified by Robert Lancaster for the StellarSolver Internal Library
129 //int file_get_last_modified_string(const char* fn, const char* timeformat,
130 //                                  anbool utc, char* output, size_t outsize);
131 
132 /**
133  Splits the given "str" into words, so that the first line is at most
134  "firstlinew" long.  Subsequent lines have length <= "linew".  If
135  "lst" is non-NULL, the words are added into it.  Otherwise a new sl
136  is allocated.
137  */
138 sl* split_long_string(const char* str, int firstlinew, int linew, sl* lst);
139 
140 // Split a string on the first instance of "splitstr".
141 // Places the addresses of (newly-allocated) copies of the first and seconds parts of the string.
142 // Returns 1 if the string is found.
143 int split_string_once(const char* str, const char* splitstr, char** first, char** second);
144 
145 /**
146  If "cmdline" starts with "keyword", returns 1 and places the address of
147  the start of the next word in "p_next_word".
148  */
149 int is_word(const char* cmdline, const char* keyword, char** p_next_word);
150 
151 int starts_with(const char* str, const char* prefix);
152 
153 int ends_with(const char* str, const char* prefix);
154 
155 char* strdup_safe(const char* str);
156 
157 #ifndef _WIN32 //# Modified by Robert Lancaster for the StellarSolver Internal Library
158 void add_sigbus_mmap_warning(void);
159 void reset_sigbus_mmap_warning(void);
160 #endif
161 
162 int write_u8(FILE* fout, unsigned char val);
163 int write_u16(FILE* fout, unsigned int val);
164 int write_u32(FILE* fout, unsigned int val);
165 int write_uints(FILE* fout, unsigned int* val, int n);
166 int write_double(FILE* fout, double val);
167 int write_float(FILE* fout, float val);
168 int write_fixed_length_string(FILE* fout, char* s, int length);
169 int write_string(FILE* fout, char* s);
170 
171 int write_u32_portable(FILE* fout, unsigned int val);
172 int write_u32s_portable(FILE* fout, unsigned int* val, int n);
173 
174 int read_u8(FILE* fin, unsigned char* val);
175 int read_u16(FILE* fout, unsigned int* val);
176 int read_u32(FILE* fin, unsigned int* val);
177 int read_double(FILE* fin, double* val);
178 int read_fixed_length_string(FILE* fin, char* s, int length);
179 char* read_string(FILE* fin);
180 char* read_string_terminated(FILE* fin, const char* terminators, int nterminators,
181                              anbool include_terminator);
182 
183 int read_u32_portable(FILE* fin, unsigned int* val);
184 int read_u32s_portable(FILE* fin, unsigned int* val, int n);
185 
186 struct buffered_read_data {
187     void* buffer;
188     int blocksize;
189     int elementsize;
190     int ntotal;
191     int nbuff;
192     int off;
193     int buffind;
194     int (*refill_buffer)(void* userdata, void* buffer, unsigned int offs, unsigned int nelems);
195     void* userdata;
196 };
197 typedef struct buffered_read_data bread_t;
198 
199 bread_t* buffered_read_new(int elementsize, int Nbuffer, int Ntotal,
200                            int (*refill_buffer)(void* userdata, void* buffer, unsigned int offs, unsigned int nelems),
201                            void* userdata);
202 
203 void* buffered_read(bread_t* buff);
204 
205 void buffered_read_pushback(bread_t* br);
206 
207 void buffered_read_reset(bread_t* br);
208 
209 void buffered_read_free(bread_t* br);
210 
211 void buffered_read_resize(bread_t* br, int newsize);
212 
213 #endif
214