X-Git-Url: http://sraa.de/git/?a=blobdiff_plain;f=dol%2Fsrc%2Fdol%2Fvisitor%2Fcbe%2Flib%2FProcessWrapperHelp.h;fp=dol%2Fsrc%2Fdol%2Fvisitor%2Fcbe%2Flib%2FProcessWrapperHelp.h;h=7d2a117de98a6109dcb8b65798a8e3be1dac026e;hb=8c411cf24ed0eb889191aaeafd8fa1e69081df42;hp=0000000000000000000000000000000000000000;hpb=dea7a4fb1ed110d3ce6e6d9255103d724bd66c0e;p=jump.git diff --git a/dol/src/dol/visitor/cbe/lib/ProcessWrapperHelp.h b/dol/src/dol/visitor/cbe/lib/ProcessWrapperHelp.h new file mode 100644 index 0000000..7d2a117 --- /dev/null +++ b/dol/src/dol/visitor/cbe/lib/ProcessWrapperHelp.h @@ -0,0 +1,100 @@ +/**************************************************************** + * Process Wrapper Help functions + * Creator: lschor, 2008-11-21 + * Description: General process wrapper fucntions + * + * Revision: + * - 2008-11-21: Created + */ + +#ifndef __PROCESS_WRAPPER_HELP_H__ +#define __PROCESS_WRAPPER_HELP_H__ + +#include // Used for va_list + +/** + * Gets an index of a string, where the index must be separated by + * a character specified in tokens. + * Returns -1, when an error occurs. + * + * Example: + * getIndex("name_1_2", "_", 0) will return 1. + * getIndex("name_1_2", "_", 1) will return 2. + * + * @param string string to parse + * @param tokens delimiter of indices + * @param indexNumber position of index (starting at 0) + */ +int getIndex(const char* string, char* tokens, int indexNumber) { + char* string_copy; + char* token_pointer; + int index = 0; + + string_copy = (char*) malloc(sizeof(char) * (strlen(string) + 1)); + if (!string_copy) { + fprintf(stderr, "getIndex(): could not allocate memory.\n"); + return -1; + } + + strcpy(string_copy, string); + + token_pointer = strtok(string_copy, tokens); + do { + token_pointer = strtok(NULL, tokens); + index++; + } while (index <= indexNumber && token_pointer != 0); + + if (token_pointer) { + index = atoi(token_pointer); + free(string_copy); + return index; + } + + free(string_copy); + return -1; +} + +/** + * Create the port name of an iterated port based on its basename and the + * given indices. + * + * @param port buffer where the result is stored (created using + * CREATEPORTVAR) + * @param base basename of the port + * @param number_of_indices number of dimensions of the port + * @param index_range_pairs index and range values for each dimension + */ +int *createPort(int *port, int base, int number_of_indices, + int index_range_pairs, ...) { + int index[4]; + int range[4]; + int i; + int value; + + va_list marker; + va_start(marker, index_range_pairs); + + value = index_range_pairs; + for (i = 0; i < number_of_indices; i++) { + index[i] = value; + value = va_arg(marker, int); + range[i] = value; + if (i < number_of_indices - 1) { + value = va_arg(marker, int); + } + } + + *port = base; + for (i = 0; i < number_of_indices; i++) { + int j; + int weight = 1; + for (j = 1; j < number_of_indices - i; j ++) { + weight *= range[j]; + } + *port += index[i] * weight; + } + + return port; +} + +#endif