static void flushRecord( DBFHandle psDBF ) { unsigned int nRecordOffset; if( psDBF->bCurrentRecordModified && psDBF->nCurrentRecord > -1 ) { psDBF->bCurrentRecordModified = MS_FALSE; nRecordOffset = psDBF->nRecordLength * psDBF->nCurrentRecord + psDBF->nHeaderLength; safe_fseek( psDBF->fp, nRecordOffset, 0 ); fwrite( psDBF->pszCurrentRecord, psDBF->nRecordLength, 1, psDBF->fp ); } }
FILE* factor_vm::open_image(vm_parameters* p) { if (p->embedded_image) { FILE* file = OPEN_READ(p->executable_path); if (file == NULL) { std::cout << "Cannot open embedded image" << std::endl; char *msg = threadsafe_strerror(errno); std::cout << "strerror:1: " << msg << std::endl; free(msg); exit(1); } embedded_image_footer footer; if (!read_embedded_image_footer(file, &footer)) { std::cout << "No embedded image" << std::endl; exit(1); } safe_fseek(file, (off_t)footer.image_offset, SEEK_SET); return file; } else return OPEN_READ(p->image_path); }
// Read an image file from disk, only done once during startup // This function also initializes the data and code heaps void factor_vm::load_image(vm_parameters* p) { FILE* file = OPEN_READ(p->image_path); if (file == NULL) { std::cout << "Cannot open image file: " << p->image_path << std::endl; char *msg = threadsafe_strerror(errno); std::cout << "strerror:2: " << msg << std::endl; free(msg); exit(1); } if (p->embedded_image) { embedded_image_footer footer; if (!read_embedded_image_footer(file, &footer)) { std::cout << "No embedded image" << std::endl; exit(1); } safe_fseek(file, (off_t)footer.image_offset, SEEK_SET); } image_header h; if (raw_fread(&h, sizeof(image_header), 1, file) != 1) fatal_error("Cannot read image header", 0); if (h.magic != image_magic) fatal_error("Bad image: magic number check failed", h.magic); if (h.version != image_version) fatal_error("Bad image: version number check failed", h.version); load_data_heap(file, &h, p); load_code_heap(file, &h, p); raw_fclose(file); // Certain special objects in the image are known to the runtime memcpy(special_objects, h.special_objects, sizeof(special_objects)); cell data_offset = data->tenured->start - h.data_relocation_base; cell code_offset = code->allocator->start - h.code_relocation_base; fixup_heaps(data_offset, code_offset); }
bool factor_vm::read_embedded_image_footer(FILE* file, embedded_image_footer* footer) { safe_fseek(file, -(off_t)sizeof(embedded_image_footer), SEEK_END); safe_fread(footer, (off_t)sizeof(embedded_image_footer), 1, file); return footer->magic == image_magic; }
static int msDBFWriteAttribute(DBFHandle psDBF, int hEntity, int iField, void * pValue ) { unsigned int nRecordOffset; int i, j; uchar *pabyRec; char szSField[40], szFormat[12]; /* -------------------------------------------------------------------- */ /* Is this a valid record? */ /* -------------------------------------------------------------------- */ if( hEntity < 0 || hEntity > psDBF->nRecords ) return( MS_FALSE ); if( psDBF->bNoHeader ) writeHeader(psDBF); /* -------------------------------------------------------------------- */ /* Is this a brand new record? */ /* -------------------------------------------------------------------- */ if( hEntity == psDBF->nRecords ) { flushRecord( psDBF ); psDBF->nRecords++; for( i = 0; i < psDBF->nRecordLength; i++ ) psDBF->pszCurrentRecord[i] = ' '; psDBF->nCurrentRecord = hEntity; } /* -------------------------------------------------------------------- */ /* Is this an existing record, but different than the last one */ /* we accessed? */ /* -------------------------------------------------------------------- */ if( psDBF->nCurrentRecord != hEntity ) { flushRecord( psDBF ); nRecordOffset = psDBF->nRecordLength * hEntity + psDBF->nHeaderLength; safe_fseek( psDBF->fp, nRecordOffset, 0 ); fread( psDBF->pszCurrentRecord, psDBF->nRecordLength, 1, psDBF->fp ); psDBF->nCurrentRecord = hEntity; } pabyRec = (uchar *) psDBF->pszCurrentRecord; /* -------------------------------------------------------------------- */ /* Assign all the record fields. */ /* -------------------------------------------------------------------- */ switch( psDBF->pachFieldType[iField] ) { case 'D': case 'N': case 'F': if( psDBF->panFieldDecimals[iField] == 0 ) { snprintf( szFormat, sizeof(szFormat), "%%%dd", psDBF->panFieldSize[iField] ); snprintf(szSField, sizeof(szSField), szFormat, (int) *((double *) pValue) ); if( (int) strlen(szSField) > psDBF->panFieldSize[iField] ) szSField[psDBF->panFieldSize[iField]] = '\0'; strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]), szSField, strlen(szSField) ); } else { snprintf( szFormat, sizeof(szFormat), "%%%d.%df", psDBF->panFieldSize[iField], psDBF->panFieldDecimals[iField] ); snprintf(szSField, sizeof(szSField), szFormat, *((double *) pValue) ); if( (int) strlen(szSField) > psDBF->panFieldSize[iField] ) szSField[psDBF->panFieldSize[iField]] = '\0'; strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]), szSField, strlen(szSField) ); } break; default: if( (int) strlen((char *) pValue) > psDBF->panFieldSize[iField] ) j = psDBF->panFieldSize[iField]; else j = strlen((char *) pValue); strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]), (char *) pValue, j ); break; } psDBF->bCurrentRecordModified = MS_TRUE; psDBF->bUpdated = MS_TRUE; return( MS_TRUE ); }
static char *msDBFReadAttribute(DBFHandle psDBF, int hEntity, int iField ) { int i; unsigned int nRecordOffset; uchar *pabyRec; char *pReturnField = NULL; /* -------------------------------------------------------------------- */ /* Is the request valid? */ /* -------------------------------------------------------------------- */ if( iField < 0 || iField >= psDBF->nFields ) { msSetError(MS_DBFERR, "Invalid field index %d.", "msDBFReadAttribute()",iField ); return( NULL ); } if( hEntity < 0 || hEntity >= psDBF->nRecords ) { msSetError(MS_DBFERR, "Invalid record number %d.", "msDBFReadAttribute()",hEntity ); return( NULL ); } /* -------------------------------------------------------------------- */ /* Have we read the record? */ /* -------------------------------------------------------------------- */ if( psDBF->nCurrentRecord != hEntity ) { flushRecord( psDBF ); nRecordOffset = psDBF->nRecordLength * hEntity + psDBF->nHeaderLength; safe_fseek( psDBF->fp, nRecordOffset, 0 ); fread( psDBF->pszCurrentRecord, psDBF->nRecordLength, 1, psDBF->fp ); psDBF->nCurrentRecord = hEntity; } pabyRec = (uchar *) psDBF->pszCurrentRecord; /* DEBUG */ /* printf("CurrentRecord(%c):%s\n", psDBF->pachFieldType[iField], pabyRec); */ /* -------------------------------------------------------------------- */ /* Ensure our field buffer is large enough to hold this buffer. */ /* -------------------------------------------------------------------- */ if( psDBF->panFieldSize[iField]+1 > psDBF->nStringFieldLen ) { psDBF->nStringFieldLen = psDBF->panFieldSize[iField]*2 + 10; psDBF->pszStringField = (char *) SfRealloc(psDBF->pszStringField,psDBF->nStringFieldLen); } /* -------------------------------------------------------------------- */ /* Extract the requested field. */ /* -------------------------------------------------------------------- */ strncpy( psDBF->pszStringField,(char *) pabyRec+psDBF->panFieldOffset[iField], psDBF->panFieldSize[iField] ); psDBF->pszStringField[psDBF->panFieldSize[iField]] = '\0'; /* ** Trim trailing blanks (SDL Modification) */ for(i=strlen(psDBF->pszStringField)-1; i>=0; i--) { if(psDBF->pszStringField[i] != ' ') { psDBF->pszStringField[i+1] = '\0'; break; } } if(i == -1) psDBF->pszStringField[0] = '\0'; /* whole string is blank (SDL fix) */ /* ** Trim/skip leading blanks (SDL/DM Modification - only on numeric types) */ if( psDBF->pachFieldType[iField] == 'N' || psDBF->pachFieldType[iField] == 'F' || psDBF->pachFieldType[iField] == 'D' ) { for(i=0; psDBF->pszStringField[i] != '\0' ; i++) { if(psDBF->pszStringField[i] != ' ') break; } pReturnField = psDBF->pszStringField+i; } else pReturnField = psDBF->pszStringField; /* detect null values */ if ( DBFIsValueNULL( pReturnField, psDBF->pachFieldType[iField] ) ) { if (psDBF->pachFieldType[iField] == 'N' || psDBF->pachFieldType[iField] == 'F' || psDBF->pachFieldType[iField] == 'D') pReturnField="0"; } return( pReturnField ); }
void factor_vm::primitive_fseek() { FILE* file = pop_file_handle(); int whence = (int)to_fixnum(ctx->pop()); off_t offset = (off_t)to_signed_8(ctx->pop()); safe_fseek(file, offset, whence); }
static void file_store(void *f, void *data, long length, long offset) { safe_fseek(f, offset, SEEK_SET); safe_fwrite(data, length, 1, f); }
static void file_load(void *f, void *data, long length, long offset) { safe_fseek(f, offset, SEEK_SET); safe_fread(data, length, 1, f); }