Boolean IsShioFile(char* path) { // the first line of the file needs to be "[StationInfo]" Boolean bIsOurFile = false; OSErr err = noErr; long line; char strLine [256]; char firstPartOfFile [256]; long lenToRead,fileLength; err = MyGetFileSize(0,0,path,&fileLength); if(err) return false; lenToRead = _min(256,fileLength); err = ReadSectionOfFile(0,0,path,0,lenToRead,firstPartOfFile,0); firstPartOfFile[lenToRead-1] = 0; // make sure it is a cString if (!err) { NthLineInTextNonOptimized (firstPartOfFile, line = 0, strLine, 256); RemoveTrailingWhiteSpace(strLine); if (!strcmpnocase(strLine,"[StationInfo]")) bIsOurFile = true; } return bIsOurFile; }
OSErr TriCurMover_c::ReadTimeData(long index,VelocityFH *velocityH, char* errmsg) { char s[256], path[256]; long i,j,line = 0; long offset,lengthToRead; CHARH h = 0; char *sectionOfFile = 0; char *strToMatch = 0; long len,numScanned; VelocityFH velH = 0; long totalNumberOfVels = 0; LongPointHdl ptsHdl = 0; //TopologyHdl topoH = GetTopologyHdl(); //TTriGridVel* triGrid = (TTriGridVel*)fGrid; OSErr err = 0; DateTimeRec time; Seconds timeSeconds; //long numPoints, numDepths; long numTris; errmsg[0]=0; strcpy(path,fVar.pathName); if (!path || !path[0]) return -1; lengthToRead = (*fTimeDataHdl)[index].lengthOfData; offset = (*fTimeDataHdl)[index].fileOffsetToStartOfData; if (fDepthDataInfo) numTris = _GetHandleSize((Handle)fDepthDataInfo)/sizeof(**fDepthDataInfo); //if(topoH) //numTris = _GetHandleSize((Handle)topoH)/sizeof(**topoH); else {err=-1; goto done;} // no data h = (CHARH)_NewHandle(lengthToRead+1); if(!h){TechError("TriCurMover::ReadTimeData()", "_NewHandle()", 0); err = memFullErr; goto done;} _HLock((Handle)h); sectionOfFile = *h; err = ReadSectionOfFile(0,0,path,offset,lengthToRead,sectionOfFile,0); if(err || !h) { char firstPartOfLine[128]; sprintf(errmsg,"Unable to open data file:%s",NEWLINESTRING); strncpy(firstPartOfLine,path,120); strcpy(firstPartOfLine+120,"..."); strcat(errmsg,firstPartOfLine); goto done; } sectionOfFile[lengthToRead] = 0; // make it a C string //numDepths = fVar.maxNumDepths; // for now we will always have a full set of velocities totalNumberOfVels = (*fDepthDataInfo)[numTris-1].indexToDepthData+(*fDepthDataInfo)[numTris-1].numDepths; //totalNumberOfVels = numTris*numDepths; if(totalNumberOfVels<numTris) {err=-1; goto done;} // must have at least full set of 2D velocity data velH = (VelocityFH)_NewHandleClear(sizeof(**velH)*totalNumberOfVels); if(!velH){TechError("TriCurMover::ReadTimeData()", "_NewHandle()", 0); err = memFullErr; goto done;} strToMatch = "[TIME]"; len = strlen(strToMatch); NthLineInTextOptimized (sectionOfFile, line = 0, s, 256); if(!strncmp(s,strToMatch,len)) { numScanned=sscanf(s+len, "%hd %hd %hd %hd %hd", &time.day, &time.month, &time.year, &time.hour, &time.minute) ; if (numScanned!= 5) { err = -1; TechError("TriCurMover::ReadTimeData()", "sscanf() == 5", 0); goto done; } // check for constant current if (time.day == -1 && time.month == -1 && time.year == -1 && time.hour == -1 && time.minute == -1) //if (time.year == time.month == time.day == time.hour == time.minute == -1) { timeSeconds = CONSTANTCURRENT; } else // time varying current { if (time.year < 1900) // two digit date, so fix it { if (time.year >= 40 && time.year <= 99) time.year += 1900; else time.year += 2000; // correct for year 2000 (00 to 40) } time.second = 0; DateToSeconds (&time, &timeSeconds); } // check time is correct if (timeSeconds!=(*fTimeDataHdl)[index].time) { err = -1; strcpy(errmsg,"Can't read data - times in the file have changed."); goto done; } line++; } for(i=0;i<numTris;i++) // interior points { VelocityRec vel; char *startScan; long scanLength,stringIndex=0; long numDepths = (*fDepthDataInfo)[i].numDepths; // allow for variable depths/velocites //long numDepths = fVar.maxNumDepths; char *s1 = new char[numDepths*64]; if(!s1) {TechError("TriCurMover::ReadTimeData()", "new[]", 0); err = memFullErr; goto done;} NthLineInTextOptimized (sectionOfFile, line, s1, numDepths*64); //might want to check that the number of lines matches the number of triangles (ie there is data at every triangle) startScan = &s1[stringIndex]; for(j=0;j<numDepths;j++) { err = ScanVelocity(startScan,&vel,&scanLength); // ScanVelocity is faster than scanf, but doesn't handle scientific notation. Try a scanf on error. if (err) { if(err!=-2 || sscanf(&s1[stringIndex],lfFix("%lf%lf"),&vel.u,&vel.v) < 2) { char firstPartOfLine[128]; sprintf(errmsg,"Unable to read velocity data from line %ld:%s",line,NEWLINESTRING); strncpy(firstPartOfLine,s1,120); strcpy(firstPartOfLine+120,"..."); strcat(errmsg,firstPartOfLine); delete[] s1; s1=0; goto done; } err = 0; } (*velH)[(*fDepthDataInfo)[i].indexToDepthData+j].u = vel.u; (*velH)[(*fDepthDataInfo)[i].indexToDepthData+j].v = vel.v; //(*velH)[i*numDepths+j].u = vel.u; //(*velH)[i*numDepths+j].v = vel.v; stringIndex += scanLength; startScan = &s1[stringIndex]; } line++; delete[] s1; s1=0; } *velocityH = velH; done: if(h) { _HUnlock((Handle)h); DisposeHandle((Handle)h); h = 0; } if(err) { if(!errmsg[0]) strcpy(errmsg,"An error occurred in TriCurMover::ReadTimeData"); //printError(errmsg); // This alert causes a freeze up... // We don't want to put up an error message here because it can lead to an infinite loop of messages. if(velH) {DisposeHandle((Handle)velH); velH = 0;} } return err; }