1 | /** \file helpers.cpp
|
---|
2 | *
|
---|
3 | * Implementation of some auxiliary functions for memory dis-/allocation and so on
|
---|
4 | */
|
---|
5 |
|
---|
6 |
|
---|
7 | #include "helpers.hpp"
|
---|
8 |
|
---|
9 | /********************************************** helpful functions *********************************/
|
---|
10 |
|
---|
11 |
|
---|
12 | /** Asks for a double value and checks input
|
---|
13 | * \param *text question
|
---|
14 | */
|
---|
15 | double ask_value(const char *text)
|
---|
16 | {
|
---|
17 | double test = 0.1439851348959832147598734598273456723948652983045928346598365;
|
---|
18 | do {
|
---|
19 | cout << Verbose(0) << text;
|
---|
20 | cin >> test;
|
---|
21 | } while (test == 0.1439851348959832147598734598273456723948652983045928346598365);
|
---|
22 | return test;
|
---|
23 | };
|
---|
24 |
|
---|
25 |
|
---|
26 | /** Wrapper for allocating'ing a memory range with *output if it fails.
|
---|
27 | * \param size number of chars to alloc for \a *buffer
|
---|
28 | * \param *output message if malloc fails
|
---|
29 | * \return pointer to memory range
|
---|
30 | */
|
---|
31 | void * Malloc(size_t size, const char* output)
|
---|
32 | {
|
---|
33 | void *buffer = NULL;
|
---|
34 | buffer = (void *)malloc(size); // alloc
|
---|
35 | if (buffer == NULL)
|
---|
36 | cout << Verbose(0) << "Malloc failed - pointer is NULL: " << output << endl;
|
---|
37 | return(buffer);
|
---|
38 | };
|
---|
39 |
|
---|
40 | /** Wrapper for allocating'ing a memory range with *output if it fails.
|
---|
41 | * \param size number of chars to alloc for \a *buffer
|
---|
42 | * \param *output message if malloc fails
|
---|
43 | * \return pointer to memory range
|
---|
44 | */
|
---|
45 | void * Calloc(size_t size, const char* output)
|
---|
46 | {
|
---|
47 | void *buffer = NULL;
|
---|
48 | buffer = (void *)calloc(size, (size_t)0); // alloc
|
---|
49 | if (buffer == NULL)
|
---|
50 | cout << Verbose(0) << "Calloc failed - pointer is NULL: " << output << endl;
|
---|
51 | return(buffer);
|
---|
52 | };
|
---|
53 |
|
---|
54 | /** Wrapper for reallocating'ing a memory range with *output if it fails.
|
---|
55 | * \param *OldPointer pointer to old memory range
|
---|
56 | * \param size number of chars to alloc for \a *buffer
|
---|
57 | * \param *output message if malloc fails
|
---|
58 | * \return pointer to memory range
|
---|
59 | */
|
---|
60 | void * ReAlloc(void * OldPointer, size_t size, const char* output)
|
---|
61 | {
|
---|
62 | void *buffer = NULL;
|
---|
63 | if (OldPointer == NULL)
|
---|
64 | cout << Verbose(0) << "ReAlloc impossible - old is NULL: " << output << endl;
|
---|
65 | buffer = (void *)realloc(OldPointer, size); // alloc
|
---|
66 | if (buffer == NULL)
|
---|
67 | cout << Verbose(0) << "ReAlloc failed - new is NULL: " << output << endl;
|
---|
68 | return(buffer);
|
---|
69 | };
|
---|
70 |
|
---|
71 | /** Wrapper for free'ing an allocated memory range with *output if it fails.
|
---|
72 | * \param *buffer pointer to buffer to be free'd
|
---|
73 | * \param *output message if free fails
|
---|
74 | */
|
---|
75 | void Free(void ** buffer, const char* output)
|
---|
76 | {
|
---|
77 | if (*buffer == NULL) {
|
---|
78 | //cout << Verbose(5) << "Free not necesary: " << output << endl;
|
---|
79 | } else {
|
---|
80 | free(*buffer);
|
---|
81 | }
|
---|
82 | *buffer = NULL;
|
---|
83 | };
|
---|
84 |
|
---|
85 | /** Malloc string array and set its length to the allocated length.
|
---|
86 | * \param *output message if malloc fails
|
---|
87 | * \param size number of chars to alloc for \a *buffer
|
---|
88 | * \return pointer to string array
|
---|
89 | */
|
---|
90 | char* MallocString(size_t size, const char* output)
|
---|
91 | {
|
---|
92 | size_t i;
|
---|
93 | char *buffer;
|
---|
94 | buffer = (char *)malloc(sizeof(char) * (size+1)); // alloc
|
---|
95 | if (buffer == NULL)
|
---|
96 | cout << Verbose(0) << output << endl;
|
---|
97 | for (i=0;i<size;i++) // reset
|
---|
98 | buffer[i] = i % 2 == 0 ? 'p': 'c';
|
---|
99 | buffer[size] = '\0'; // and set length marker on its end
|
---|
100 | return(buffer);
|
---|
101 | }
|
---|
102 |
|
---|
103 | /** modulo operator for doubles.
|
---|
104 | * \param *b pointer to double
|
---|
105 | * \param lower_bound lower bound
|
---|
106 | * \param upper_bound upper bound
|
---|
107 | */
|
---|
108 | void bound(double *b, double lower_bound, double upper_bound)
|
---|
109 | {
|
---|
110 | double step = (upper_bound - lower_bound);
|
---|
111 | while (*b >= upper_bound)
|
---|
112 | *b -= step;
|
---|
113 | while (*b < lower_bound)
|
---|
114 | *b += step;
|
---|
115 | };
|
---|
116 |
|
---|
117 | /** Flips two doubles.
|
---|
118 | * \param *x pointer to first double
|
---|
119 | * \param *y pointer to second double
|
---|
120 | */
|
---|
121 | void flip(double *x, double *y)
|
---|
122 | {
|
---|
123 | double tmp;
|
---|
124 | tmp = *x;
|
---|
125 | *x = *y;
|
---|
126 | *y = tmp;
|
---|
127 | };
|
---|
128 |
|
---|
129 | /** Returns the power of \a n with respect to \a base.
|
---|
130 | * \param base basis
|
---|
131 | * \param n power
|
---|
132 | * \return \f$base^n\f$
|
---|
133 | */
|
---|
134 | int pot(int base, int n)
|
---|
135 | {
|
---|
136 | int res = 1;
|
---|
137 | int j;
|
---|
138 | for (j=0;j<n;j++)
|
---|
139 | res *= base;
|
---|
140 | return res;
|
---|
141 | };
|
---|
142 |
|
---|
143 | /** Returns a string with \a i prefixed with 0s to match order of total number of molecules in digits.
|
---|
144 | * \param FragmentNumber total number of fragments to determine necessary number of digits
|
---|
145 | * \param digits number to create with 0 prefixed
|
---|
146 | * \return allocated(!) char array with number in digits, ten base.
|
---|
147 | */
|
---|
148 | char *FixedDigitNumber(const int FragmentNumber, const int digits)
|
---|
149 | {
|
---|
150 | char *returnstring;
|
---|
151 | int number = FragmentNumber;
|
---|
152 | int order = 0;
|
---|
153 | while (number != 0) { // determine number of digits needed
|
---|
154 | number = (int)floor(((double)number / 10.));
|
---|
155 | order++;
|
---|
156 | //cout << "Number is " << number << ", order is " << order << "." << endl;
|
---|
157 | }
|
---|
158 | // allocate string
|
---|
159 | returnstring = (char *) Malloc(sizeof(char)*(order+2), "MoleculeListClass::CreateFragmentNumberForOutput: *returnstring");
|
---|
160 | // terminate and fill string array from end backward
|
---|
161 | returnstring[order] = '\0';
|
---|
162 | number = digits;
|
---|
163 | for (int i=order-1;i>=0;i--){
|
---|
164 | returnstring[i] = '0' + (char)(number % 10);
|
---|
165 | number = (int)floor(((double)number / 10.));
|
---|
166 | }
|
---|
167 | return returnstring;
|
---|
168 | };
|
---|
169 |
|
---|