/** * Creates an expression wrapping an identifier */ struct memorycontainer* createIdentifierExpression(char * identifier) { struct memorycontainer* memoryContainer = (struct memorycontainer*) malloc(sizeof(struct memorycontainer)); if (doesVariableExist(identifier)) { memoryContainer->length=sizeof(unsigned char)+sizeof(unsigned short); memoryContainer->data=(char*) malloc(memoryContainer->length); memoryContainer->lineDefns=NULL; int location=appendStatement(memoryContainer, IDENTIFIER_TOKEN, 0); appendVariable(memoryContainer, getVariableId(identifier, 0), location); } else { memoryContainer->length=sizeof(unsigned char)+sizeof(unsigned short); memoryContainer->data=(char*) malloc(memoryContainer->length); appendStatement(memoryContainer, FN_ADDR_TOKEN, 0); struct lineDefinition * defn = (struct lineDefinition*) malloc(sizeof(struct lineDefinition)); defn->next=NULL; defn->type=4; defn->linenumber=line_num; defn->name=(char*) malloc(strlen(identifier)+1); strcpy(defn->name, identifier); defn->currentpoint=sizeof(unsigned char); memoryContainer->lineDefns=defn; if (currentCall==NULL) { mainCodeCallTree.calledFunctions[mainCodeCallTree.number_of_calls]=(char*)malloc(strlen(identifier)+1); strcpy(mainCodeCallTree.calledFunctions[mainCodeCallTree.number_of_calls++], identifier); } else { currentCall->calledFunctions[currentCall->number_of_calls]=(char*)malloc(strlen(identifier)+1); strcpy(currentCall->calledFunctions[currentCall->number_of_calls++], identifier); } } return memoryContainer; }
/** * Returns a pointer to a std::vector<float> containing the values of the selected variable * stored in the selected file. This allocates a new std::vector<float> pointer. Make sure you * delete the contents when you done using it, or you will have a memory leak. * @param variable * @return std::vector<float> containing the values of the selected variable. */ std::vector<float>* CDFFileReader::getVariable(long variable) { //std::cout << "reading " << variable << std::endl; //get variable number std::vector<float>* variableData = new std::vector<float>(); if(doesVariableExist(variable)) { long recStart = 0L; long recCount = 1L; long recInterval = 1L; long dimIndices[] = { 0 }; long dimIntervals[] = { 1 }; long counts[1]; //get dim sizes //std::cout << BOOST_CURRENT_FUNCTION << ": current_file_id: " << current_file_id << std::endl; CDFgetzVarDimSizes(current_file_id, variable, counts); long dataType; CDFgetzVarDataType(current_file_id, variable, &dataType); if (dataType == CDF_FLOAT) { float * buffer = new float[counts[0]]; CDFhyperGetzVarData(current_file_id, variable, recStart, recCount, recInterval, dimIndices, counts, dimIntervals, buffer); //add data to vector type, and delete original array variableData->reserve(counts[0]); for (long i = 0; i < counts[0]; i++) { variableData->push_back(buffer[i]); } delete[] buffer; } else if (dataType == CDF_DOUBLE) { double * buffer = new double[counts[0]]; CDFhyperGetzVarData(current_file_id, variable, recStart, recCount, recInterval, dimIndices, counts, dimIntervals, buffer); //add data to vector type, and delete original array variableData->reserve(counts[0]); for (long i = 0; i < counts[0]; i++) { variableData->push_back((float)buffer[i]); } delete[] buffer; } //std::cout << "finished reading " << variable << std::endl; //std::cout << "size of variable: " << variableData.size() << std::endl; //std::cout << "dimSizes[0]: " << dimSizes[0] << std::endl; } return variableData; }
/** * Appends and returns a call function, this is added as a placeholder and then resolved at the end to point to the absolute byte code location * which is needed as the function might appear at any point */ struct memorycontainer* appendCallFunctionStatement(char* functionName, struct stack_t* args) { char * last_dot=strrchr(functionName,'.'); if (last_dot != NULL) functionName=last_dot+1; if (currentFunctionName != NULL && strcmp(currentFunctionName, functionName) == 0) isFnRecursive=1; struct memorycontainer* assignmentContainer=NULL; unsigned short numArgs=(unsigned short) getStackSize(args); char *isArgIdentifier=(char*) malloc(numArgs); unsigned short *varIds=(unsigned short*) malloc(sizeof(unsigned short) * numArgs); char * varname=(char*) malloc(strlen(functionName)+5); int i; for (i=0;i<numArgs;i++) { struct memorycontainer* expression=getExpressionAt(args, i); unsigned char command=((unsigned char*) expression->data)[0]; if (command != IDENTIFIER_TOKEN) { isArgIdentifier[i]=0; sprintf(varname,"%s#%d", functionName, i); if (assignmentContainer == NULL) { assignmentContainer=appendLetStatement(varname, getExpressionAt(args, i)); } else { assignmentContainer=concatenateMemory(assignmentContainer, appendLetStatement(varname, getExpressionAt(args, i))); } } else { isArgIdentifier[i]=1; varIds[i]=*((unsigned short*) (&((char*) expression->data)[1])); free(expression->data); } } struct memorycontainer* memoryContainer = (struct memorycontainer*) malloc(sizeof(struct memorycontainer)); memoryContainer->length=sizeof(unsigned short)*(2+numArgs)+sizeof(unsigned char); memoryContainer->data=(char*) malloc(memoryContainer->length); unsigned int position=0; if (doesVariableExist(functionName)) { memoryContainer->lineDefns=NULL; position=appendStatement(memoryContainer, FNCALL_BY_VAR_TOKEN, position); position=appendVariable(memoryContainer, getVariableId(functionName, 0), position); } else { struct lineDefinition * defn = (struct lineDefinition*) malloc(sizeof(struct lineDefinition)); defn->next=NULL; defn->type=3; defn->linenumber=line_num; defn->name=(char*) malloc(strlen(functionName)+1); strcpy(defn->name, functionName); defn->currentpoint=sizeof(unsigned char); memoryContainer->lineDefns=defn; position=appendStatement(memoryContainer, FNCALL_TOKEN, position); position+=sizeof(unsigned short); } position=appendVariable(memoryContainer, numArgs, position); for (i=0;i<numArgs;i++) { if (isArgIdentifier[i]) { position=appendVariable(memoryContainer, varIds[i], position); } else { sprintf(varname,"%s#%d", functionName, i); position=appendVariable(memoryContainer, getVariableId(varname, 0), position); } } clearStack(args); free(varname); free(isArgIdentifier); free(varIds); if (currentCall==NULL) { mainCodeCallTree.calledFunctions[mainCodeCallTree.number_of_calls]=(char*)malloc(strlen(functionName)+1); strcpy(mainCodeCallTree.calledFunctions[mainCodeCallTree.number_of_calls++], functionName); } else { currentCall->calledFunctions[currentCall->number_of_calls]=(char*)malloc(strlen(functionName)+1); strcpy(currentCall->calledFunctions[currentCall->number_of_calls++], functionName); } if (assignmentContainer != NULL) { return concatenateMemory(assignmentContainer, memoryContainer); } else { return memoryContainer; } }