dol: initial dol commit
[jump.git] / dol / src / dol / visitor / PipeAndFilter / lib / ProcessWrapper.cpp
diff --git a/dol/src/dol/visitor/PipeAndFilter/lib/ProcessWrapper.cpp b/dol/src/dol/visitor/PipeAndFilter/lib/ProcessWrapper.cpp
new file mode 100644 (file)
index 0000000..fbba359
--- /dev/null
@@ -0,0 +1,128 @@
+#include "ProcessWrapper.h"\r
+#include "dolSupport.h"\r
+\r
+/**\r
+ *\r
+ */\r
+ProcessWrapper::ProcessWrapper(char* name) {\r
+    _name = new char[strlen(name) + 1];\r
+    strcpy(_name, name);\r
+\r
+    _isDetached = false;\r
+    for (int i = 0; i < 4; i++) {\r
+        _iteratorIndex[i] = getIndex(_name, "_", i);\r
+    }\r
+}\r
+\r
+/**\r
+ *\r
+ */\r
+ProcessWrapper::~ProcessWrapper() {\r
+    if (_name) {\r
+        delete _name;\r
+    }\r
+}\r
+\r
+/**\r
+ *\r
+ */\r
+void ProcessWrapper::initialize() {\r
+    _process.init(&_process);\r
+}\r
+\r
+/**\r
+ *\r
+ */\r
+int ProcessWrapper::fire()\r
+{\r
+    return _process.fire(&_process);\r
+}\r
+\r
+\r
+/**\r
+ *\r
+ */\r
+void ProcessWrapper::detach() {\r
+    _isDetached = true;\r
+}\r
+\r
+\r
+/**\r
+ * Gets an index of a string, where the index must be separated by\r
+ * a character specified in tokens.\r
+ * Returns -1, when an error occurs.\r
+ *\r
+ * Example:\r
+ * getIndex("name_1_2", "_", 0) will return 1.\r
+ * getIndex("name_1_2", "_", 1) will return 2.\r
+ *\r
+ * @param string string to parse\r
+ * @param tokens delimiter of indices\r
+ * @param indexNumber position of index (starting at 0)\r
+ */\r
+int ProcessWrapper::getIndex(const char* string, char* tokens,\r
+        int indexNumber) const {\r
+    char* string_copy;\r
+    char* token_pointer;\r
+    int index = 0;\r
+\r
+    string_copy = (char*) malloc(sizeof(char) * (strlen(string) + 1));\r
+    if (!string_copy) {\r
+        fprintf(stderr, "getIndex(): could not allocate memory.\n");\r
+        return -1;\r
+    }\r
+\r
+    strcpy(string_copy, string);\r
+\r
+    token_pointer = strtok(string_copy, tokens);\r
+    do {\r
+        token_pointer = strtok(NULL, tokens);\r
+        index++;\r
+    } while (index <= indexNumber && token_pointer != 0);\r
+\r
+    if (token_pointer) {\r
+        index = atoi(token_pointer);\r
+        free(string_copy);\r
+        return index;\r
+    }\r
+\r
+    return -1;\r
+}\r
+\r
+\r
+/**\r
+ * Get the index of this process.\r
+ * @param indexNumber position of index (starting at 0)\r
+ */\r
+int ProcessWrapper::getIndex(unsigned indexNumber) const {\r
+    if (indexNumber < 4) {\r
+        return _iteratorIndex[indexNumber];\r
+    }\r
+    return -1;\r
+}\r
+\r
+\r
+/**\r
+ * Get the name of this process.\r
+ */\r
+char* ProcessWrapper::getName() const {\r
+    return _name;\r
+}\r
+\r
+\r
+/**\r
+ *\r
+ */\r
+#ifdef INCLUDE_PROFILER\r
+void ProcessWrapper::addToProfile(const char *event, void *port,\r
+    int length) {\r
+    if (profiler_output_file != NULL) {\r
+        fprintf(profiler_output_file, "%u %s %s %p %d\n",\r
+                profiler_event_counter++, _name, event, port,\r
+                length);\r
+\r
+    } else {\r
+        printf("profiler_output_file does not exist");\r
+    }\r
+}\r
+#endif\r