d852cf50368a5da78b49ed539f8e6b9cf0103cd9
[jump.git] / dol / src / dol / visitor / systemC / lib / dol.c
1 #include "dol.h"\r
2 \r
3 /**\r
4  * Create the port name of an iterated port based on its basename and the\r
5  * given indices.\r
6  * Example: createPort(xxx, "port", 2, 1, N1, 3, N2) will result in xxx\r
7  * having the value "port_1_3". The range definitions N1, N2 will be ignored\r
8  * in this implementation\r
9  *\r
10  * @param port buffer where the result is stored\r
11  * @param base basename of the port\r
12  * @param number_of_indices number of dimensions of the port\r
13  * @param index_range_pairs index and range values for each dimension\r
14  */\r
15 char* createPort(char* port, char* base,\r
16         int number_of_indices, int indices, ...) {\r
17     char next_index_string[10];\r
18     strcpy(port, base);\r
19 \r
20     if (number_of_indices > 4) {\r
21         printf("Error: iterated ports must not have more than ");\r
22         printf("4 dimensions.\n");\r
23         exit(-1);\r
24     }\r
25 \r
26     va_list argumentlist;\r
27     va_start(argumentlist, indices);\r
28 \r
29     if (number_of_indices > 0) {\r
30       sprintf(next_index_string, "_%d", indices);\r
31       strcat(port, next_index_string);\r
32 \r
33       for (int k = 1; k < number_of_indices; k++) {\r
34           va_arg(argumentlist, int); //skip the range value\r
35           sprintf(next_index_string, "_%d", va_arg(argumentlist, int));\r
36           strcat(port, next_index_string);\r
37       }\r
38     }\r
39     return port;\r
40 }\r
41 \r
42 /**\r
43  * Gets an index of a string, where the index must be separated by\r
44  * a character specified in tokens.\r
45  * Returns -1, when an error occurs.\r
46  *\r
47  * Example:\r
48  * getIndex("name_1_2", "_", 0) will return 1.\r
49  * getIndex("name_1_2", "_", 1) will return 2.\r
50  *\r
51  * @param string string to parse\r
52  * @param tokens delimiter of indices\r
53  * @param indexNumber position of index (starting at 0)\r
54  */\r
55 int getIndex(const char* string, char* tokens, int indexNumber) {\r
56   char* string_copy;\r
57   char* token_pointer;\r
58   int index = 0;\r
59 \r
60   string_copy = (char*) malloc(sizeof(char) * (strlen(string) + 1));\r
61   if (!string_copy) {\r
62     fprintf(stderr, "getIndex(): could not allocate memory.\n");\r
63     return -1;\r
64   }\r
65 \r
66   strcpy(string_copy, string);\r
67 \r
68   token_pointer = strtok(string_copy, tokens);\r
69   do {\r
70     token_pointer = strtok(NULL, tokens);\r
71     index++;\r
72   } while (index <= indexNumber && token_pointer != 0);\r
73 \r
74   if (token_pointer) {\r
75     index = atoi(token_pointer);\r
76     free(string_copy);\r
77     return index;\r
78   }\r
79 \r
80   return -1;\r
81 }\r