示例#1
0
void freeElement(void* element){
    int i;
    Element* e = (Element*)element;
    ModelDescription* md;
    if (!e) return;
    // free attributes
    for (i=0; i<e->n; i+=2) 
        free((void *)e->attributes[i+1]);
    free(e->attributes);
    // free child nodes
    switch (getAstNodeType(e->type)) {
        case astListElement:
            freeList((void **)((ListElement*)e)->list);
            break;
        case astScalarVariable:
            freeList((void **)((ScalarVariable*)e)->directDependencies);
        case astType:
            freeElement(((Type*)e)->typeSpec);
            break;
        case astModelDescription:
            md = (ModelDescription*)e;
            freeList((void **)md->unitDefinitions);
            freeList((void **)md->typeDefinitions);
            freeElement(md->defaultExperiment);
            freeList((void **)md->vendorAnnotations);
            freeList((void **)md->modelVariables);
            break;
        case astElement:
           break;
        default:
           fprintf(stderr, "Ignoring unexpected AstNodeType %d\n", getAstNodeType(e->type));
    }
    // free the struct
    free(e);
}
示例#2
0
static void freeList(void** list){
    int i;
    if (!list) return;
    for (i=0; list[i]; i++) 
        freeElement(list[i]);
    free(list);
}
示例#3
0
int main(int argc, char *argv[]) {
    const char* fmuFileName;

    // parse command line arguments and load the FMU
    double tEnd = 1.0;
    double h=0.1;
    int loggingOn = 0;
    char csv_separator = ',';
    parseArguments(argc, argv, &fmuFileName, &tEnd, &h, &loggingOn, &csv_separator);
    loadFMU(fmuFileName);

    // run the simulation
    printf("FMU Simulator: run '%s' from t=0..%g with step size h=%g, loggingOn=%d, csv separator='%c'\n",
            fmuFileName, tEnd, h, loggingOn, csv_separator);
    simulate(&fmu, tEnd, h, loggingOn, csv_separator);
    printf("CSV file '%s' written\n", RESULT_FILE);

    // release FMU 
#ifdef _MSC_VER
    FreeLibrary(fmu.dllHandle);
#else
    dlclose(fmu.dllHandle);
#endif
    freeElement(fmu.modelDescription);
    deleteUnzippedFiles();
    return EXIT_SUCCESS;
}
示例#4
0
//////////////////////////////////////////////////////////////////////////////////
/// Parse the fmu. The receiver must call freeElement(md) to release AST memory.
///
///\param xmlPath The xml file to be parsed
///\return the root node md of the AST if no error occurred. NULL to indicate failure
/////////////////////////////////////////////////////////////////////////////////
ModelDescription* parse(const char* xmlPath) {
  ModelDescription* md = NULL;
  FILE *file;
  int done = 0;

  stack = stackNew(100, 10); // Allocate stack memory	  
  if (checkPointer(stack)) return NULL;  // Check if the stack is creatted

  parser = XML_ParserCreate(NULL); // Create an parser
  if (checkPointer(parser)) return NULL;  // Check if the parser is created

  XML_SetElementHandler(parser, startElement, endElement); // Set handler for start and end tags
  XML_SetCharacterDataHandler(parser, handleData); // Set handler for text

	file = fopen(xmlPath, "rb");

	if (file == NULL) {
    printfError("Cannot open file '%s'\n", xmlPath);
    XML_ParserFree(parser); // Free the memory for parser
    return NULL; // Failure
  }

  while (!done) {
    int n = fread(text, sizeof(char), XMLBUFSIZE, file); // Read XMLBUFSIZE characters from file
    if (n != XMLBUFSIZE) done = 1; // Reach the end of file
    if (!XML_Parse(parser, text, n, done)){
      printf("Parse error in file %s at line %d:\n%s\n",
             xmlPath,
             (int)XML_GetCurrentLineNumber(parser),
              XML_ErrorString(XML_GetErrorCode(parser)));
      while (!stackIsEmpty(stack)) md = stackPop(stack);
      if (md) freeElement(md);
      cleanup(file);
      return NULL; // failure
    }
  }

  md = stackPop(stack);
  assert(stackIsEmpty(stack));
  cleanup(file);
  //printElement(1, md); // Print the element for debugging
  return md; // Success if all refs are valid    
}
示例#5
0
// Returns NULL to indicate failure
// Otherwise, return the root node md of the AST.
// The receiver must call freeElement(md) to release AST memory.
ModelDescription* parse(const char* xmlPath) {
    ModelDescription* md = NULL;
    FILE *file;
    int done = 0;
    stack = stackNew(100, 10);
    if (!checkPointer(stack)) return NULL;  // failure
    parser = XML_ParserCreate(NULL);
    if (!checkPointer(parser)) return NULL;  // failure
    XML_SetElementHandler(parser, startElement, endElement);
    XML_SetCharacterDataHandler(parser, handleData);
  	file = fopen(xmlPath, "rb");
	if (file == NULL) {
        fprintf(stderr,"Cannot open file '%s'\n", xmlPath);
     	XML_ParserFree(parser);
        return NULL; // failure
    }
    while (!done) {
        int n = fread(text, sizeof(char), XMLBUFSIZE, file);
	    if (n != XMLBUFSIZE) done = 1;
        if (!XML_Parse(parser, text, n, done)){
             fprintf(stderr,"Parse error in file %s at line %d:\n%s\n", 
                     xmlPath,
                         (int)XML_GetCurrentLineNumber(parser),
	                 XML_ErrorString(XML_GetErrorCode(parser)));
             while (! stackIsEmpty(stack)) md = stackPop(stack);
             if (md) freeElement(md);
             cleanup(file);
             return NULL; // failure
        }
    }
    md = stackPop(stack);
    assert(stackIsEmpty(stack));
    cleanup(file);
    //printElement(1, md); // debug
    return md; // success if all refs are valid    
}
示例#6
0
///////////////////////////////////////////////////////////////////////////////
/// main routine of the demo code
///
///\param argc Number of arguments
///\param argv Arguments
///\return 0 if no error occurred
///////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[]) {
    const char* fmuFilNam;
    char* fmuPat;
    char* tmpPat;
    char* xmlPat;
    char* dllPat;
    
    // define default argument values
    double tEnd = 1.0;
    double h=0.1;
    int loggingOn = 0;
    char csv_separator = ',';  
   
    // parse command line arguments
    if (argc>1) {
        fmuFilNam = argv[1];
        
        if(!strcmp(fmuFilNam, "-h") | !strcmp(fmuFilNam, "--help")) {
          printHelp(argv[0]);
          return 0;
        }

        /*if(!strstr(fmuFilNam,"DoubleInputDoubleOutput.fmu")) {
          printf("Sorry, this demo only works with the FMU file DoubleInputDoubleOutput.fmu");
          return 0;
        }*/
    }
    else {
        printError("No fmu file.\n");
        printHelp(argv[0]);
        exit(EXIT_FAILURE);
    }
    if (argc>2) {
        if (sscanf(argv[2],"%lf", &tEnd) != 1) {
            printfError("The given end time (%s) is not a number.\n", argv[2]);
            exit(EXIT_FAILURE);
        }
    }
    if (argc>3) {
        if (sscanf(argv[3],"%lf", &h) != 1) {
            printfError("The given stepsize (%s) is not a number.\n", argv[3]);
            exit(EXIT_FAILURE);
        }
    }
    if (argc>4) {
        if (sscanf(argv[4],"%d", &loggingOn) != 1 || loggingOn<0 || loggingOn>1) {
            printfError("The given logging flag (%s) is not boolean.\n", argv[4]);
            exit(EXIT_FAILURE);
        }
        if(loggingOn) setDebug();
    }
    if (argc>5) {
        if (strlen(argv[5]) != 1) {
            printfError("The given CSV separator char (%s) is not valid.\n", argv[5]);
            exit(EXIT_FAILURE);
        }
        csv_separator = argv[5][0];
    }
    if (argc>6) {
        printf("warning: Ignoring %d additional arguments: %s ...\n", argc-6, argv[6]);
        printHelp(argv[0]);
    }

    // Get absolute path to FMU, NULL if not found  
    tmpPat = getTmpPath(fmuFilNam, strlen(fmuFilNam)-4);
    if(tmpPat==NULL){
      printError("Cannot allocate temporary path.\n");
      exit(EXIT_FAILURE);
    }

    // Unzip the FMU to the tmpPat directory
    if (unpack(fmuFilNam, tmpPat)) {  
        printfError("Fail to unpack fmu \"%s\".\n", fmuFilNam);
        exit(EXIT_FAILURE);
    }

    printDebug("parse tmpPat\\modelDescription.xml.\n");
    xmlPat = calloc(sizeof(char), strlen(tmpPat) + strlen(XML_FILE) + 1);
    sprintf(xmlPat, "%s%s", tmpPat, XML_FILE);

    // Parse only parses the model description and store in structure fmu.modelDescription
    fmu.modelDescription = parse(xmlPat); 
    free(xmlPat);
    if (!fmu.modelDescription) exit(EXIT_FAILURE);

    // Allocate the memory for dllPat
    dllPat = calloc(sizeof(char), strlen(tmpPat) + strlen(DLL_DIR) 
            + strlen( getModelIdentifier(fmu.modelDescription)) +  strlen(".dll") + 1); 
    sprintf(dllPat,"%s%s%s.dll", tmpPat, DLL_DIR, getModelIdentifier(fmu.modelDescription)); 

    // Load the FMU dll
    if (loadDll(dllPat, &fmu)) exit(EXIT_FAILURE); 
	  printfDebug("Loaded \"%s\"\n", dllPat); 

    free(dllPat);
    free(tmpPat);

    // Run the simulation
    printf("FMU Simulator: run '%s' from t=0..%g with step size h=%g, loggingOn=%d, csv separator='%c'\n", 
            fmuFilNam, tEnd, h, loggingOn, csv_separator);
    if (simulate(&fmu, tEnd, h, loggingOn, csv_separator)){
      printError("Simulation failed.\n");
      exit(EXIT_FAILURE);
    }

    printf("CSV file '%s' written", RESULT_FILE);

    // Release FMU 
    FreeLibrary(fmu.dllHandle);
    freeElement(fmu.modelDescription);
    return EXIT_SUCCESS;
}
示例#7
0
文件: main.c 项目: mtiller/fmusdk
int main(int argc, char *argv[]) {
    const char* fmuFileName;
    char* fmuPath;
    char* tmpPath;
    char* xmlPath;
    char* dllPath;

    // define default argument values
    double tEnd = 1.0;
    double h=0.1;
    int loggingOn = 0;
    char csv_separator = ';';

    // parse command line arguments
    if (argc>1) {
        fmuFileName = argv[1];
    }
    else {
        printf("error: no fmu file\n");
        printHelp(argv[0]);
        exit(EXIT_FAILURE);
    }
    if (argc>2) {
        if (sscanf(argv[2],"%lf", &tEnd) != 1) {
            printf("error: The given end time (%s) is not a number\n", argv[2]);
            exit(EXIT_FAILURE);
        }
    }
    if (argc>3) {
        if (sscanf(argv[3],"%lf", &h) != 1) {
            printf("error: The given stepsize (%s) is not a number\n", argv[3]);
            exit(EXIT_FAILURE);
        }
    }
    if (argc>4) {
        if (sscanf(argv[4],"%d", &loggingOn) != 1 || loggingOn<0 || loggingOn>1) {
            printf("error: The given logging flag (%s) is not boolean\n", argv[4]);
            exit(EXIT_FAILURE);
        }
    }
    if (argc>5) {
        if (strlen(argv[5]) != 1) {
            printf("error: The given CSV separator char (%s) is not valid\n", argv[5]);
            exit(EXIT_FAILURE);
        }
        csv_separator = argv[5][0];
    }
    if (argc>6) {
        printf("warning: Ignoring %d additional arguments: %s ...\n", argc-6, argv[6]);
        printHelp(argv[0]);
    }

    // get absolute path to FMU, NULL if not found
    fmuPath = getFmuPath(fmuFileName);
    if (!fmuPath) exit(EXIT_FAILURE);

    // unzip the FMU to the tmpPath directory
    tmpPath = getTmpPath();
    if (!unzip(fmuPath, tmpPath)) exit(EXIT_FAILURE);

    // parse tmpPath\modelDescription.xml
    xmlPath = calloc(sizeof(char), strlen(tmpPath) + strlen(XML_FILE) + 1);
    sprintf(xmlPath, "%s%s", tmpPath, XML_FILE);
    fmu.modelDescription = parse(xmlPath);
    free(xmlPath);
    if (!fmu.modelDescription) exit(EXIT_FAILURE);

    // load the FMU dll
    dllPath = calloc(sizeof(char), strlen(tmpPath) + strlen(DLL_DIR)
                     + strlen( getModelIdentifier(fmu.modelDescription)) +  strlen(".dll") + 1);
    sprintf(dllPath,"%s%s%s.dll", tmpPath, DLL_DIR, getModelIdentifier(fmu.modelDescription));
    if (!loadDll(dllPath, &fmu)) exit(EXIT_FAILURE);
    free(dllPath);
    free(fmuPath);
    free(tmpPath);

    // run the simulation
    printf("FMU Simulator: run '%s' from t=0..%g with step size h=%g, loggingOn=%d, csv separator='%c'\n",
           fmuFileName, tEnd, h, loggingOn, csv_separator);
    simulate(&fmu, tEnd, h, loggingOn, csv_separator);
    printf("CSV file '%s' written", RESULT_FILE);

    // release FMU
    FreeLibrary(fmu.dllHandle);
    freeElement(fmu.modelDescription);
    return EXIT_SUCCESS;
}