Debian Codegraph @

components/chsdet/src/nsCodingStateMachine.pas

raw
1 // +----------------------------------------------------------------------+
2 // | chsdet - Charset Detector Library |
3 // +----------------------------------------------------------------------+
4 // | Copyright (C) 2006, Nick Yakowlew http://chsdet.sourceforge.net |
5 // +----------------------------------------------------------------------+
6 // | Based on Mozilla sources http://www.mozilla.org/projects/intl/ |
7 // +----------------------------------------------------------------------+
8 // | This library is free software; you can redistribute it and/or modify |
9 // | it under the terms of the GNU General Public License as published by |
10 // | the Free Software Foundation; either version 2 of the License, or |
11 // | (at your option) any later version. |
12 // | This library is distributed in the hope that it will be useful |
13 // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
15 // | See the GNU Lesser General Public License for more details. |
16 // | http://www.opensource.org/licenses/lgpl-license.php |
17 // +----------------------------------------------------------------------+
18 //
19 // $Id: nsCodingStateMachine.pas,v 1.4 2013/04/23 19:47:10 ya_nick Exp $
20
21 unit nsCodingStateMachine;
22
23 {$R-}
24
25 interface
26
27 uses
28 nsCore;
29
30 type
31 nsSMState = (
32 eStart = 0,
33 eError = 1,
34 eItsMe = 2
35 );
36
37 (*state machine model*)
38 type
39 SMModel = record
40 classTable: Pointer; //nsPkgInt;
41 classFactor: uInt32;
42 stateTable: Pointer; //nsPkgInt;
43 charLenTable: Pointer; // aByteArray; // array of byte; // puInt32;
44 CharsetID: eInternalCharsetID;
45 end;
46 pSMModel = ^SMModel;
47
48 type
49 TnsCodingStateMachine = class (TObject)
50 protected
51 mCurrentState: nsSMState;
52 mCurrentCharLen: uInt32;
53 mCurrentBytePos: uInt32;
54 mModel: SMModel;
55
56 public
57 Enabled: Boolean;
58 constructor Create(sm: SMModel);
59 destructor Destroy; override;
60 function NextState(c: AnsiChar): nsSMState;
61 function GetCurrentCharLen: uInt32;
62 procedure Reset;
63 function GetCharsetID: eInternalCharsetID;
64
65 property LangModel: SMModel read mModel;
66 end;
67
68 implementation
69
70 { TnsCodingStateMachine }
71
72 constructor TnsCodingStateMachine.Create(sm: SMModel);
73 begin
74 mCurrentState:= eStart;
75
76 mModel := sm;
77 Enabled := true;
78 end;
79
80 destructor TnsCodingStateMachine.Destroy;
81 begin
82 mModel.classTable := nil;
83 mModel.stateTable := nil;
84 mModel.charLenTable := nil;
85 inherited;
86 end;
87
88 function TnsCodingStateMachine.NextState(c: AnsiChar): nsSMState;
89 var
90 byteCls: uInt32;
91 begin
92 if not Enabled then
93 begin
94 Result := eError;
95 exit;
96 end;
97 (*for each byte we get its class , if it is first byte, we also get byte length*)
98 byteCls := aByteArray(mModel.classTable)[integer(c)];
99 if mCurrentState = eStart then
100 begin
101 mCurrentBytePos := 0;
102 mCurrentCharLen := aByteArray(mModel.charLenTable)[byteCls];
103 end;
104 (*from byte's class and stateTable, we get its next state*)
105 mCurrentState := nsSMState(aByteArray(mModel.stateTable)[cardinal(mCurrentState) * mModel.classFactor + byteCls]);
106 inc(mCurrentBytePos);
107
108 //if mCurrentBytePos > mCurrentCharLen then
109 // mCurrentState := eError;
110
111 Result:= mCurrentState;
112 end;
113
114 function TnsCodingStateMachine.GetCurrentCharLen: uInt32;
115 begin
116 Result:= mCurrentCharLen;
117 end;
118
119 procedure TnsCodingStateMachine.Reset;
120 begin
121 mCurrentState:= eStart;
122 end;
123
124 function TnsCodingStateMachine.GetCharsetID: eInternalCharsetID;
125 begin
126 Result:= mModel.CharsetID;
127 end;
128
129 end.