dol: initial dol commit
[jump.git] / dol / src / dol / visitor / yapi / lib / ProcessWrapper.cpp
1 #include "ProcessWrapper.h"\r
2 #include "dolSupport.h"\r
3 \r
4 /**\r
5  *\r
6  */\r
7 ProcessWrapper::ProcessWrapper(const char* name, const Id& n) : Process(n) {\r
8     _name = new char[strlen(name) + 1];\r
9     strcpy(_name, name);\r
10 \r
11     _isDetached = false;\r
12     for (int i = 0; i < 4; i++) {\r
13         _iteratorIndex[i] = getIndex(_name, "_", i);\r
14     }\r
15 }\r
16 \r
17 /**\r
18  *\r
19  */\r
20 ProcessWrapper::~ProcessWrapper() {\r
21 }\r
22 \r
23 /**\r
24  *\r
25  */\r
26 void ProcessWrapper::initialize() {\r
27     _process.init(&_process);\r
28 }\r
29 \r
30 /**\r
31  *\r
32  */\r
33 int ProcessWrapper::fire()\r
34 {\r
35     return _process.fire(&_process);\r
36 }\r
37 \r
38 \r
39 /**\r
40  *\r
41  */\r
42 void ProcessWrapper::detach() {\r
43     _isDetached = true;\r
44 }\r
45 \r
46 \r
47 /**\r
48  * Gets an index of a string, where the index must be separated by\r
49  * a character specified in tokens.\r
50  * Returns -1, when an error occurs.\r
51  *\r
52  * Example:\r
53  * getIndex("name_1_2", "_", 0) will return 1.\r
54  * getIndex("name_1_2", "_", 1) will return 2.\r
55  *\r
56  * @param string string to parse\r
57  * @param tokens delimiter of indices\r
58  * @param indexNumber position of index (starting at 0)\r
59  */\r
60 int ProcessWrapper::getIndex(const char* string, char* tokens,\r
61         int indexNumber) const {\r
62     char* string_copy;\r
63     char* token_pointer;\r
64     int index = 0;\r
65 \r
66     string_copy = (char*) malloc(sizeof(char) * (strlen(string) + 1));\r
67     if (!string_copy) {\r
68         fprintf(stderr, "getIndex(): could not allocate memory.\n");\r
69         return -1;\r
70     }\r
71 \r
72     strcpy(string_copy, string);\r
73 \r
74     token_pointer = strtok(string_copy, tokens);\r
75     do {\r
76         token_pointer = strtok(NULL, tokens);\r
77         index++;\r
78     } while (index <= indexNumber && token_pointer != 0);\r
79 \r
80     if (token_pointer) {\r
81         index = atoi(token_pointer);\r
82         free(string_copy);\r
83         return index;\r
84     }\r
85 \r
86     return -1;\r
87 }\r
88 \r
89 \r
90 /**\r
91  * Get the name of this process.\r
92  */\r
93 char* ProcessWrapper::getName() const {\r
94     return _name;\r
95 }\r
96 \r
97 \r
98 /**\r
99  * Get the index of this process.\r
100  * @param indexNumber position of index (starting at 0)\r
101  */\r
102 int ProcessWrapper::getIndex(unsigned indexNumber) const {\r
103     if (indexNumber < 4) {\r
104         return _iteratorIndex[indexNumber];\r
105     }\r
106     return -1;\r
107 }\r