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