summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 6fdaf74)
raw | patch | inline | side by side (parent: 6fdaf74)
author | Ketmar Dark <ketmar@ketmar.no-ip.org> | |
Wed, 6 Sep 2017 23:10:37 +0000 (02:10 +0300) | ||
committer | Ketmar Dark <ketmar@ketmar.no-ip.org> | |
Wed, 6 Sep 2017 23:10:50 +0000 (02:10 +0300) |
src/shared/utils.pas | patch | blob | history | |
src/shared/xdynrec.pas | patch | blob | history |
diff --git a/src/shared/utils.pas b/src/shared/utils.pas
index 43bdc8f015faa59bc00523c1eac0b583ad9c996b..d8b6e85c266dcf6fb2142759b53e8b5e35e913aa 100644 (file)
--- a/src/shared/utils.pas
+++ b/src/shared/utils.pas
procedure clear (); inline;
procedure append (constref it: ItemT); inline;
+ procedure delete (idx: Integer); inline;
+ function remove (idx: Integer): ItemT; inline;
public
property count: Integer read mCount;
procedure TSimpleList.append (constref it: ItemT); inline;
+var
+ newsz: Integer;
begin
- if (mCount = Length(mItems)) then
+ if (mCount >= Length(mItems)) then
begin
- if (mCount = 0) then SetLength(mItems, 128) else SetLength(mItems, mCount*2);
+ newsz := mCount+(mCount div 3)+128;
+ SetLength(mItems, newsz);
end;
mItems[mCount] := it;
Inc(mCount);
end;
+procedure TSimpleList.delete (idx: Integer); inline;
+var
+ f: Integer;
+begin
+ if (idx >= 0) and (idx < mCount) then
+ begin
+ for f := idx+1 to mCount-1 do mItems[f-1] := mItems[f];
+ end;
+end;
+
+
+function TSimpleList.remove (idx: Integer): ItemT; inline;
+var
+ f: Integer;
+begin
+ if (idx >= 0) and (idx < mCount) then
+ begin
+ result := mItems[idx];
+ for f := idx+1 to mCount-1 do mItems[f-1] := mItems[f];
+ end
+ else
+ begin
+ result := Default(ItemT);
+ end;
+end;
+
+
// ////////////////////////////////////////////////////////////////////////// //
var
wc2shitmap: array[0..65535] of AnsiChar;
diff --git a/src/shared/xdynrec.pas b/src/shared/xdynrec.pas
index 6c4e376194b85e63beb7f3572b4cd389a5446e3e..223dc51518c92d89b11e8ad9cb957c89c57ba1aa 100644 (file)
--- a/src/shared/xdynrec.pas
+++ b/src/shared/xdynrec.pas
protected
// returns `true` for duplicate record id
function addListItem (rec: TDynRecord): Boolean; inline;
+ function removeListItem (const aid: AnsiString): TDynRecord; // returns nil or removed record
public
// get string name for the given type
private
mOwner: TDynMapDef;
mId: AnsiString;
- mName: AnsiString;
+ mTypeName: AnsiString;
mSize: Integer;
mFields: TDynFieldList;
{$IF DEFINED(XDYNREC_USE_FIELDHASH)}
function getUserVar (const aname: AnsiString): Variant;
procedure setUserVar (const aname: AnsiString; val: Variant);
+ procedure clearRefRecs (rec: TDynRecord);
+
protected
function findRecordByTypeId (const atypename, aid: AnsiString): TDynRecord;
function findRecordNumByType (const atypename: AnsiString; rc: TDynRecord): Integer;
// only for headers: create new record with the given type
// will return cloned record ready for use, or `nil` on unknown type name
+ // `aid` must not be empty, and must be unique
function newTypedRecord (const atypename, aid: AnsiString): TDynRecord;
+ // remove record with the given type and id
+ // return `true` if record was successfully found and removed
+ // this will do all necessary recref cleanup too
+ // WARNING: not tested yet
+ function removeTypedRecord (const atypename, aid: AnsiString): Boolean;
+
+ //TODO:
+ // [.] API to create triggers
+ // [.] API to properly remove triggers (remove trigdata)
+ // [.] check if `removeTypedRecord()` does the right thing with inline records
+ // [.] for fields: assigning `recref` should remove previously assigned inline record (record without id)
+ // [.] other API i forgot
+
public
// text parser
// `beginEaten`: `true` if "{" was eaten
public
property mapdef: TDynMapDef read mOwner;
property id: AnsiString read mId; // record id in text map
- property typeName: AnsiString read mName; // record type name (like "panel", or "trigger")
+ property typeName: AnsiString read mTypeName; // record type name (like "panel", or "trigger")
property has[const aname: AnsiString]: Boolean read hasByName; // do we have field with the given name?
property count: Integer read getCount; // number of fields in this record
property field[const aname: AnsiString]: TDynField read getFieldByName; default; // get field by name
private
mOwner: TDynMapDef;
mIsEnum: Boolean;
- mName: AnsiString;
+ mTypeName: AnsiString;
mIds: array of AnsiString;
mVals: array of Integer;
mMaxName: AnsiString; // MAX field
public
property mapdef: TDynMapDef read mOwner;
- property typeName: AnsiString read mName; // enum/bitset type name
+ property typeName: AnsiString read mTypeName; // enum/bitset type name
property isEnum: Boolean read mIsEnum; // is this enum? `false` means "bitset"
property has[const aname: AnsiString]: Boolean read hasByName;
property field[const aname: AnsiString]: Integer read getFieldByName;
if not mHasDefault then
begin
if mInternal then exit;
- raise TDynRecException.CreateFmt('field ''%s'' in record ''%s'' of record type ''%s'' is not set', [mName, mOwner.mId, mOwner.mName]);
+ raise TDynRecException.CreateFmt('field ''%s'' in record ''%s'' of record type ''%s'' is not set', [mName, mOwner.mId, mOwner.mTypeName]);
end;
if (mEBS = TEBS.TRec) then mRecRef := mDefRecRef;
mSVal := mDefSVal;
end;
+function TDynField.removeListItem (const aid: AnsiString): TDynRecord;
+var
+ f, idx: Integer;
+begin
+ result := nil;
+ if mRHash.get(aid, idx) then
+ begin
+ assert((idx >= 0) and (idx < mRVal.count));
+ result := mRVal[idx];
+ // fix hash and list
+ for f := idx+1 to mRVal.count-1 do
+ begin
+ if (Length(mRVal[f].mId) > 0) then mRHash.put(mRVal[f].mId, f-1);
+ end;
+ mRHash.del(aid);
+ mRVal.delete(idx);
+ end;
+end;
+
+
class function TDynField.getTypeName (t: TType): AnsiString;
begin
case t of
rec := mOwner;
// find trigger definition
tfld := rec.trigTypeField();
- if (tfld = nil) then raise TDynRecException.CreateFmt('triggerdata value for field ''%s'' in record ''%s'' without TriggerType field', [mName, rec.mName]);
+ if (tfld = nil) then raise TDynRecException.CreateFmt('triggerdata value for field ''%s'' in record ''%s'' without TriggerType field', [mName, rec.mTypeName]);
rc := mOwner.mOwner.trigTypeFor[tfld.mSVal]; // find in mapdef
- if (rc = nil) then raise TDynRecException.CreateFmt('triggerdata definition for field ''%s'' in record ''%s'' with type ''%s'' not found', [mName, rec.mName, tfld.mSVal]);
+ if (rc = nil) then raise TDynRecException.CreateFmt('triggerdata definition for field ''%s'' in record ''%s'' with type ''%s'' not found', [mName, rec.mTypeName, tfld.mSVal]);
rc := rc.clone(mOwner.mHeaderRec);
rc.mHeaderRec := mOwner.mHeaderRec;
// on error, it will be freed by memowner
rec := mOwner;
// find trigger definition
tfld := rec.trigTypeField();
- if (tfld = nil) then raise TDynParseException.CreateFmt(pr, 'triggerdata value for field ''%s'' in record ''%s'' without ''type'' field', [mName, rec.mName]);
+ if (tfld = nil) then raise TDynParseException.CreateFmt(pr, 'triggerdata value for field ''%s'' in record ''%s'' without ''type'' field', [mName, rec.mTypeName]);
rc := mOwner.mOwner.trigTypeFor[tfld.mSVal]; // find in mapdef
- if (rc = nil) then raise TDynParseException.CreateFmt(pr, 'triggerdata definition for field ''%s'' in record ''%s'' with type ''%s'' not found', [mName, rec.mName, tfld.mSVal]);
+ if (rc = nil) then raise TDynParseException.CreateFmt(pr, 'triggerdata definition for field ''%s'' in record ''%s'' with type ''%s'' not found', [mName, rec.mTypeName, tfld.mSVal]);
rc := rc.clone(mOwner.mHeaderRec);
rc.mHeaderRec := mOwner.mHeaderRec;
//writeln(rc.definition);
mDefined := true;
if mOwner.addRecordByType(mEBSTypeName, rc) then
begin
- raise TDynParseException.CreateFmt(pr, 'duplicate record with id ''%s'' for field ''%s'' in record ''%s''', [rc.mId, mName, mOwner.mName]);
+ raise TDynParseException.CreateFmt(pr, 'duplicate record with id ''%s'' for field ''%s'' in record ''%s''', [rc.mId, mName, mOwner.mTypeName]);
end;
pr.eatTT(pr.TTSemi); // hack: allow (but don't require) semicolon after inline records
exit;
begin
if (pr = nil) then raise TDynParseException.Create(pr, 'cannot create record type without type definition');
mId := '';
- mName := '';
+ mTypeName := '';
mSize := 0;
mFields := TDynFieldList.Create();
{$IF DEFINED(XDYNREC_USE_FIELDHASH)}
constructor TDynRecord.Create ();
begin
- mName := '';
+ mTypeName := '';
mSize := 0;
mFields := TDynFieldList.Create();
{$IF DEFINED(XDYNREC_USE_FIELDHASH)}
mRec2Free.Free();
mRec2Free := nil;
end;
- mName := '';
+ mTypeName := '';
for fld in mFields do fld.Free();
mFields.Free();
mFields := nil;
result := TDynRecord.Create();
result.mOwner := mOwner;
result.mId := mId;
- result.mName := mName;
+ result.mTypeName := mTypeName;
result.mSize := mSize;
result.mHeader := mHeader;
result.mBinBlock := mBinBlock;
if not (fld.mEBSType is TDynEBS) then continue;
es := (fld.mEBSType as TDynEBS);
assert(es <> nil);
- if StrEqu(es.mName, 'TriggerType') then begin result := fld; exit; end;
+ if StrEqu(es.mTypeName, 'TriggerType') then begin result := fld; exit; end;
end;
result := nil;
end;
function TDynRecord.newTypedRecord (const atypename, aid: AnsiString): TDynRecord;
var
trc: TDynRecord;
+ fld: TDynField;
begin
if not mHeader then raise TDynRecException.Create('cannot create new records with non-header');
+ if (Length(aid) = 0) then raise TDynRecException.CreateFmt('cannot create new record of type ''%s'' without id', [atypename]);
trc := mapdef.recType[atypename];
if (trc = nil) then begin result := nil; exit; end;
+ // check if aid is unique
+ fld := field[atypename];
+ if (fld <> nil) and (fld.getListItem(aid) <> nil) then raise TDynRecException.CreateFmt('cannot create record of type ''%s'' with duplicate id ''%s''', [atypename, aid]);
result := trc.clone(self);
- result.mId := ''; // for now
- addRecordByType(atypename, result);
result.mId := aid;
+ addRecordByType(atypename, result);
+end;
+
+
+procedure TDynRecord.clearRefRecs (rec: TDynRecord);
+ procedure clearRefs (fld: TDynField);
+ var
+ rc: TDynRecord;
+ begin
+ if (fld = nil) then exit;
+ if (fld.mRecRef = rec) then fld.mRecRef := nil;
+ if (fld.mType = fld.TType.TList) then for rc in fld.mRVal do rc.clearRefRecs(rec);
+ end;
+var
+ fld: TDynField;
+begin
+ if (rec = nil) or (mFields = nil) then exit;
+ for fld in mFields do clearRefs(fld);
+end;
+
+
+// remove record with the given type and id
+// return `true` if record was successfully found and removed
+// this will do all necessary recref cleanup too
+function TDynRecord.removeTypedRecord (const atypename, aid: AnsiString): Boolean;
+var
+ trc, rec: TDynRecord;
+ fld: TDynField;
+ f: Integer;
+ doFree: Boolean = false;
+begin
+ result := false;
+ if not mHeader then raise TDynRecException.Create('cannot remove records with non-header');
+ if (Length(aid) = 0) then exit;
+ trc := mapdef.recType[atypename];
+ if (trc = nil) then exit;
+ fld := field[atypename];
+ if (fld = nil) then exit;
+ rec := fld.removeListItem(aid);
+ if (rec = nil) then exit;
+ clearRefRecs(rec);
+ for f := 0 to mRec2Free.count-1 do
+ begin
+ if (mRec2Free[f] = rec) then
+ begin
+ mRec2Free[f] := nil;
+ doFree := true;
+ end;
+ end;
+ if doFree then rec.Free();
end;
while pr.eatTT(pr.TTComma) do begin end;
if pr.eatDelim(')') then break;
tdn := pr.expectId();
- if isForTrig[tdn] then raise TDynParseException.CreateFmt(pr, 'duplicate trigdata ''%s'' trigtype ''%s''', [mName, tdn]);
+ if isForTrig[tdn] then raise TDynParseException.CreateFmt(pr, 'duplicate trigdata ''%s'' trigtype ''%s''', [mTypeName, tdn]);
SetLength(mTrigTypes, Length(mTrigTypes)+1);
mTrigTypes[High(mTrigTypes)] := tdn;
end;
SetLength(mTrigTypes, 1);
mTrigTypes[0] := tdn;
end;
- mName := 'TriggerData';
+ mTypeName := 'TriggerData';
end
else
begin
- mName := pr.expectStrOrId();
+ mTypeName := pr.expectStrOrId();
while (pr.tokType <> pr.TTBegin) do
begin
if pr.eatId('header') then begin mHeader := true; continue; end;
if pr.eatId('size') then
begin
- if (mSize > 0) then raise TDynParseException.CreateFmt(pr, 'duplicate `size` in record ''%s''', [mName]);
+ if (mSize > 0) then raise TDynParseException.CreateFmt(pr, 'duplicate `size` in record ''%s''', [mTypeName]);
mSize := pr.expectInt();
- if (mSize < 1) then raise TDynParseException.CreateFmt(pr, 'invalid record ''%s'' size: %d', [mName, mSize]);
+ if (mSize < 1) then raise TDynParseException.CreateFmt(pr, 'invalid record ''%s'' size: %d', [mTypeName, mSize]);
pr.expectId('bytes');
continue;
end;
if pr.eatId('binblock') then
begin
- if (mBinBlock >= 0) then raise TDynParseException.CreateFmt(pr, 'duplicate `binblock` in record ''%s''', [mName]);
+ if (mBinBlock >= 0) then raise TDynParseException.CreateFmt(pr, 'duplicate `binblock` in record ''%s''', [mTypeName]);
mBinBlock := pr.expectInt();
- if (mBinBlock < 1) then raise TDynParseException.CreateFmt(pr, 'invalid record ''%s'' binblock: %d', [mName, mBinBlock]);
+ if (mBinBlock < 1) then raise TDynParseException.CreateFmt(pr, 'invalid record ''%s'' binblock: %d', [mTypeName, mBinBlock]);
continue;
end;
end;
else
begin
// record
- result := quoteStr(mName);
+ result := quoteStr(mTypeName);
if (mSize >= 0) then result += Format(' size %d bytes', [mSize]);
if mHeader then result += ' header';
end;
rt := findRecordByTypeId(fld.mEBSTypeName, fld.mRecRefId);
if (rt = nil) then
begin
- e_LogWritefln('record of type ''%s'' with id ''%s'' links to inexistant record of type ''%s'' with id ''%s''', [rec.mName, rec.mId, fld.mEBSTypeName, fld.mRecRefId], MSG_WARNING);
+ e_LogWritefln('record of type ''%s'' with id ''%s'' links to inexistant record of type ''%s'' with id ''%s''', [rec.mTypeName, rec.mId, fld.mEBSTypeName, fld.mRecRefId], MSG_WARNING);
//raise TDynRecException.CreateFmt('record of type ''%s'' with id ''%s'' links to inexistant record of type ''%s'' with id ''%s''', [rec.mName, rec.mId, fld.mEBSTypeName, fld.mRecRefId]);
end;
//writeln(' ', rec.mName, '.', rec.mId, ':', fld.mName, ' -> ', rt.mName, '.', rt.mId, ' (', fld.mEBSTypeName, '.', fld.mRecRefId, ')');
else
begin
// create list for this type
- fld := TDynField.Create(rec.mName, TDynField.TType.TList);
+ fld := TDynField.Create(rec.mTypeName, TDynField.TType.TList);
fld.mOwner := self;
addField(fld);
if (bsize > 0) then
rec := rect.clone(self);
rec.mHeaderRec := self;
rec.parseBinValue(mst);
- rec.mId := Format('%s%d', [rec.mName, f]);
+ rec.mId := Format('%s%d', [rec.mTypeName, f]);
fld.addListItem(rec);
//writeln('parsed ''', rec.mId, '''...');
end;
end;
// read fields
- if StrEqu(mName, 'TriggerData') then mSize := Integer(st.size-st.position);
- if (mSize < 1) then raise TDynRecException.CreateFmt('cannot read record of type ''%s'' with unknown size', [mName]);
+ if StrEqu(mTypeName, 'TriggerData') then mSize := Integer(st.size-st.position);
+ if (mSize < 1) then raise TDynRecException.CreateFmt('cannot read record of type ''%s'' with unknown size', [mTypeName]);
GetMem(buf, mSize);
st.ReadBuffer(buf^, mSize);
for fld in mFields do
begin
if putHeader then
begin
- wr.put(mName);
+ wr.put(mTypeName);
if (Length(mId) > 0) then begin wr.put(' '); wr.put(mId); end;
wr.put(' ');
end;
if (rt = nil) then
begin
//e_LogWritefln('record of type ''%s'' with id ''%s'' links to inexistant record of type ''%s'' with id ''%s''', [rec.mName, rec.mId, fld.mEBSTypeName, fld.mRecRefId], MSG_WARNING);
- raise TDynParseException.CreateFmt(pr, 'record of type ''%s'' with id ''%s'' links to inexistant record of type ''%s'' with id ''%s''', [rec.mName, rec.mId, fld.mEBSTypeName, fld.mRecRefId]);
+ raise TDynParseException.CreateFmt(pr, 'record of type ''%s'' with id ''%s'' links to inexistant record of type ''%s'' with id ''%s''', [rec.mTypeName, rec.mId, fld.mEBSTypeName, fld.mRecRefId]);
end;
//writeln(' ', rec.mName, '.', rec.mId, ':', fld.mName, ' -> ', rt.mName, '.', rt.mId, ' (', fld.mEBSTypeName, '.', fld.mRecRefId, ')');
fld.mRecRefId := '';
end;
begin
- if (mOwner = nil) then raise TDynParseException.CreateFmt(pr, 'can''t parse record ''%s'' value without owner', [mName]);
+ if (mOwner = nil) then raise TDynParseException.CreateFmt(pr, 'can''t parse record ''%s'' value without owner', [mTypeName]);
{$IF DEFINED(D2D_DYNREC_PROFILER)}stall := curTimeMicro();{$ENDIF}
pr.skipToken();
rec.parseValue(pr);
{$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
- addRecordByType(rec.mName, rec);
+ addRecordByType(rec.mTypeName, rec);
{$IF DEFINED(D2D_DYNREC_PROFILER)}profAddRecByType := curTimeMicro()-stt;{$ENDIF}
continue;
end;
if (fld <> nil) then
begin
//writeln('2: <', mName, '.', pr.tokStr, '>');
- if fld.defined then raise TDynParseException.CreateFmt(pr, 'duplicate field ''%s'' in record ''%s''', [fld.mName, mName]);
- if fld.internal then raise TDynParseException.CreateFmt(pr, 'internal field ''%s'' in record ''%s''', [fld.mName, mName]);
+ if fld.defined then raise TDynParseException.CreateFmt(pr, 'duplicate field ''%s'' in record ''%s''', [fld.mName, mTypeName]);
+ if fld.internal then raise TDynParseException.CreateFmt(pr, 'internal field ''%s'' in record ''%s''', [fld.mName, mTypeName]);
pr.skipToken(); // skip field name
//writeln('3: <', mName, '.', pr.tokStr, '>:', pr.tokType);
{$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
end;
// something is wrong
- raise TDynParseException.CreateFmt(pr, 'unknown field ''%s'' in record ''%s''', [pr.tokStr, mName]);
+ raise TDynParseException.CreateFmt(pr, 'unknown field ''%s'' in record ''%s''', [pr.tokStr, mTypeName]);
end;
pr.expectTT(pr.TTEnd);
procedure TDynEBS.cleanup ();
begin
mIsEnum := false;
- mName := '';
+ mTypeName := '';
mIds := nil;
mVals := nil;
mMaxName := '';
f, cv: Integer;
begin
if mIsEnum then result :='enum ' else result := 'bitset ';
- result += mName;
+ result += mTypeName;
result += ' {'#10;
// fields
if mIsEnum then cv := 0 else cv := 1;
var
f: Integer;
begin
- result := '// '+mName+#10'const'#10;
+ result := '// '+mTypeName+#10'const'#10;
// fields
for f := 0 to High(mIds) do
begin
if pr.eatId('enum') then mIsEnum := true
else if pr.eatId('bitset') then mIsEnum := false
else pr.expectId('enum');
- mName := pr.expectId();
+ mTypeName := pr.expectId();
mMaxVal := Integer($80000000);
if mIsEnum then cv := 0 else cv := 1;
pr.expectTT(pr.TTBegin);
idname := pr.expectId();
for f := 0 to High(mIds) do
begin
- if StrEqu(mIds[f], idname) then raise TDynParseException.CreateFmt(pr, 'duplicate field ''%s'' in enum/bitset ''%s''', [idname, mName]);
+ if StrEqu(mIds[f], idname) then raise TDynParseException.CreateFmt(pr, 'duplicate field ''%s'' in enum/bitset ''%s''', [idname, mTypeName]);
end;
- if StrEqu(mMaxName, idname) then raise TDynParseException.CreateFmt(pr, 'duplicate field ''%s'' in enum/bitset ''%s''', [idname, mName]);
+ if StrEqu(mMaxName, idname) then raise TDynParseException.CreateFmt(pr, 'duplicate field ''%s'' in enum/bitset ''%s''', [idname, mTypeName]);
skipAdd := false;
hasV := false;
v := cv;
begin
if pr.eatId('MAX') then
begin
- if (Length(mMaxName) > 0) then raise TDynParseException.CreateFmt(pr, 'duplicate max field ''%s'' in enum/bitset ''%s''', [idname, mName]);
+ if (Length(mMaxName) > 0) then raise TDynParseException.CreateFmt(pr, 'duplicate max field ''%s'' in enum/bitset ''%s''', [idname, mTypeName]);
mMaxName := idname;
skipAdd := true;
end