OSErr GetScaleFactorFromUser(char *msg, double *scaleFactor) { long itemHit; char defaultAns[256]; char ans[256]; OSErr err = 0; sprintf(defaultAns,lfFix("%.1lf"),1.0); while(1) { // ask for a scale factor itemHit = REQUEST(msg,defaultAns,ans); if (itemHit == 2) // user cancelled { err = -1; break; } short numScanned = sscanf(ans,lfFix("%lf"),scaleFactor); if(! numScanned == 1) { printError("Error reading scale factor. Invalid entry."); err = -1; } if(*scaleFactor > 0 /*&& *scaleFactor <= maxScaleFactor*/) // valid entry, go on break; else // go back to dialog { //if (*scaleFactor > maxScaleFactor) sprintf(msg,lfFix("Maximum scale factor is %.1lf"),maxScaleFactor); if (*scaleFactor <= 0) sprintf(msg,"Scale factor must be positive"); printNote(msg); } } return err; }
// move to Genutil or Cross2... char *MyNumToStr(double x, char *s) { double ax = fabs(x); //if(ax <= .01) if(ax < .01) { if(ax < 1e-6) { sprintf(s,"0"); } //else sprintf(s,"%.3e",x); else sprintf(s,"%.2e",x); } //else sprintf(s,lfFix("%.3lf"),x); else sprintf(s,lfFix("%.2lf"),x); ChopEndZeros(s); if(strlen(s) > 1 && s[0] == '0') { return s+1; } else return s; }
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; }