void CStringz::Lock() { if (!IsLocked) { _HLock(mh); str=(char *)_HandToPtr(mh); /* if (!str) { _UserError("5"); } */ IsLocked=TRUE; } }
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; }
CStringz operator +(CStringz&p1,CStringz &p2){ //add CStringz temp; temp.mh=_AllocHand(strlen(p1.str)+strlen(p2.str)+1); if (temp.mh==0) _Error(182); _HLock(temp.mh); temp.str=(char *)_HandToPtr(temp.mh); p1.Refresh(); strcpy(temp.str,p1.str); p2.Refresh(); strcat(temp.str,p2.str); _HUnLock(temp.mh); return temp; }
CStringz & CStringz::operator =(Value * v) { //assignment if (v->ev_type!='C') _UserError("Value: Wrong type"); UnLock(); if (!mh) { mh=_AllocHand(v->ev_length+1); } else { _SetHandSize(mh,v->ev_length+1); } if (!mh) _Error(182); Lock(); _HLock(v->ev_handle); _MemMove(str,(char *)_HandToPtr(v->ev_handle),v->ev_length); str[v->ev_length]='\0'; _HUnLock(v->ev_handle); UnLock(); return *this; }
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; }
// will need to read from text file instead YEARDATAHDL GetYearData(short year) { // IMPORTANT: The calling function should NOT dispose the handle it gets YEARDATAHDL yrHdl=nil; short yearMinus1990 = year-1990; long i,n,resSize=0; if(0<= yearMinus1990 && yearMinus1990 <kMAXNUMSAVEDYEARS) { if(gYearDataHdl1990Plus[yearMinus1990]) return gYearDataHdl1990Plus[yearMinus1990]; } #ifdef MAC Handle r = nil; r=GetResource('YEAR',(long)year); #ifdef SWAP_BINARY resSize = GetMaxResourceSize(r); if(resSize > 0 && r) { yrHdl = (YEARDATAHDL)_NewHandle(resSize); if(yrHdl) { _HLock(r); // so it can't be purged !!! YEARDATAHDL rHdl = (YEARDATAHDL)_NewHandle(resSize); DetachResource(r); rHdl = (YEARDATAHDL) r; // copy and swap the bytes n = resSize/sizeof(YEARDATA); for(i = 0; i< n; i++) { YEARDATA yrd = (YEARDATA)INDEXH(rHdl,i); SwapFloat(&yrd.XODE); SwapFloat(&yrd.VPU); INDEXH(yrHdl,i) = yrd; } // I don't think we free something gotten from a resource } ReleaseResource(r);// don't dispose of a resource handle !!! r = 0; } #else if(r) { DetachResource(r); yrHdl = (YEARDATAHDL) r; } #endif #else char numStr[32]; HRSRC hResInfo =0; HGLOBAL r = 0; sprintf(numStr,"#%ld",year); hResInfo = FindResource(hInst,numStr,"YEAR"); if(hResInfo) { // copy the handle so we can be // just like the mac // //also we need to swap the bytes // // be careful r is a HGLOBAL, not one of our special fake handles resSize = SizeofResource(hInst,hResInfo); if(resSize > 0) r = LoadResource(hInst,hResInfo); if(resSize > 0 && r) { yrHdl = (YEARDATAHDL)_NewHandle(resSize); if(yrHdl) { YEARDATAPTR rPtr = (YEARDATAPTR) LockResource(r); // copy and swap the bytes n = resSize/sizeof(YEARDATA); for(i = 0; i< n; i++) { YEARDATA yrd = rPtr[i]; SwapFloat(&yrd.XODE); SwapFloat(&yrd.VPU); INDEXH(yrHdl,i) = yrd; } // WIN32 applications do not have to unlock resources locked by LockResource // I don't think we free something gotten from a resource } } } #endif if(yrHdl && 0<= yearMinus1990 && yearMinus1990 <kMAXNUMSAVEDYEARS) { gYearDataHdl1990Plus[yearMinus1990] = yrHdl; } return(yrHdl); }
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; }