RideFile *TacxCafFileReader::openRideFile(QFile &file, QStringList &errors, QList<RideFile*>*) const { if (!file.open(QFile::ReadOnly)) { errors << ("Could not open ride file: \"" + file.fileName() + "\""); return NULL; } QByteArray bytes = file.readAll(); file.close(); qint16 version = readHeaderBlock(bytes.left(TACX_HEADER_BLOCK_SIZE), errors); if (version == 0) return NULL; return readBlocks(bytes.mid(TACX_HEADER_BLOCK_SIZE), version, errors); }
// This is the first prototype version of HSpiceRead function for reading HSpice // output files. // TODO: // ascii format support // different vector types support (like voltage, current ..., although I do not // know what it would be good for) // scale monotonity check static PyObject *HSpiceRead(PyObject *self, PyObject *args) { const char *fileName; char *token, *buf = NULL, **name = NULL; int debugMode, num, numOfVectors, numOfVariables, type, sweepSize = 1, i = dateStartPosition - 1, offset = 0; struct FastArray faSweep, *faPtr = NULL; int postVersion = 0; FILE *f = NULL; PyObject *date = NULL, *title = NULL, *scale = NULL, *sweep = NULL, *sweepValues = NULL, *dataList = NULL, **tmpArray = NULL, *sweeps = NULL, *tuple = NULL, *list = NULL; // Get hspice_read() arguments. if(!PyArg_ParseTuple(args, "si", &fileName, &debugMode)) return Py_None; if(debugMode) fprintf(debugFile, "HSpiceRead: reading file %s.\n", fileName); f = fopen(fileName, "rb"); // Open the file. if(f == NULL) { if(debugMode) fprintf(debugFile, "HSpiceRead: cannot open file %s.\n", fileName); goto failed; } num = getc(f); ungetc(num, f); if(num == EOF) // Test if there is data in the file. { if(debugMode) fprintf(debugFile, "HSpiceRead: file %s is empty.\n", fileName); goto failed; } if((num & 0x000000ff) >= ' ') // Test if the file is in binary format. { if(debugMode) fprintf(debugFile, "HSpiceRead: file %s is in ascii format.\n", fileName); goto failed; } // Read file header blocks. do num = readHeaderBlock(f, debugMode, fileName, &buf, &offset); while(num == 0); if(num > 0) goto failed; if(strncmp(&buf[postStartPosition1], "9007", numOfPostCharacters) == 0) postVersion = 9007; else if(strncmp(&buf[postStartPosition1], "9601", numOfPostCharacters) == 0) postVersion = 9601; else if(strncmp(&buf[postStartPosition2], "2001", numOfPostCharacters) == 0) postVersion = 2001; else { if(debugMode) fprintf(debugFile, "HSpiceRead: unknown post format.\n"); goto failed; } if(debugMode) fprintf(debugFile, "POST_VERSION=%d\n", postVersion); buf[dateEndPosition] = 0; date = PyString_FromString(&buf[dateStartPosition]); // Get creation date. if(date == NULL) { if(debugMode) fprintf(debugFile, "HSpiceRead: failed to create date string.\n"); goto failed; } while(buf[i] == ' ') i--; buf[i + 1] = 0; title = PyString_FromString(&buf[titleStartPosition]); // Get title. if(title == NULL) { if(debugMode) fprintf(debugFile, "HSpiceRead: failed to create title string.\n"); goto failed; } buf[numOfSweepsEndPosition] = 0; // Check number of sweep parameters. num = atoi(&buf[numOfSweepsPosition]); if(num < 0 || num > 1) { if(debugMode) fprintf(debugFile, "HSpiceRead: only onedimensional sweep supported.\n"); goto failed; } buf[numOfSweepsPosition] = 0; // Get number of vectors (variables and probes). numOfVectors = atoi(&buf[numOfProbesPosition]); buf[numOfProbesPosition] = 0; numOfVariables = atoi(&buf[numOfVariablesPosition]); // Scale included. numOfVectors = numOfVectors + numOfVariables; // Get type of variables. Scale is always real. token = strtok(&buf[vectorDescriptionStartPosition], " \t\n"); type = atoi(token); if(type == frequency) type = complex_var; else type = real_var; for(i = 0; i < numOfVectors; i++) token = strtok(NULL, " \t\n"); if(token == NULL) { if(debugMode) fprintf(debugFile, "HSpiceRead: failed to extract independent variable name.\n"); goto failed; } scale = PyString_FromString(token); // Get independent variable name. if(scale == NULL) { if(debugMode) fprintf(debugFile, "HSpiceRead: failed to create independent variable name string.\n"); goto failed; } // Allocate space for pointers to vector names. name = (char **)PyMem_Malloc((numOfVectors - 1) * sizeof(char *)); if(name == NULL) { if(debugMode) fprintf(debugFile, "HSpiceRead: cannot allocate pointers to vector names.\n"); goto failed; } for(i = 0; i < numOfVectors - 1; i++) // Get vector names. { name[i] = strtok(NULL, " \t\n"); if(name[i] == NULL) { if(debugMode) fprintf(debugFile, "HSpiceRead: failed to extract vector names.\n"); goto failed; } } // Process vector names: make name lowercase, remove v( in front of name for(i=0; i < numOfVectors - 1; i++) { int j; for(j=0;name[i][j];j++) { if (name[i][j]>='A' && name[i][j]<='Z') { name[i][j]-='A'-'a'; } } if (name[i][0]=='v' && name[i][1]=='(') { for(j=2;name[i][j];j++) { name[i][j-2]=name[i][j]; } name[i][j-2]=0; } } if(num == 1) // Get sweep information. { int num = getSweepInfo(debugMode, postVersion, &sweep, buf, &sweepSize, &sweepValues, &faSweep); if(num) goto failed; } dataList = PyList_New(0); // Create an empty list for data dictionaries. if(dataList == NULL) { if(debugMode) fprintf(debugFile, "HSpiceRead: failed to create data list.\n"); goto failed; } // Allocate space for pointers to arrays. tmpArray = (PyObject **)PyMem_Malloc(numOfVectors * sizeof(PyObject *)); if(tmpArray == NULL) { if(debugMode) fprintf(debugFile, "HSpiceRead: cannot allocate pointers to arrays.\n"); goto failed; } // Allocate space for fast array pointers. faPtr = (struct FastArray *)PyMem_Malloc(numOfVectors * sizeof(struct FastArray)); if(faPtr == NULL) { if(debugMode) fprintf(debugFile, "HSpiceRead: failed to create array.\n"); goto failed; } if(debugMode) fprintf(debugFile,"numOfVectors=%d\n", numOfVectors); for(i = 0; i < sweepSize; i++) // Read i-th table. { num = readTable(f, debugMode, postVersion, fileName, sweep, numOfVariables, type, numOfVectors, &faSweep, tmpArray, faPtr, token, name, dataList); if(num) goto failed; } fclose(f); f = NULL; PyMem_Free(faPtr); faPtr = NULL; PyMem_Free(buf); buf = NULL; PyMem_Free(name); name = NULL; PyMem_Free(tmpArray); tmpArray = NULL; // Create sweeps tuple. if(sweep == NULL) sweeps = PyTuple_Pack(3, Py_None, Py_None, dataList); else sweeps = PyTuple_Pack(3, sweep, sweepValues, dataList); if(sweeps == NULL) { if(debugMode) fprintf(debugFile, "HSpiceRead: failed to create tuple with sweeps.\n"); goto failed; } Py_XDECREF(sweep); Py_XDECREF(sweepValues); Py_XDECREF(dataList); // Prepare return tuple. tuple = PyTuple_Pack(6, sweeps, scale, Py_None, title, date, Py_None); if(tuple == NULL) { if(debugMode) fprintf(debugFile, "HSpiceRead: failed to create tuple with read data.\n"); goto failed; } Py_XDECREF(date); date = NULL; Py_XDECREF(title); title = NULL; Py_XDECREF(scale); scale = NULL; Py_XDECREF(sweeps); sweeps = NULL; list = PyList_New(0); // Create an empty list. if(list == NULL) { if(debugMode) fprintf(debugFile, "HSpiceRead: failed to create return list.\n"); goto failed; } num = PyList_Append(list, tuple); // Insert tuple into return list. if(num) { if(debugMode) fprintf(debugFile, "HSpiceRead: failed to append tuple to return list.\n"); goto failed; } Py_XDECREF(tuple); return list; failed: // Error occured. Close open file, relese memory and python references. if (f) fclose(f); PyMem_Free(buf); Py_XDECREF(date); Py_XDECREF(title); Py_XDECREF(scale); PyMem_Free(name); Py_XDECREF(sweep); Py_XDECREF(sweepValues); Py_XDECREF(dataList); PyMem_Free(tmpArray); PyMem_Free(faPtr); Py_XDECREF(sweeps); Py_XDECREF(tuple); Py_XDECREF(list); return Py_None; }