Changeset a048fa for molecuilder/src/helpers.cpp
- Timestamp:
- Jul 23, 2009, 2:21:57 PM (16 years ago)
- Children:
- c3a303
- Parents:
- c95b69
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
molecuilder/src/helpers.cpp
rc95b69 ra048fa 15 15 double ask_value(const char *text) 16 16 { 17 18 19 20 21 22 17 double test = 0.1439851348959832147598734598273456723948652983045928346598365; 18 do { 19 cout << Verbose(0) << text; 20 cin >> test; 21 } while (test == 0.1439851348959832147598734598273456723948652983045928346598365); 22 return test; 23 23 }; 24 24 … … 29 29 #ifdef HAVE_DEBUG 30 30 void debug_in(const char *output, const char *file, const int line) { 31 31 if (output) fprintf(stderr,"DEBUG: in %s at line %i: %s\n", file, line, output); 32 32 } 33 33 #else 34 void debug_in(const char *output, const char *file, const int line) {} 34 void debug_in(const char *output, const char *file, const int line) {} // print nothing 35 35 #endif 36 36 … … 42 42 void * Malloc(size_t size, const char* output) 43 43 { 44 45 46 47 48 44 void *buffer = NULL; 45 buffer = (void *)malloc(size); // alloc 46 if (buffer == NULL) 47 cout << Verbose(0) << "Malloc failed - pointer is NULL: " << output << endl; 48 return(buffer); 49 49 }; 50 50 … … 56 56 void * Calloc(size_t size, const char* output) 57 57 { 58 59 60 61 62 58 void *buffer = NULL; 59 buffer = (void *)calloc(size, (size_t)0); // alloc 60 if (buffer == NULL) 61 cout << Verbose(0) << "Calloc failed - pointer is NULL: " << output << endl; 62 return(buffer); 63 63 }; 64 64 … … 71 71 void * ReAlloc(void * OldPointer, size_t size, const char* output) 72 72 { 73 74 75 76 77 78 79 80 81 73 void *buffer = NULL; 74 if (OldPointer == NULL) 75 //cout << Verbose(0) << "ReAlloc impossible - old is NULL: " << output << endl; 76 buffer = (void *)malloc(size); // malloc 77 else 78 buffer = (void *)realloc(OldPointer, size); // realloc 79 if (buffer == NULL) 80 cout << Verbose(0) << "ReAlloc failed - new is NULL: " << output << endl; 81 return(buffer); 82 82 }; 83 83 … … 88 88 void Free(void ** buffer, const char* output) 89 89 { 90 91 92 93 94 95 90 if (*buffer == NULL) { 91 //cout << Verbose(5) << "Free not necesary: " << output << endl; 92 } else { 93 free(*buffer); 94 *buffer = NULL; 95 } 96 96 }; 97 97 … … 103 103 char* MallocString(size_t size, const char* output) 104 104 { 105 106 107 108 109 110 for (i=size;i--;)// reset111 112 113 105 size_t i; 106 char *buffer; 107 buffer = (char *)malloc(sizeof(char) * (size+1)); // alloc 108 if (buffer == NULL) 109 cout << Verbose(0) << output << endl; 110 for (i=size;i--;) // reset 111 buffer[i] = i % 2 == 0 ? 'p': 'c'; 112 buffer[size] = '\0'; // and set length marker on its end 113 return(buffer); 114 114 } 115 115 … … 121 121 void bound(double *b, double lower_bound, double upper_bound) 122 122 { 123 124 125 126 127 123 double step = (upper_bound - lower_bound); 124 while (*b >= upper_bound) 125 *b -= step; 126 while (*b < lower_bound) 127 *b += step; 128 128 }; 129 129 … … 134 134 void flip(double *x, double *y) 135 135 { 136 137 138 139 136 double tmp; 137 tmp = *x; 138 *x = *y; 139 *y = tmp; 140 140 }; 141 141 … … 147 147 int pot(int base, int n) 148 148 { 149 150 151 152 153 149 int res = 1; 150 int j; 151 for (j=n;j--;) 152 res *= base; 153 return res; 154 154 }; 155 155 … … 161 161 char *FixedDigitNumber(const int FragmentNumber, const int digits) 162 162 { 163 164 165 166 167 168 169 170 171 172 173 // terminateand fill string array from end backward174 175 176 177 178 179 180 181 163 char *returnstring; 164 int number = FragmentNumber; 165 int order = 0; 166 while (number != 0) { // determine number of digits needed 167 number = (int)floor(((double)number / 10.)); 168 order++; 169 //cout << "Number is " << number << ", order is " << order << "." << endl; 170 } 171 // allocate string 172 returnstring = (char *) Malloc(sizeof(char)*(order+2), "FixedDigitNumber: *returnstring"); 173 // terminate and fill string array from end backward 174 returnstring[order] = '\0'; 175 number = digits; 176 for (int i=order;i--;) { 177 returnstring[i] = '0' + (char)(number % 10); 178 number = (int)floor(((double)number / 10.)); 179 } 180 //cout << returnstring << endl; 181 return returnstring; 182 182 }; 183 183 … … 188 188 bool IsValidNumber( const char *string) 189 189 { 190 191 192 193 194 195 190 int ptr = 0; 191 if ((string[ptr] == '.') || (string[ptr] == '-')) // number may be negative or start with dot 192 ptr++; 193 if ((string[ptr] >= '0') && (string[ptr] <= '9')) 194 return true; 195 return false; 196 196 }; 197 197
Note:
See TracChangeset
for help on using the changeset viewer.