7d2a117de98a6109dcb8b65798a8e3be1dac026e
[jump.git] / dol / src / dol / visitor / cbe / lib / ProcessWrapperHelp.h
1 /**************************************************************** \r
2  *      Process Wrapper Help functions\r
3  *      Creator: lschor, 2008-11-21\r
4  *      Description: General process wrapper fucntions\r
5  *      \r
6  *      Revision: \r
7  *      - 2008-11-21: Created\r
8  */\r
9 \r
10 #ifndef __PROCESS_WRAPPER_HELP_H__\r
11 #define __PROCESS_WRAPPER_HELP_H__\r
12 \r
13 #include <stdarg.h>     // Used for va_list\r
14 \r
15 /**\r
16  * Gets an index of a string, where the index must be separated by\r
17  * a character specified in tokens.\r
18  * Returns -1, when an error occurs.\r
19  *\r
20  * Example:\r
21  * getIndex("name_1_2", "_", 0) will return 1.\r
22  * getIndex("name_1_2", "_", 1) will return 2.\r
23  *\r
24  * @param string string to parse\r
25  * @param tokens delimiter of indices\r
26  * @param indexNumber position of index (starting at 0)\r
27  */\r
28 int getIndex(const char* string, char* tokens, int indexNumber) {\r
29     char* string_copy;\r
30     char* token_pointer;\r
31     int index = 0;\r
32 \r
33     string_copy = (char*) malloc(sizeof(char) * (strlen(string) + 1));\r
34     if (!string_copy) {\r
35         fprintf(stderr, "getIndex(): could not allocate memory.\n");\r
36         return -1;\r
37     }\r
38 \r
39     strcpy(string_copy, string);\r
40 \r
41     token_pointer = strtok(string_copy, tokens);\r
42     do {\r
43         token_pointer = strtok(NULL, tokens);\r
44         index++;\r
45     } while (index <= indexNumber && token_pointer != 0);\r
46 \r
47     if (token_pointer) {\r
48         index = atoi(token_pointer);\r
49         free(string_copy);\r
50         return index;\r
51     }\r
52 \r
53     free(string_copy);\r
54     return -1;\r
55 }\r
56 \r
57 /**\r
58  * Create the port name of an iterated port based on its basename and the\r
59  * given indices.\r
60  *\r
61  * @param port buffer where the result is stored (created using\r
62  *             CREATEPORTVAR)\r
63  * @param base basename of the port\r
64  * @param number_of_indices number of dimensions of the port\r
65  * @param index_range_pairs index and range values for each dimension\r
66  */\r
67 int *createPort(int *port, int base, int number_of_indices,\r
68                 int index_range_pairs, ...) {\r
69     int index[4];\r
70     int range[4];\r
71     int i;\r
72     int value;\r
73 \r
74     va_list marker;\r
75     va_start(marker, index_range_pairs);\r
76 \r
77     value = index_range_pairs;\r
78     for (i = 0; i < number_of_indices; i++) {\r
79         index[i] = value;\r
80         value = va_arg(marker, int);\r
81         range[i] = value;\r
82         if (i < number_of_indices - 1) {\r
83             value = va_arg(marker, int);\r
84         }\r
85     }\r
86 \r
87     *port = base;\r
88     for (i = 0; i < number_of_indices; i++) {\r
89         int j;\r
90         int weight = 1;\r
91         for (j = 1; j < number_of_indices - i; j ++) {\r
92             weight *= range[j];\r
93         }\r
94         *port += index[i] * weight;\r
95     }\r
96 \r
97     return port;\r
98 }\r
99 \r
100 #endif\r