STIR  6.2.0
utilities.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2000 PARAPET partners
3  Copyright (C) 2000-2010, Hammersmith Imanet Ltd
4  This file is part of STIR.
5 
6  SPDX-License-Identifier: Apache-2.0 AND License-ref-PARAPET-license
7 
8  See STIR/LICENSE.txt for details
9 */
10 #ifndef __stir_UTILITIES_H__
11 #define __stir_UTILITIES_H__
12 
20 #include "stir/common.h"
21 #include <stdio.h>
22 #include <iostream>
23 #include <string>
24 
25 START_NAMESPACE_STIR
26 
27 /******************************************************/
32 template <class NUMBER>
34 inline NUMBER ask_num(const std::string& prompt, NUMBER minimum_value, NUMBER maximum_value, NUMBER default_value);
35 
37 std::string ask_string(const std::string& prompt, const std::string& default_value = "");
38 
54 bool ask(const std::string& prompt, bool default_value);
55 
56 /***** filename functions ****/
57 
74 char* ask_filename_with_extension(char* file_in_directory_name, const std::string& prompt, const std::string& default_extension);
75 
87 std::string ask_filename_with_extension(const std::string& prompt, const std::string& default_extension);
88 
102 // Implementation note: gcc 2.8.1 seems to have problems with default
103 // values when templates are around, so I overload the function
104 template <class FSTREAM>
106  FSTREAM& s, const std::string& prompt, const std::string& default_extension, std::ios::openmode mode, bool abort_if_failed);
107 
109 template <class FSTREAM>
110 void inline ask_filename_and_open(FSTREAM& s,
111  const std::string& prompt,
112  const std::string& default_extension,
113  std::ios::openmode mode)
114 {
115  ask_filename_and_open(s, prompt, default_extension, mode, true);
116 }
117 
119 
120 /******************************************************/
136 void* read_stream_in_memory(std::istream& input, std::streamsize& file_size);
137 
145 std::streamsize find_remaining_size(std::istream& input);
146 
148 
149 template <class IFSTREAM>
150 inline IFSTREAM& open_read_binary(IFSTREAM& s, const std::string& name);
152 
153 FILE*& open_read_binary(FILE*& fptr, const std::string& name);
154 
156 
160 template <class OFSTREAM>
161 inline OFSTREAM& open_write_binary(OFSTREAM& s, const std::string& name);
163 
164 FILE*& open_write_binary(FILE*& fptr, const std::string& name);
165 
167 
173 template <class FSTREAM>
174 inline void close_file(FSTREAM& s);
175 
177 
181 void close_file(FILE*& fptr);
182 
184 
191 const int max_filename_length = 1000;
192 
193 /******************************************************/
203 
205 
213 extern const char* find_filename(const char* const filename_with_directory);
214 
216 
221 std::string::size_type find_pos_of_filename(const std::string& filename_with_directory);
222 
224 
228 std::string get_filename(const std::string& filename_with_directory);
229 
240 char* get_directory_name(char* directory_name, const char* const filename_with_directory);
241 
243 
250 std::string get_directory_name(const std::string& filename_with_directory);
251 
257 extern bool is_absolute_pathname(const std::string& filename_with_directory);
258 
264 extern bool is_absolute_pathname(const char* const filename_with_directory);
265 
277 extern char* prepend_directory_name(char* filename_with_directory, const char* const directory_name);
278 
280 
284 std::string::size_type find_pos_of_extension(const std::string& file_in_directory_name);
285 #if 0
286 // terribly dangerous for memory overrun.
287 
304 extern char *
305 add_extension(char * file_in_directory_name,
306  const char * const extension);
307 #endif
308 
310 std::string& add_extension(std::string& file_in_directory_name, const std::string& extension);
311 
312 #if 0
313 // disabled because possible memory overrun
314 
331 extern char *
332 replace_extension(char *file_in_directory_name,
333  const char * const extension);
334 #endif
335 
337 std::string& replace_extension(std::string& file_in_directory_name, const std::string& extension);
338 
340 
341 /**********************************************************************
342  C-string manipulation function
343 ***********************************************************************/
344 #ifndef _MSC_VER
345 
347 
349 inline char* strupr(char* const str);
350 #else
351 # define strupr _strupr
352 #endif
353 
354 END_NAMESPACE_STIR
355 
356 #include "stir/utilities.inl"
357 
358 #endif // __UTILITIES_H__
char * prepend_directory_name(char *filename_with_directory, const char *const directory_name)
Prepend directory_name to the filename, but only if !is_absolute_pathname(filename_with_directory) ...
Definition: utilities.cxx:285
streamsize find_remaining_size(istream &input)
Find number of remaining characters in the stream.
Definition: utilities.cxx:379
IFSTREAM & open_read_binary(IFSTREAM &s, const std::string &name)
opens a stream for reading binary data. Calls error() when it does not succeed.
Definition: utilities.inl:53
const char * find_filename(const char *const filename_with_directory)
return a pointer to the start of the filename (i.e. after directory specifications) ...
Definition: utilities.cxx:108
const int max_filename_length
some large value to say how long filenames can be in the (deprecated) function ask_filename_with_exte...
Definition: utilities.h:191
char * get_directory_name(char *directory_name, const char *const filename_with_directory)
Copies the directory part from &#39;filename_with_directory&#39; into &#39;directory_name&#39; and returns the &#39;direc...
Definition: utilities.cxx:167
inline implementations for utility.h
string & add_extension(string &file_in_directory_name, const string &extension)
Append extension if none present.
Definition: utilities.cxx:209
NUMBER ask_num(const std::string &prompt, NUMBER minimum_value, NUMBER maximum_value, NUMBER default_value)
A function to ask a number from the user.
Definition: utilities.inl:33
char * strupr(char *const str)
make C-string uppercase
Definition: utilities.inl:95
string::size_type find_pos_of_filename(const string &filename_with_directory)
return the position of the start of the filename (i.e. after directory specifications) ...
Definition: utilities.cxx:135
void close_file(FSTREAM &s)
closes a stream without error checking.
Definition: utilities.inl:88
string ask_string(const string &str, const string &default_value)
A function to ask a string from the user.
Definition: utilities.cxx:67
string & replace_extension(string &file_in_directory_name, const string &extension)
Replace extension (or append if none present)
Definition: utilities.cxx:236
bool ask(const string &str, bool default_value)
A function to ask a yes/no question from the user.
Definition: utilities.cxx:41
void * read_stream_in_memory(std::istream &input, std::streamsize &file_size)
reads data into memory, returning a pointer to the memory
string::size_type find_pos_of_extension(const string &file_in_directory_name)
find the position of the &#39;.&#39; of the extension
Definition: utilities.cxx:185
basic configuration include file
string get_filename(const string &filename_with_directory)
return a std::string containing only the filename (i.e. after directory specifications) ...
Definition: utilities.cxx:161
string ask_filename_with_extension(const string &prompt, const string &default_extension)
Asks for a filename (appending an extension if none is provided).
Definition: utilities.cxx:332
OFSTREAM & open_write_binary(OFSTREAM &s, const std::string &name)
opens a stream for writing binary data. Calls error() when it does not succeed.
Definition: utilities.inl:75
void ask_filename_and_open(FSTREAM &s, const std::string &prompt, const std::string &default_extension, std::ios::openmode mode)
as above, but with default abort_if_failed = true
Definition: utilities.h:110
bool is_absolute_pathname(const char *const filename_with_directory)
Checks if the filename points to an absolute location, or is a relative (e.g. to current directory) p...
Definition: utilities.cxx:247