]>
Commit | Line | Data |
---|---|---|
cecfc5e7 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: No names yet. | |
3 | // Purpose: Contrib. demo | |
4 | // Author: Aleksandras Gluchovas | |
5 | // Modified by: | |
6 | // Created: 27/12/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Aleskandars Gluchovas | |
fa1af598 | 9 | // Licence: wxWindows licence |
cecfc5e7 VZ |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
ad053960 | 13 | # pragma implementation "ifcontext.h" |
cecfc5e7 VZ |
14 | #endif |
15 | ||
16 | // For compilers that support precompilation, includes "wx/wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/wx.h" | |
25 | #endif | |
26 | ||
d5939a20 | 27 | #ifndef __DARWIN__ |
ad053960 GD |
28 | # include <malloc.h> |
29 | #endif | |
cecfc5e7 VZ |
30 | #include <stdio.h> |
31 | ||
32 | #include "ifcontext.h" | |
33 | ||
34 | /***** Implementation for class spInterFileContext *****/ | |
35 | ||
36 | size_t spInterFileContext::GetFileNo( const string& fname ) | |
37 | { | |
fa1af598 WS |
38 | for ( size_t i = 0; i != mFiles.size(); ++i ) |
39 | { | |
40 | if ( fname == mFiles[i] ) | |
3a87625f | 41 | return i; |
fa1af598 | 42 | } |
cecfc5e7 | 43 | |
3a87625f | 44 | wxFAIL_MSG("File not found in array in function spInterFileContext::GetFileNo()"); |
fa1af598 WS |
45 | |
46 | return 0; | |
cecfc5e7 VZ |
47 | } |
48 | ||
49 | size_t spInterFileContext::GetFileNoOfContext( spContext& ctx ) | |
50 | { | |
fa1af598 | 51 | spContext* pCtx = ctx.GetEnclosingContext( SP_CTX_FILE ); |
cecfc5e7 | 52 | |
fa1af598 WS |
53 | // DBG:: outer-file context should be present |
54 | wxASSERT( pCtx && pCtx->GetType() == SP_CTX_FILE ); | |
cecfc5e7 | 55 | |
fa1af598 | 56 | return GetFileNo( ((spFile*)pCtx)->mFileName ); |
cecfc5e7 VZ |
57 | } |
58 | ||
59 | /*** public interface ***/ | |
60 | ||
61 | spInterFileContext::spInterFileContext() | |
62 | {} | |
63 | ||
64 | spInterFileContext::~spInterFileContext() | |
65 | {} | |
66 | ||
67 | void spInterFileContext::AddFile( const string& fname, const string& content ) | |
68 | { | |
fa1af598 WS |
69 | mFiles.push_back( fname ); |
70 | mContents.push_back( content ); | |
cecfc5e7 VZ |
71 | } |
72 | ||
73 | void spInterFileContext::RemoveContext( spContext& ctx ) | |
74 | { | |
fa1af598 | 75 | wxASSERT( ctx.PositionIsKnown() ); // DBG:: should be checked by-user code |
cecfc5e7 | 76 | |
fa1af598 | 77 | size_t fNo = GetFileNoOfContext( ctx ); |
cecfc5e7 | 78 | |
fa1af598 | 79 | mDeletionMarks.push_back( spBookmark( ctx.mSrcOffset, ctx.mContextLength, fNo ) ); |
cecfc5e7 VZ |
80 | } |
81 | ||
82 | void spInterFileContext::InsertBookmarkSorted( BookmarkListT& lst, spBookmark& mark ) | |
83 | { | |
fa1af598 | 84 | for( size_t i = 0; i != lst.size(); ++i ) |
cecfc5e7 | 85 | |
fa1af598 WS |
86 | if ( lst[i].mFrom > mark.mFrom ) |
87 | { | |
88 | lst.insert( &lst[i], mark ); | |
89 | return; | |
90 | } | |
91 | ||
92 | lst.push_back( mark ); | |
93 | } | |
cecfc5e7 VZ |
94 | |
95 | void spInterFileContext::DoAppendSourceFragment( string& source, | |
fa1af598 WS |
96 | string& result, |
97 | size_t pos, size_t len ) | |
cecfc5e7 | 98 | { |
fa1af598 | 99 | mFiltered.erase( mFiltered.begin(), mFiltered.end() ); |
cecfc5e7 | 100 | |
fa1af598 WS |
101 | size_t i; |
102 | ||
103 | for( i = 0; i != mDeletionMarks.size(); ++i ) | |
104 | { | |
105 | spBookmark& mark = mDeletionMarks[i]; | |
cecfc5e7 | 106 | |
fa1af598 WS |
107 | if ( mark.mFileNo == mCurFileNo && |
108 | mark.mFrom >= pos && mark.mFrom < pos + len ) | |
cecfc5e7 | 109 | |
fa1af598 WS |
110 | InsertBookmarkSorted( mFiltered, mark ); |
111 | } | |
cecfc5e7 | 112 | |
fa1af598 | 113 | size_t cur = pos; |
cecfc5e7 | 114 | |
fa1af598 WS |
115 | for( i = 0; i != mFiltered.size(); ++ i ) |
116 | { | |
117 | spBookmark& mark = mFiltered[i]; | |
cecfc5e7 | 118 | |
fa1af598 | 119 | result.append( source, cur, ( (size_t)mark.mFrom - cur ) ); |
cecfc5e7 | 120 | |
fa1af598 | 121 | cur = size_t( mark.mFrom + mark.mLen ); |
cecfc5e7 | 122 | |
fa1af598 WS |
123 | if ( cur >= pos + len ) // check if we've overstepped the current source-fragment |
124 | { | |
125 | // wxASSERT(0); // DBG:: with current imp. this should not happen | |
126 | wxFAIL_MSG("Overstepped the current source fragment in function\nspInterFileContext::DoAppendSourceFragment()"); | |
127 | cur = pos + len; break; | |
128 | } | |
129 | } | |
130 | ||
131 | result.append( source, cur, ( pos + len ) - cur ); | |
cecfc5e7 VZ |
132 | } |
133 | ||
134 | void spInterFileContext::GenerateContextBody( spContext& ctx, | |
fa1af598 WS |
135 | string& source, |
136 | string& result, | |
137 | size_t& lastSavedPos, | |
138 | size_t& lastKnownPos ) | |
cecfc5e7 | 139 | { |
fa1af598 WS |
140 | if ( ctx.PositionIsKnown() ) |
141 | lastKnownPos = ctx.mSrcOffset; | |
cecfc5e7 | 142 | |
fa1af598 WS |
143 | if ( ctx.IsVirtualContext() ) |
144 | { | |
145 | // add fragment accumulated before this context | |
cecfc5e7 | 146 | |
fa1af598 WS |
147 | DoAppendSourceFragment( source, result, |
148 | size_t(lastSavedPos), | |
149 | size_t(lastKnownPos - lastSavedPos) ); | |
cecfc5e7 | 150 | |
fa1af598 | 151 | // add context body |
cecfc5e7 | 152 | |
fa1af598 | 153 | result += ctx.GetVirtualContextBody(); |
cecfc5e7 | 154 | |
fa1af598 | 155 | lastSavedPos = lastKnownPos; |
cecfc5e7 | 156 | |
fa1af598 WS |
157 | if ( ctx.PositionIsKnown() ) |
158 | { | |
159 | if ( ctx.VitualContextHasChildren() ) | |
160 | { | |
161 | lastKnownPos = ctx.mSrcOffset + ctx.mHeaderLength; | |
cecfc5e7 | 162 | |
fa1af598 WS |
163 | lastSavedPos = lastKnownPos; |
164 | } | |
165 | else | |
166 | { | |
167 | lastKnownPos = ctx.mSrcOffset + ctx.mContextLength; | |
cecfc5e7 | 168 | |
fa1af598 | 169 | lastSavedPos = lastKnownPos; |
cecfc5e7 | 170 | |
fa1af598 WS |
171 | return; // have not children |
172 | } | |
173 | } | |
174 | } | |
cecfc5e7 | 175 | |
fa1af598 | 176 | MMemberListT& lst = ctx.GetMembers(); |
cecfc5e7 | 177 | |
fa1af598 WS |
178 | for( size_t i = 0; i != lst.size(); ++i ) |
179 | { | |
180 | GenerateContextBody( *lst[i], source, result, lastSavedPos, lastKnownPos ); | |
181 | } | |
cecfc5e7 | 182 | |
fa1af598 WS |
183 | if ( ctx.IsVirtualContext() ) |
184 | { | |
185 | if ( ctx.VitualContextHasChildren() && !ctx.GetFooterOfVirtualContextBody().empty() ) | |
186 | { | |
187 | // append the reminder space after children of the context | |
cecfc5e7 | 188 | |
fa1af598 WS |
189 | DoAppendSourceFragment( result, source, |
190 | size_t(lastSavedPos), | |
191 | size_t(lastKnownPos - lastSavedPos) ); | |
cecfc5e7 | 192 | |
fa1af598 WS |
193 | // add footer |
194 | result += ctx.GetFooterOfVirtualContextBody(); | |
cecfc5e7 | 195 | |
fa1af598 | 196 | lastKnownPos = ctx.mSrcOffset + ctx.mContextLength; |
cecfc5e7 | 197 | |
fa1af598 WS |
198 | lastSavedPos = lastKnownPos; |
199 | } | |
200 | } | |
cecfc5e7 | 201 | |
fa1af598 | 202 | if ( ctx.PositionIsKnown() ) |
cecfc5e7 | 203 | |
fa1af598 | 204 | lastKnownPos = ctx.mSrcOffset + ctx.mContextLength; |
cecfc5e7 VZ |
205 | } |
206 | ||
207 | void spInterFileContext::GenrateContents() | |
208 | { | |
fa1af598 | 209 | MMemberListT& lst = GetMembers(); |
cecfc5e7 | 210 | |
fa1af598 WS |
211 | for( size_t f = 0; f != lst.size(); ++f ) |
212 | { | |
213 | string& fname = ((spFile*)lst[f])->mFileName; | |
cecfc5e7 | 214 | |
fa1af598 | 215 | size_t fileNo = GetFileNo( fname ); |
cecfc5e7 | 216 | |
fa1af598 | 217 | string& source = mContents[ fileNo ]; |
cecfc5e7 | 218 | |
fa1af598 | 219 | string result; |
cecfc5e7 | 220 | |
fa1af598 WS |
221 | size_t lastKnownPos = 0, // the begining of the file is always "known" |
222 | lastSavedPos = 0; | |
cecfc5e7 | 223 | |
fa1af598 | 224 | mCurFileNo = fileNo; |
cecfc5e7 | 225 | |
fa1af598 | 226 | GenerateContextBody( *lst[f], source, result, lastSavedPos, lastKnownPos ); |
cecfc5e7 | 227 | |
fa1af598 | 228 | // the end of file is always known |
cecfc5e7 | 229 | |
fa1af598 | 230 | lastKnownPos = mContents[ fileNo ].length(); |
cecfc5e7 | 231 | |
fa1af598 | 232 | // append the reminder |
cecfc5e7 | 233 | |
fa1af598 WS |
234 | DoAppendSourceFragment( source, result, |
235 | size_t(lastSavedPos), | |
236 | size_t(lastKnownPos - lastSavedPos) ); | |
cecfc5e7 | 237 | |
fa1af598 | 238 | // replace original contnet with newly generated one |
cecfc5e7 | 239 | |
fa1af598 WS |
240 | mContents[ fileNo ] = result; |
241 | } | |
cecfc5e7 VZ |
242 | } |
243 | ||
244 | void spInterFileContext::ParseContents( SourceParserPlugin* pPlugin ) | |
245 | { | |
fa1af598 | 246 | mDeletionMarks.erase( mDeletionMarks.begin(), mDeletionMarks.end() ); |
cecfc5e7 | 247 | |
fa1af598 | 248 | RemoveChildren(); // clean up top-level context |
cecfc5e7 | 249 | |
fa1af598 | 250 | mParser.SetPlugin( pPlugin ); |
cecfc5e7 | 251 | |
fa1af598 WS |
252 | for( size_t i = 0; i != mFiles.size(); ++i ) |
253 | { | |
254 | char* s = (char*)(mContents[i].c_str()); | |
cecfc5e7 | 255 | |
fa1af598 | 256 | spFile* pFCtx = mParser.Parse( s, s + mContents[i].length() ); |
cecfc5e7 | 257 | |
fa1af598 | 258 | pFCtx->mFileName = mFiles[i]; |
cecfc5e7 | 259 | |
fa1af598 WS |
260 | AddMember( pFCtx ); |
261 | } | |
cecfc5e7 VZ |
262 | } |
263 | ||
264 | void spInterFileContext::WriteToFiles() | |
265 | { | |
fa1af598 WS |
266 | for( size_t i = 0; i != mFiles.size(); ++i ) |
267 | { | |
268 | FILE* fp = fopen( mFiles[i].c_str(), "w+t" ); | |
cecfc5e7 | 269 | |
fa1af598 WS |
270 | if ( fp != NULL ) |
271 | { | |
272 | fwrite( mContents[i].c_str(), sizeof(char), mContents[i].length(), fp ); | |
cecfc5e7 | 273 | |
fa1af598 WS |
274 | fclose( fp ); |
275 | } | |
276 | } | |
cecfc5e7 VZ |
277 | } |
278 | ||
fa1af598 | 279 | wxString spInterFileContext::GetBody( spContext* pCtx ) |
cecfc5e7 | 280 | { |
fa1af598 | 281 | wxASSERT( pCtx->PositionIsKnown() ); // DBG:: should be checked by-user code |
cecfc5e7 | 282 | |
fa1af598 | 283 | wxString& source = mContents[ GetFileNoOfContext( *pCtx ) ]; |
cecfc5e7 | 284 | |
fa1af598 | 285 | return wxString( source.c_str() + pCtx->mSrcOffset, pCtx->mContextLength ); |
cecfc5e7 VZ |
286 | } |
287 | ||
fa1af598 | 288 | wxString spInterFileContext::GetHeader( spContext* pCtx ) |
cecfc5e7 | 289 | { |
fa1af598 | 290 | wxASSERT( pCtx->PositionIsKnown() ); // DBG:: should be checked by-user code |
cecfc5e7 | 291 | |
fa1af598 | 292 | wxASSERT( pCtx->mHeaderLength != -1 ); // DBG:: -/- |
cecfc5e7 | 293 | |
fa1af598 | 294 | wxString& source = mContents[ GetFileNoOfContext( *pCtx ) ]; |
cecfc5e7 | 295 | |
fa1af598 | 296 | return wxString( source.c_str() + pCtx->mSrcOffset, pCtx->mHeaderLength ); |
cecfc5e7 | 297 | } |