| 1 | /** \file helpers.cpp
 | 
|---|
| 2 |  *
 | 
|---|
| 3 |  * Implementation of some auxiliary functions for memory dis-/allocation and so on
 | 
|---|
| 4 |  */
 | 
|---|
| 5 | 
 | 
|---|
| 6 | // include config.h
 | 
|---|
| 7 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 8 | #include <config.h>
 | 
|---|
| 9 | #endif
 | 
|---|
| 10 | 
 | 
|---|
| 11 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 12 | 
 | 
|---|
| 13 | #include "Helpers/helpers.hpp"
 | 
|---|
| 14 | #include "Helpers/fast_functions.hpp"
 | 
|---|
| 15 | #include "Helpers/Verbose.hpp"
 | 
|---|
| 16 | #include "Helpers/Log.hpp"
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include <iostream>
 | 
|---|
| 19 | 
 | 
|---|
| 20 | /********************************************** helpful functions *********************************/
 | 
|---|
| 21 | 
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /** Asks for a double value and checks input
 | 
|---|
| 24 |  * \param *text question
 | 
|---|
| 25 |  */
 | 
|---|
| 26 | double ask_value(const char *text)
 | 
|---|
| 27 | {
 | 
|---|
| 28 |   double test = 0.1439851348959832147598734598273456723948652983045928346598365;
 | 
|---|
| 29 |   do {
 | 
|---|
| 30 |     DoLog(0) && (Log() << Verbose(0) << text);
 | 
|---|
| 31 |     cin >> test;
 | 
|---|
| 32 |   } while (test == 0.1439851348959832147598734598273456723948652983045928346598365);
 | 
|---|
| 33 |   return test;
 | 
|---|
| 34 | };
 | 
|---|
| 35 | 
 | 
|---|
| 36 | /** Output of a debug message to stderr.
 | 
|---|
| 37 |  * \param *P Problem at hand, points to ParallelSimulationData#me
 | 
|---|
| 38 |  * \param output output string
 | 
|---|
| 39 |  */
 | 
|---|
| 40 | #ifdef HAVE_DEBUG
 | 
|---|
| 41 | void debug_in(const char *output, const char *file, const int line) {
 | 
|---|
| 42 |   if (output) fprintf(stderr,"DEBUG: in %s at line %i: %s\n", file, line, output);
 | 
|---|
| 43 | }
 | 
|---|
| 44 | #else
 | 
|---|
| 45 | void debug_in(const char *output, const char *file, const int line) {}  // print nothing
 | 
|---|
| 46 | #endif
 | 
|---|
| 47 | 
 | 
|---|
| 48 | /** modulo operator for doubles.
 | 
|---|
| 49 |  * \param *b pointer to double
 | 
|---|
| 50 |  * \param lower_bound lower bound
 | 
|---|
| 51 |  * \param upper_bound upper bound
 | 
|---|
| 52 |  */
 | 
|---|
| 53 | void bound(double *b, double lower_bound, double upper_bound)
 | 
|---|
| 54 | {
 | 
|---|
| 55 |   double step = (upper_bound - lower_bound);
 | 
|---|
| 56 |   while (*b >= upper_bound)
 | 
|---|
| 57 |     *b -= step;
 | 
|---|
| 58 |   while (*b < lower_bound)
 | 
|---|
| 59 |     *b += step;
 | 
|---|
| 60 | };
 | 
|---|
| 61 | 
 | 
|---|
| 62 | /** Counts lines in file.
 | 
|---|
| 63 |  * Note we are scanning lines from current position, not from beginning.
 | 
|---|
| 64 |  * \param InputFile file to be scanned.
 | 
|---|
| 65 |  */
 | 
|---|
| 66 | int CountLinesinFile(ifstream &InputFile)
 | 
|---|
| 67 | {
 | 
|---|
| 68 |   char *buffer = new char[MAXSTRINGSIZE];
 | 
|---|
| 69 |   int lines=0;
 | 
|---|
| 70 | 
 | 
|---|
| 71 |   int PositionMarker = InputFile.tellg();  // not needed as Inputfile is copied, given by value, not by ref
 | 
|---|
| 72 |   // count the number of lines, i.e. the number of fragments
 | 
|---|
| 73 |   InputFile.getline(buffer, MAXSTRINGSIZE); // skip comment lines
 | 
|---|
| 74 |   InputFile.getline(buffer, MAXSTRINGSIZE);
 | 
|---|
| 75 |   while(!InputFile.eof()) {
 | 
|---|
| 76 |     InputFile.getline(buffer, MAXSTRINGSIZE);
 | 
|---|
| 77 |     lines++;
 | 
|---|
| 78 |   }
 | 
|---|
| 79 |   InputFile.seekg(PositionMarker, ios::beg);
 | 
|---|
| 80 |   delete[](buffer);
 | 
|---|
| 81 |   return lines;
 | 
|---|
| 82 | };
 | 
