dol: initial dol commit
[jump.git] / dol / src / dol / visitor / systemC / lib / dol.c
diff --git a/dol/src/dol/visitor/systemC/lib/dol.c b/dol/src/dol/visitor/systemC/lib/dol.c
new file mode 100644 (file)
index 0000000..d852cf5
--- /dev/null
@@ -0,0 +1,81 @@
+#include "dol.h"\r
+\r
+/**\r
+ * Create the port name of an iterated port based on its basename and the\r
+ * given indices.\r
+ * Example: createPort(xxx, "port", 2, 1, N1, 3, N2) will result in xxx\r
+ * having the value "port_1_3". The range definitions N1, N2 will be ignored\r
+ * in this implementation\r
+ *\r
+ * @param port buffer where the result is stored\r
+ * @param base basename of the port\r
+ * @param number_of_indices number of dimensions of the port\r
+ * @param index_range_pairs index and range values for each dimension\r
+ */\r
+char* createPort(char* port, char* base,\r
+        int number_of_indices, int indices, ...) {\r
+    char next_index_string[10];\r
+    strcpy(port, base);\r
+\r
+    if (number_of_indices > 4) {\r
+        printf("Error: iterated ports must not have more than ");\r
+        printf("4 dimensions.\n");\r
+        exit(-1);\r
+    }\r
+\r
+    va_list argumentlist;\r
+    va_start(argumentlist, indices);\r
+\r
+    if (number_of_indices > 0) {\r
+      sprintf(next_index_string, "_%d", indices);\r
+      strcat(port, next_index_string);\r
+\r
+      for (int k = 1; k < number_of_indices; k++) {\r
+          va_arg(argumentlist, int); //skip the range value\r
+          sprintf(next_index_string, "_%d", va_arg(argumentlist, int));\r
+          strcat(port, next_index_string);\r
+      }\r
+    }\r
+    return port;\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 getIndex(const char* string, char* tokens, int indexNumber) {\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