STIR 6.4.0
utilities.inl
Go to the documentation of this file.
1
9/*
10 Copyright (C) 2000 PARAPET partners
11 Copyright (C) 2000-2009, Hammersmith Imanet Ltd
12 This file is part of STIR.
13
14 SPDX-License-Identifier: Apache-2.0 AND License-ref-PARAPET-license
15
16 See STIR/LICENSE.txt for details
17*/
18#include <iostream>
19#include "stir/error.h"
20#include <sstream>
21
22START_NAMESPACE_STIR
31template <class NUMBER>
32inline NUMBER
33ask_num(const std::string& str, NUMBER minimum_value, NUMBER maximum_value, NUMBER default_value)
34{
35
36 while (1)
37 {
38 std::string input;
39 std::cerr << "\n" << str << "[" << minimum_value << "," << maximum_value << " D:" << default_value << "]: ";
40 std::getline(std::cin, input);
41 std::istringstream ss(input.c_str());
42
43 NUMBER value = default_value;
44 ss >> value;
45 if ((value >= minimum_value) && (maximum_value >= value))
46 return value;
47 std::cerr << "\nOut of bounds. Try again.";
48 }
49}
50
51template <class IFSTREAM>
52inline IFSTREAM&
53open_read_binary(IFSTREAM& s, const std::string& name)
54{
55#if 0
56 //KT 30/07/98 The next lines are only necessary (in VC 5.0) when importing
57 // <fstream.h>. We use <fstream> now, so they are disabled.
58
59 // Visual C++ does not complain when opening a nonexisting file for reading,
60 // unless using std::ios::nocreate
61 s.open(name.c_str(), std::ios::in | std::ios::binary | std::ios::nocreate);
62#else
63 s.open(name.c_str(), std::ios::in | std::ios::binary);
64#endif
65 // KT 14/01/2000 added name of file in error message
66 if (s.fail() || s.bad())
67 {
68 error("Error opening file %s\n", name.c_str());
69 }
70 return s;
71}
72
73template <class OFSTREAM>
74inline OFSTREAM&
75open_write_binary(OFSTREAM& s, const std::string& name)
76{
77 s.open(name.c_str(), std::ios::out | std::ios::binary);
78 // KT 14/01/2000 added name of file in error message
79 if (s.fail() || s.bad())
80 {
81 error("Error opening file %s\n", name.c_str());
82 }
83 return s;
84}
85
86template <class FSTREAM>
87inline void
88close_file(FSTREAM& s)
89{
90 s.close();
91}
92
93#ifndef _MSC_VER
94char*
95strupr(char* const str)
96{
97 for (char* a = str; *a; a++)
98 {
99 if ((*a >= 'a') && (*a <= 'z'))
100 *a += 'A' - 'a';
101 };
102 return str;
103}
104#endif
105
106END_NAMESPACE_STIR
Declaration of stir::error()
void close_file(FILE *&fptr)
closes a FILE without error checking.
Definition utilities.cxx:101
void error(const char *const s,...)
Print error with format string a la printf and throw exception.
Definition error.cxx:42
FILE *& open_write_binary(FILE *&fptr, const string &name)
opens a FILE for writing binary data. Calls error() when it does not succeed.
Definition utilities.cxx:90
FILE *& open_read_binary(FILE *&fptr, const string &name)
opens a FILE for reading binary data. Calls error() when it does not succeed.
Definition utilities.cxx:79
char * strupr(char *const str)
make C-string uppercase
Definition utilities.inl:95
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