|---|
| 83 | 
 | 
|---|
| 84 | /** Returns a string with \a i prefixed with 0s to match order of total number of molecules in digits.
 | 
|---|
| 85 |  * \param FragmentNumber total number of fragments to determine necessary number of digits
 | 
|---|
| 86 |  * \param digits number to create with 0 prefixed
 | 
|---|
| 87 |  * \return allocated(!) char array with number in digits, ten base.
 | 
|---|
| 88 |  */
 | 
|---|
| 89 | char *FixedDigitNumber(const int FragmentNumber, const int digits)
 | 
|---|
| 90 | {
 | 
|---|
| 91 |   char *returnstring;
 | 
|---|
| 92 |   int number = FragmentNumber;
 | 
|---|
| 93 |   int order = 0;
 | 
|---|
| 94 |   while (number != 0) { // determine number of digits needed
 | 
|---|
| 95 |     number = (int)floor(((double)number / 10.));
 | 
|---|
| 96 |     order++;
 | 
|---|
| 97 |     //Log() << Verbose(0) << "Number is " << number << ", order is " << order << "." << endl;
 | 
|---|
| 98 |   }
 | 
|---|
| 99 |   // allocate string
 | 
|---|
| 100 |   returnstring = new char[order + 2];
 | 
|---|
| 101 |   // terminate  and fill string array from end backward
 | 
|---|
| 102 |   returnstring[order] = '\0';
 | 
|---|
| 103 |   number = digits;
 | 
|---|
| 104 |   for (int i=order;i--;) {
 | 
|---|
| 105 |     returnstring[i] = '0' + (char)(number % 10);
 | 
|---|
| 106 |     number = (int)floor(((double)number / 10.));
 | 
|---|
| 107 |   }
 | 
|---|
| 108 |   //Log() << Verbose(0) << returnstring << endl;
 | 
|---|
| 109 |   return returnstring;
 | 
|---|
| 110 | };
 | 
|---|
| 111 | 
 | 
|---|
| 112 | /** Tests whether a given string contains a valid number or not.
 | 
|---|
| 113 |  * \param *string
 | 
|---|
| 114 |  * \return true - is a number, false - is not a valid number
 | 
|---|
| 115 |  */
 | 
|---|
| 116 | bool IsValidNumber( const char *string)
 | 
|---|
| 117 | {
 | 
|---|
| 118 |   int ptr = 0;
 | 
|---|
| 119 |   if ((string[ptr] == '.') || (string[ptr] == '-')) // number may be negative or start with dot
 | 
|---|
| 120 |     ptr++;
 | 
|---|
| 121 |   if ((string[ptr] >= '0') && (string[ptr] <= '9'))
 | 
|---|
| 122 |     return true;
 | 
|---|
| 123 |   return false;
 | 
|---|
| 124 | };
 | 
|---|
| 125 | 
 | 
|---|
| 126 | /** Comparison function for GSL heapsort on distances in two molecules.
 | 
|---|
| 127 |  * \param *a
 | 
|---|
| 128 |  * \param *b
 | 
|---|
| 129 |  * \return <0, \a *a less than \a *b, ==0 if equal, >0 \a *a greater than \a *b
 | 
|---|
| 130 |  */
 | 
|---|
| 131 | int CompareDoubles (const void * a, const void * b)
 | 
|---|
| 132 | {
 | 
|---|
| 133 |   if (*(double *)a > *(double *)b)
 | 
|---|
| 134 |     return -1;
 | 
|---|
| 135 |   else if (*(double *)a < *(double *)b)
 | 
|---|
| 136 |     return 1;
 | 
|---|
| 137 |   else
 | 
|---|
| 138 |     return 0;
 | 
|---|
| 139 | };
 | 
|---|
| 140 | 
 | 
|---|
| 141 | 
 | 
|---|
| 142 | /**
 | 
|---|
| 143 |  * Calls exit(255).
 | 
|---|
| 144 |  */
 | 
|---|
| 145 | void performCriticalExit() {
 | 
|---|
| 146 |   exit(255);
 | 
|---|
| 147 | }
 | 
|---|