#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "types.h"
#include "globals.h"
#include "errors.h"
#include "odin_util.h"
Go to the source code of this file.
Functions | |
char * | make_signal_name (char *signal_name, int bit) |
char * | make_full_ref_name (char *previous, char *module_name, char *module_instance_name, char *signal_name, int bit) |
char * | convert_long_to_bit_string (long long orig_long, int num_bits) |
long long | convert_dec_string_of_size_to_long (char *orig_string, int size) |
long long | convert_hex_string_of_size_to_long (char *orig_string, int size) |
long long | convert_oct_string_of_size_to_long (char *orig_string, int size) |
long long | convert_binary_string_of_size_to_long (char *orig_string, int size) |
long long int | my_power (long long int x, long long int y) |
char * | make_string_based_on_id (nnode_t *node) |
char * | make_simple_name (char *input, char *flatten_string, char flatten_char) |
void * | my_malloc_struct (int bytes_to_alloc) |
long long int | pow2 (int to_the_power) |
long long convert_binary_string_of_size_to_long | ( | char * | orig_string, | |
int | size | |||
) |
Definition at line 248 of file odin_util.c.
00249 { 00250 int i; 00251 long long return_value = 0; 00252 long long current_base_value = 1; 00253 char temp[2]; 00254 00255 if (strlen(orig_string) > 63) 00256 { 00257 /* greater than our bit capacity so not a constant */ 00258 return -1; 00259 } 00260 00261 for (i = strlen(orig_string)-1; i > -1; i--) 00262 { 00263 if ((tolower(orig_string[i]) == 'x') || (tolower(orig_string[i]) == 'z')) 00264 { 00265 /* this can't be converted to a decimal value */ 00266 return -1; 00267 } 00268 else if (isdigit(orig_string[i])) 00269 { 00270 sprintf(temp, "%c", orig_string[i]); 00271 oassert(atoi(temp) < 2); 00272 return_value += (long long)(atoi(temp) * current_base_value); 00273 } 00274 else 00275 { 00276 error_message(PARSE_ERROR, -1, -1, "This suspected binary number (%s) is not\n", orig_string); 00277 } 00278 current_base_value *= 2; 00279 } 00280 00281 return return_value; 00282 }
long long convert_dec_string_of_size_to_long | ( | char * | orig_string, | |
int | size | |||
) |
Definition at line 124 of file odin_util.c.
00125 { 00126 int i; 00127 long long return_value = 0; 00128 long long current_base_value = 1; 00129 char temp[2]; 00130 00131 if (strlen(orig_string) > 19) 00132 { 00133 /* greater than our bit capacity so not a constant 64 bits */ 00134 return -1; 00135 } 00136 00137 for (i = strlen(orig_string)-1; i > -1; i--) 00138 { 00139 if (isdigit(orig_string[i])) 00140 { 00141 sprintf(temp, "%c", orig_string[i]); 00142 return_value += (long long)(atoi(temp) * current_base_value); 00143 } 00144 else 00145 { 00146 error_message(PARSE_ERROR, -1, -1, "This suspected decimal number (%s) is not\n", orig_string); 00147 } 00148 current_base_value *= 10; 00149 } 00150 00151 return return_value; 00152 }
long long convert_hex_string_of_size_to_long | ( | char * | orig_string, | |
int | size | |||
) |
Definition at line 157 of file odin_util.c.
00158 { 00159 int i; 00160 long long return_value = 0; 00161 long long current_base_value = 1; 00162 char temp[2]; 00163 00164 if (strlen(orig_string) > 16) 00165 { 00166 /* greater than our bit capacity so not a constant */ 00167 return -1; 00168 } 00169 00170 for (i = strlen(orig_string)-1; i > -1; i--) 00171 { 00172 if (isdigit(orig_string[i])) 00173 { 00174 sprintf(temp, "%c", orig_string[i]); 00175 return_value += (long long)(atoi(temp) * current_base_value); 00176 } 00177 else if ((orig_string[i] == 'a') || (orig_string[i] == 'A')) 00178 { 00179 return_value += (long long)(10 * current_base_value); 00180 } 00181 else if ((orig_string[i] == 'b') || (orig_string[i] == 'B')) 00182 { 00183 return_value += (long long)(11 * current_base_value); 00184 } 00185 else if ((orig_string[i] == 'c') || (orig_string[i] == 'C')) 00186 { 00187 return_value += (long long)(11 * current_base_value); 00188 } 00189 else if ((orig_string[i] == 'd') || (orig_string[i] == 'D')) 00190 { 00191 return_value += (long long)(11 * current_base_value); 00192 } 00193 else if ((orig_string[i] == 'e') || (orig_string[i] == 'E')) 00194 { 00195 return_value += (long long)(11 * current_base_value); 00196 } 00197 else if ((orig_string[i] == 'f') || (orig_string[i] == 'F')) 00198 { 00199 return_value += (long long)(11 * current_base_value); 00200 } 00201 else 00202 { 00203 error_message(PARSE_ERROR, -1, -1, "This suspected hex number (%s) is not\n", orig_string); 00204 } 00205 current_base_value *= 16; 00206 } 00207 00208 return return_value; 00209 }
char* convert_long_to_bit_string | ( | long long | orig_long, | |
int | num_bits | |||
) |
Definition at line 98 of file odin_util.c.
00099 { 00100 int i; 00101 char *return_val = (char*)malloc(sizeof(char)*(num_bits+1)); 00102 int mask = 1; 00103 00104 for (i = num_bits-1; i >= 0; i--) 00105 { 00106 if((mask & orig_long) > 0) 00107 { 00108 return_val[i] = '1'; 00109 } 00110 else 00111 { 00112 return_val[i] = '0'; 00113 } 00114 mask = mask << 1; 00115 } 00116 return_val[num_bits] = '\0'; 00117 00118 return return_val; 00119 }
long long convert_oct_string_of_size_to_long | ( | char * | orig_string, | |
int | size | |||
) |
Definition at line 214 of file odin_util.c.
00215 { 00216 int i; 00217 long long return_value = 0; 00218 long long current_base_value = 1; 00219 char temp[2]; 00220 00221 if (strlen(orig_string) > 21) 00222 { 00223 /* greater than our bit capacity so not a constant */ 00224 return -1; 00225 } 00226 00227 for (i = strlen(orig_string)-1; i > -1; i--) 00228 { 00229 if (isdigit(orig_string[i])) 00230 { 00231 oassert(atoi(temp) < 8); 00232 sprintf(temp, "%c", orig_string[i]); 00233 return_value += (long long)(atoi(temp) * current_base_value); 00234 } 00235 else 00236 { 00237 error_message(PARSE_ERROR, -1, -1, "This suspected oct number (%s) is not\n", orig_string); 00238 } 00239 current_base_value *= 8; 00240 } 00241 00242 return return_value; 00243 }
char* make_full_ref_name | ( | char * | previous, | |
char * | module_name, | |||
char * | module_instance_name, | |||
char * | signal_name, | |||
int | bit | |||
) |
Definition at line 58 of file odin_util.c.
00059 { 00060 char *return_string; 00061 return_string = (char*)malloc(sizeof(char)*1); 00062 return_string[0] = '\0'; 00063 00064 if (previous != NULL) 00065 { 00066 return_string = (char*)realloc(return_string, sizeof(char)*(strlen(previous)+1+1)); 00067 sprintf(return_string, "%s", previous); 00068 } 00069 if (module_name != NULL) 00070 { 00071 return_string = (char*)realloc(return_string, sizeof(char)*(strlen(return_string)+1+strlen(module_name)+1+strlen(module_instance_name)+1)); 00072 sprintf(return_string, "%s.%s+%s", return_string, module_name, module_instance_name); 00073 } 00074 if ((signal_name != NULL) && ((previous != NULL) || ((module_name != NULL)))) 00075 { 00076 return_string = (char*)realloc(return_string, sizeof(char)*(strlen(return_string)+1+strlen(signal_name)+1)); 00077 strcat(return_string, "^"); 00078 strcat(return_string, signal_name); 00079 } 00080 else if (signal_name != NULL) 00081 { 00082 return_string = (char*)realloc(return_string, sizeof(char)*(strlen(return_string)+1+strlen(signal_name)+1)); 00083 sprintf(return_string, "%s", signal_name); 00084 } 00085 if (bit != -1) 00086 { 00087 oassert(signal_name != NULL); 00088 return_string = (char*)realloc(return_string, sizeof(char)*(strlen(return_string)+1+10+1)); 00089 sprintf(return_string, "%s~%d", return_string, bit); 00090 } 00091 return return_string; 00092 }
char* make_signal_name | ( | char * | signal_name, | |
int | bit | |||
) |
Definition at line 38 of file odin_util.c.
00039 { 00040 char *return_string; 00041 00042 oassert(signal_name != NULL); 00043 if (bit == -1) 00044 return strdup(signal_name); 00045 00046 return_string = strdup(signal_name); 00047 return_string = (char*)realloc(return_string, sizeof(char)*(strlen(return_string)+1+10+1)); 00048 sprintf(return_string, "%s-%d", return_string, bit); 00049 return return_string; 00050 }
char* make_simple_name | ( | char * | input, | |
char * | flatten_string, | |||
char | flatten_char | |||
) |
Definition at line 325 of file odin_util.c.
00326 { 00327 int i; 00328 int j; 00329 char *return_string = NULL; 00330 oassert(input != NULL); 00331 00332 return_string = (char*)malloc(sizeof(char)*(strlen(input)+1)); 00333 00334 for (i = 0; i < strlen(input); i++) 00335 { 00336 return_string[i] = input[i]; 00337 for (j = 0; j < strlen(flatten_string); j++) 00338 { 00339 if (input[i] == flatten_string[j]) 00340 { 00341 return_string[i] = flatten_char; 00342 break; 00343 } 00344 } 00345 } 00346 00347 return_string[strlen(input)] = '\0'; 00348 00349 return return_string; 00350 }
char* make_string_based_on_id | ( | nnode_t * | node | ) |
Definition at line 311 of file odin_util.c.
00312 { 00313 char *return_string; 00314 00315 return_string = (char*)malloc(sizeof(char)*(20+2)); // any unique id greater than 20 characters means trouble 00316 00317 sprintf(return_string, "n%ld", node->unique_id); 00318 00319 return return_string; 00320 }
void* my_malloc_struct | ( | int | bytes_to_alloc | ) |
Definition at line 355 of file odin_util.c.
00356 { 00357 void *allocated = NULL; 00358 static long int m_id = 0; 00359 00360 // ways to stop the execution at the point when a specific structure is built 00361 //oassert(m_id != 1777); 00362 00363 allocated = malloc(bytes_to_alloc); 00364 if(allocated == NULL) 00365 { 00366 fprintf(stderr,"MEMORY FAILURE\n"); 00367 oassert (0); 00368 } 00369 00370 /* mark the unique_id */ 00371 *((long int*)allocated) = m_id; 00372 00373 m_id++; 00374 00375 return(allocated); 00376 }
long long int my_power | ( | long long int | x, | |
long long int | y | |||
) |
Definition at line 288 of file odin_util.c.
00289 { 00290 int i; 00291 long long int value; 00292 00293 if (y == 0) 00294 { 00295 return 1; 00296 } 00297 00298 value = x; 00299 00300 for (i = 1; i < y; i++) 00301 { 00302 value *= x; 00303 } 00304 00305 return value; 00306 }
long long int pow2 | ( | int | to_the_power | ) |
Definition at line 380 of file odin_util.c.
00381 { 00382 int i; 00383 long long int return_val = 1; 00384 00385 for (i = 0; i < to_the_power; i++) 00386 { 00387 return_val = return_val << 1; 00388 } 00389 00390 return return_val; 00391 }