fbba359425ae664e699a5602d1a40e872370c86e
[jump.git] / dol / src / dol / visitor / PipeAndFilter / lib / ProcessWrapper.cpp
1 #include "ProcessWrapper.h"\r
2 #include "dolSupport.h"\r
3 \r
4 /**\r
5  *\r
6  */\r
7 ProcessWrapper::ProcessWrapper(char* name) {\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     if (_name) {\r
22         delete _name;\r
23     }\r
24 }\r
25 \r
26 /**\r
27  *\r
28  */\r
29 void ProcessWrapper::initialize() {\r
30     _process.init(&_process);\r
31 }\r
32 \r
33 /**\r
34  *\r
35  */\r
36 int ProcessWrapper::fire()\r
37 {\r
38     return _process.fire(&_process);\r
39 }\r
40 \r
41 \r
42 /**\r
43  *\r
44  */\r
45 void ProcessWrapper::detach() {\r
46     _isDetached = true;\r
47 }\r
48 \r
49 \r
50 /**\r
51  * Gets an index of a string, where the index must be separated by\r
52  * a character specified in tokens.\r
53  * Returns -1, when an error occurs.\r
54  *\r
55  * Example:\r
56  * getIndex("name_1_2", "_", 0) will return 1.\r
57  * getIndex("name_1_2", "_", 1) will return 2.\r
58  *\r
59  * @param string string to parse\r
60  * @param tokens delimiter of indices\r
61  * @param indexNumber position of index (starting at 0)\r
62  */\r
63 int ProcessWrapper::getIndex(const char* string, char* tokens,\r
64         int indexNumber) const {\r
65     char* string_copy;\r
66     char* token_pointer;\r
67     int index = 0;\r
68 \r
69     string_copy = (char*) malloc(sizeof(char) * (strlen(string) + 1));\r
70     if (!string_copy) {\r
71         fprintf(stderr, "getIndex(): could not allocate memory.\n");\r
72         return -1;\r
73     }\r
74 \r
75     strcpy(string_copy, string);\r
76 \r
77     token_pointer = strtok(string_copy, tokens);\r
78     do {\r
79         token_pointer = strtok(NULL, tokens);\r
80         index++;\r
81     } while (index <= indexNumber && token_pointer != 0);\r
82 \r
83     if (token_pointer) {\r
84         index = atoi(token_pointer);\r
85         free(string_copy);\r
86         return index;\r
87     }\r
88 \r
89     return -1;\r
90 }\r
91 \r
92 \r
93 /**\r
94  * Get the index of this process.\r
95  * @param indexNumber position of index (starting at 0)\r
96  */\r
97 int ProcessWrapper::getIndex(unsigned indexNumber) const {\r
98     if (indexNumber < 4) {\r
99         return _iteratorIndex[indexNumber];\r
100     }\r
101     return -1;\r
102 }\r
103 \r
104 \r
105 /**\r
106  * Get the name of this process.\r
107  */\r
108 char* ProcessWrapper::getName() const {\r
109     return _name;\r
110 }\r
111 \r
112 \r
113 /**\r
114  *\r
115  */\r
116 #ifdef INCLUDE_PROFILER\r
117 void ProcessWrapper::addToProfile(const char *event, void *port,\r
118     int length) {\r
119     if (profiler_output_file != NULL) {\r
120         fprintf(profiler_output_file, "%u %s %s %p %d\n",\r
121                 profiler_event_counter++, _name, event, port,\r
122                 length);\r
123 \r
124     } else {\r
125         printf("profiler_output_file does not exist");\r
126     }\r
127 }\r
128 #endif\r