char* ModelicaAllocateString(size_t len) { char *res = ModelicaAllocateStringWithErrorReturn(len); if (!res) { ModelicaFormatError("%s:%d: ModelicaAllocateString failed\n", __FILE__, __LINE__); } return res; }
static const char* ModelicaInternal_readLine(const char* fileName, int lineNumber, int* endOfFile) { /* Read line lineNumber from file fileName */ FILE* fp = ModelicaStreams_openFileForReading(fileName); char* line; int c; size_t lineLen; size_t iLines; long offset; /* Read upto line lineNumber-1 */ iLines = 0; c = 1; while ( iLines != (size_t) lineNumber-1 && c != EOF ) { c = fgetc(fp); while ( c != '\n' && c != EOF ) { c = fgetc(fp); } iLines++; } if ( iLines != (size_t) lineNumber-1 ) goto END_OF_FILE; /* Determine length of line lineNumber */ offset = ftell(fp); lineLen = 0; c = fgetc(fp); while ( c != '\n' && c != EOF ) { lineLen++; c = fgetc(fp); } if ( lineLen == 0 && c == EOF ) goto END_OF_FILE; /* Read line lineNumber */ line = ModelicaAllocateStringWithErrorReturn(lineLen); if ( line == NULL ) goto ERROR; if ( fseek(fp, offset, SEEK_SET != 0) ) goto ERROR; if ( fread(line, sizeof(char), lineLen, fp) != lineLen ) goto ERROR; fclose(fp); line[lineLen] = '\0'; *endOfFile = 0; return line; /* End-of-File or error */ END_OF_FILE: fclose(fp); *endOfFile = 1; line = ModelicaAllocateString(0); return line; ERROR : fclose(fp); ModelicaFormatError("Error when reading line %i from file\n\"%s\":\n%s", lineNumber, fileName, strerror(errno)); return ""; }
static void ModelicaInternal_readFile(const char* fileName, const char* string[], size_t nLines) { /* Read file into string vector string[nLines] */ FILE* fp = ModelicaStreams_openFileForReading(fileName); char* line; int c; size_t lineLen; size_t iLines; long offset; size_t nc; /* Read data from file */ iLines = 1; while ( iLines <= nLines ) { /* Determine length of next line */ offset = ftell(fp); lineLen = 0; c = fgetc(fp); while ( c != '\n' && c != EOF ) { lineLen++; c = fgetc(fp); } /* Allocate storage for next line */ line = ModelicaAllocateStringWithErrorReturn(lineLen); if ( line == NULL ) { fclose(fp); ModelicaFormatError("Not enough memory to allocate string for reading line %i from file\n" "\"%s\".\n" "(this file contains %i lines)\n", iLines, fileName, nLines); } /* Read next line */ if ( fseek(fp, offset, SEEK_SET != 0) ) { fclose(fp); ModelicaFormatError("Error when reading line %i from file\n\"%s\":\n" "%s\n", iLines, fileName, strerror(errno)); }; nc = ( iLines < nLines ? lineLen+1 : lineLen); if ( fread(line, sizeof(char), nc, fp) != nc ) { fclose(fp); ModelicaFormatError("Error when reading line %i from file\n\"%s\"\n", iLines, fileName); }; line[lineLen] = '\0'; string[iLines-1] = line; iLines++; } fclose(fp); }
static void ModelicaInternal_readDirectory(const char* directory, int nFiles, const char** files) { /* Get all file and directory names in a directory in any order (must be very careful, to call closedir if an error occurs) */ #if defined(_WIN32) || defined(_POSIX_) int errnoTemp; int iFiles = 0; char *pName; struct dirent *pinfo; DIR* pdir; /* Open directory information inquiry */ pdir = opendir(directory); if ( pdir == NULL ) { ModelicaFormatError("1: Not possible to get file names of \"%s\":\n%s", directory, strerror(errno)); } /* Read file and directory names and store them in vector "files" */ errno = 0; while ( (pinfo = readdir(pdir)) != NULL ) { if ( (strcmp(pinfo->d_name, "." ) != 0) && (strcmp(pinfo->d_name, "..") != 0) ) { /* Check if enough space in "files" vector */ if ( iFiles >= nFiles ) { closedir(pdir); ModelicaFormatError("Not possible to get file names of \"%s\":\n" "More files in this directory as reported by nFiles (= %i)", directory, nFiles); } /* Allocate Modelica memory for file/directory name and copy name */ pName = ModelicaAllocateStringWithErrorReturn(strlen(pinfo->d_name)); if ( pName == NULL ) { errnoTemp = errno; closedir(pdir); if ( errnoTemp == 0 ) { ModelicaFormatError("Not possible to get file names of \"%s\":\n" "Not enough storage", directory); } else { ModelicaFormatError("Not possible to get file names of \"%s\":\n%s", directory, strerror(errnoTemp)); } } strcpy(pName, pinfo->d_name); /* Save pointer to file */ files[iFiles] = pName; iFiles++; }; }; if ( errno != 0 ) { errnoTemp = errno; closedir(pdir); ModelicaFormatError("Not possible to get file names of \"%s\":\n%s", directory, strerror(errnoTemp)); } /* Check, whether the whole "files" vector is filled and close inquiry */ if ( iFiles != nFiles) { closedir(pdir); ModelicaFormatError("Not possible to get file names of \"%s\":\n" "Less files (= %d) found as defined by argument nNames (= %d)", directory, iFiles, nFiles); } if ( closedir(pdir) != 0 ) { ModelicaFormatError("Not possible to get file names of \"%s\":\n", directory, strerror(errno)); } #else ModelicaNotExistError("ModelicaInternal_readDirectory"); #endif };