123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- #include <OINFO.h>
- #include <ODYNARRB.h>
- #define EMPTY_ROOM_ALLOC_STEP 5
- DynArrayB::DynArrayB(int eleSize,int blockNum,int reuseIntervalDays) : DynArray(eleSize, blockNum)
- {
- empty_room_array = NULL;
- empty_room_num = 0;
- empty_room_count = 0;
- reuse_interval_days = reuseIntervalDays;
- }
- DynArrayB::~DynArrayB()
- {
- if( empty_room_array )
- mem_del( empty_room_array );
- }
- void DynArrayB::linkin(void* ent)
- {
-
- int reusedFlag=0;
- if( empty_room_count > 0 )
- {
- if( reuse_interval_days )
- {
-
- if( info.game_date >= empty_room_array[0].deleted_game_date + reuse_interval_days )
- {
- cur_pos = empty_room_array[0].recno;
- memmove( empty_room_array, empty_room_array+1, sizeof(empty_room_array[0]) * (empty_room_count-1) );
- empty_room_count--;
- reusedFlag = 1;
- }
- }
- else
- {
-
- cur_pos = empty_room_array[empty_room_count-1].recno;
- empty_room_count--;
- reusedFlag = 1;
- }
- }
- if( !reusedFlag )
- {
- last_ele++;
- cur_pos=last_ele;
- }
-
- if ( last_ele > ele_num )
- resize( ele_num + block_num );
- if ( ent )
- memcpy(body_buf+(cur_pos-1)*ele_size, ent, ele_size );
- else
- *(body_buf+(cur_pos-1)*ele_size) = NULL;
- }
- void DynArrayB::linkout(int delPos)
- {
- if( delPos < 0 )
- delPos = cur_pos;
- if( delPos == 0 || delPos > last_ele )
- return;
-
- if( ++empty_room_count > empty_room_num )
- {
- empty_room_array = (EmptyRoom*) mem_resize( empty_room_array,
- (empty_room_num+EMPTY_ROOM_ALLOC_STEP) * sizeof(*empty_room_array) );
- empty_room_num += EMPTY_ROOM_ALLOC_STEP;
- }
- empty_room_array[empty_room_count-1].recno = delPos;
- empty_room_array[empty_room_count-1].deleted_game_date = info.game_date;
- memset( body_buf+(delPos-1)*ele_size, 0, ele_size );
- }
- int DynArrayB::write_file(File* filePtr)
- {
- if( !filePtr->file_write( this, sizeof(DynArray) ) )
- return 0;
-
- if( last_ele > 0 )
- {
- if( !filePtr->file_write( body_buf, ele_size*last_ele ) )
- return 0;
- }
-
- write_empty_room(filePtr);
- return 1;
- }
- int DynArrayB::read_file(File* filePtr)
- {
- char* bodyBuf = body_buf;
- if( !filePtr->file_read( this, sizeof(DynArray) ) )
- return 0;
-
- body_buf = mem_resize( bodyBuf, ele_size*ele_num );
- if( last_ele > 0 )
- {
- if( !filePtr->file_read( body_buf, ele_size*last_ele ) )
- return 0;
- }
-
- read_empty_room(filePtr);
-
- start();
- return 1;
- }
- int DynArrayB::write_empty_room(File* filePtr)
- {
- filePtr->file_put_short( empty_room_count );
-
- if( empty_room_count > 0 )
- {
- if( !filePtr->file_write( empty_room_array,
- sizeof(EmptyRoom) * empty_room_count ) )
- {
- return 0;
- }
- }
- return 1;
- }
- int DynArrayB::read_empty_room(File* filePtr)
- {
- empty_room_num = empty_room_count = filePtr->file_get_short();
-
- if( empty_room_count > 0 )
- {
- empty_room_array = (EmptyRoom*) mem_resize( empty_room_array,
- sizeof(EmptyRoom) * empty_room_count );
- if( !filePtr->file_read( empty_room_array,
- sizeof(*empty_room_array) * empty_room_count ) )
- {
- return 0;
- }
- }
- else
- {
- if( empty_room_array )
- {
- mem_del( empty_room_array );
- empty_room_array = NULL;
- }
- }
-
- return 1;
- }
- int DynArrayB::packed_recno(int recNo)
- {
- int i, packedRecno = recNo;
- for( i=0 ; i<empty_room_count ; i++ )
- {
- if( empty_room_array[i].recno < recNo )
- packedRecno--;
- }
- return packedRecno;
- }
- void DynArrayB::zap()
- {
- DynArray::zap();
- empty_room_count=0;
- }
- int DynArrayB::write_ptr_array(File* filePtr, int objectSize)
- {
- int i;
- char* elePtr;
- filePtr->file_put_short( size() );
- for( i=1; i<=size() ; i++ )
- {
- elePtr = (char*) get_ptr(i);
-
- if( !elePtr )
- {
- filePtr->file_put_short(0);
- }
- else
- {
- filePtr->file_put_short(1);
- if( !filePtr->file_write(elePtr, objectSize) )
- return 0;
- }
- }
-
- write_empty_room(filePtr);
- return 1;
- }
- int DynArrayB::read_ptr_array(File* filePtr, int objectSize, CreateEleFP createEleFunc)
- {
- int i;
- char* elePtr;
- int eleCount = filePtr->file_get_short();
- for( i=1 ; i<=eleCount ; i++ )
- {
- if( filePtr->file_get_short()==0 )
- {
- add_blank(1);
- }
- else
- {
- elePtr = (*createEleFunc)();
- if( !filePtr->file_read(elePtr, objectSize) )
- return 0;
- }
- }
-
-
-
- for( i=size() ; i>0 ; i-- )
- {
- DynArrayB::go(i);
- if( get_ptr() == NULL )
- linkout();
- }
-
- read_empty_room(filePtr);
- return 1;
- }
|