long MyGetScrap(Handle hDest, ResType theType) { // returns the length // If the handle is not nil, it sizes handle to hold the data and fills in the data #if TARGET_API_MAC_CARBON ScrapRef scrapRef; ScrapFlavorType flavorType = theType; Size byteCount,numBytesAllocated; OSStatus err = GetCurrentScrap (&scrapRef); if(err) return 0; // err = GetScrapFlavorSize(scrapRef,flavorType,&byteCount); if(err) return 0; if(byteCount == 0) return 0; if(hDest) { // then the caller wants the data filled in ( _SetHandleSize(hDest,byteCount); numBytesAllocated = _GetHandleSize(hDest); // _MemError(); if(byteCount != numBytesAllocated){// we had a memory err, the requested space was not allocated SysBeep(5);return(0); } _HLock(hDest); err = GetScrapFlavorData(scrapRef,flavorType,&byteCount,*hDest); _HUnlock(hDest); if(err) return 0; } return byteCount; #else long offset; // note: returned value is not used return GetScrap(hDest, theType, &offset); #endif }
void GetObjectLabel (ObjectRecHdl objectHdl, char *objectName) { _HLock ((Handle) objectHdl); strcpy (objectName, (**objectHdl).objectLabel); _HUnlock ((Handle) objectHdl); return; }
OSErr WriteObjectInfo (short FRefNum, ObjectRecHdl thisObjectHdl) { OSErr ErrCode = 0; long byteCount, structSize; // write the size of this object structSize = _GetHandleSize ((Handle) thisObjectHdl); byteCount = sizeof (long); ErrCode = FSWrite (FRefNum, &byteCount, (Ptr) &structSize); if (!ErrCode) { _HLock ((Handle) thisObjectHdl); ErrCode = FSWrite (FRefNum, &structSize, (Ptr) *thisObjectHdl); _HUnlock ((Handle) thisObjectHdl); if (!ErrCode) // write the contents of the data handle if necessary { Handle thisObjectDataHdl; thisObjectDataHdl = GetObjectDataHdl (thisObjectHdl); if (thisObjectDataHdl != nil) { // write the size of the data handle first structSize = _GetHandleSize (thisObjectDataHdl); byteCount = sizeof (long); ErrCode = FSWrite (FRefNum, &byteCount, (Ptr) &structSize); if (!ErrCode) { _HLock ((Handle) thisObjectDataHdl); ErrCode = FSWrite (FRefNum, &structSize, (Ptr) *thisObjectDataHdl); _HUnlock ((Handle) thisObjectDataHdl); } } } } return (ErrCode); }
void SetObjectLabel (ObjectRecHdl ObjectHdl, char *ObjectName) { _HLock ((Handle) ObjectHdl); if (strlen (ObjectName) < kObjectNameLen) strcpy ((**ObjectHdl).objectLabel, ObjectName); else strncpy ((**ObjectHdl).objectLabel, ObjectName, kObjectNameLen - 1); _HUnlock ((Handle) ObjectHdl); return; }
void SortObjectsBySize (CMyList *thisObjectList) // subroutine to create and add a new poly region handle. // Note: default parameters are also inserted into certain poly data fields { long objectCount; Handle ListDataHdl; Ptr ListDataPtr; Size ListDataSize; objectCount = thisObjectList -> GetItemCount (); ListDataHdl = thisObjectList -> GetListDataHdl (); ListDataSize = thisObjectList -> GetItemSize (); _HLock (ListDataHdl); ListDataPtr = *ListDataHdl; qsort (ListDataPtr, objectCount, ListDataSize, ObjSizeComp); _HUnlock (ListDataHdl); return; }
OSErr ReadObjectInfo (short FRefNum, ObjectRecHdl *thisObjectHdlPtr) { OSErr ErrCode = 0; long byteCount, structSize; Handle thisObjectHdl = nil, thisObjectDataHdl = nil; // read the size of this object's info handle byteCount = sizeof (long); ErrCode = FSRead (FRefNum, &byteCount, (Ptr) &structSize); if (!ErrCode) { thisObjectHdl = _NewHandleClear (structSize); if (thisObjectHdl != nil) { _HLock (thisObjectHdl); ErrCode = FSRead (FRefNum, &structSize, (Ptr) *thisObjectHdl); _HUnlock (thisObjectHdl); } else ErrCode = memFullErr; } if (!ErrCode) // read the object's data handle if any { if (GetObjectDataHdl ((ObjectRecHdl) thisObjectHdl) != nil) { // read the size of this object's data handle byteCount = sizeof (long); ErrCode = FSRead (FRefNum, &byteCount, (Ptr) &structSize); if (!ErrCode) { thisObjectDataHdl = _NewHandleClear (structSize); if (thisObjectDataHdl != nil) { _HLock (thisObjectDataHdl); ErrCode = FSRead (FRefNum, &structSize, (Ptr) *thisObjectDataHdl); _HUnlock (thisObjectDataHdl); if (!ErrCode) SetObjectDataHdl ((ObjectRecHdl) thisObjectHdl, thisObjectDataHdl); } else ErrCode = memFullErr; } } } if (ErrCode) { if (thisObjectHdl != nil) DisposeHandle (thisObjectDataHdl); if (thisObjectHdl != nil) DisposeHandle (thisObjectDataHdl); } if (!ErrCode) *thisObjectHdlPtr = (ObjectRecHdl) thisObjectHdl; // send this handle back return (ErrCode); }
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; }