]>
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 | ||
60ec1c87 | 36 | size_t spInterFileContext::GetFileNo( const wxString& fname ) |
cecfc5e7 | 37 | { |
60ec1c87 | 38 | for ( size_t i = 0; i != m_Files.size(); ++i ) |
fa1af598 | 39 | { |
60ec1c87 | 40 | if ( fname == m_Files[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 | 53 | // DBG:: outer-file context should be present |
60ec1c87 | 54 | wxASSERT( pCtx && pCtx->GetType() == SP_CTX_FILE ); |
cecfc5e7 | 55 | |
60ec1c87 | 56 | return GetFileNo( ((spFile*)pCtx)->m_FileName ); |
cecfc5e7 VZ |
57 | } |
58 | ||
59 | /*** public interface ***/ | |
60 | ||
61 | spInterFileContext::spInterFileContext() | |
62 | {} | |
63 | ||
64 | spInterFileContext::~spInterFileContext() | |
65 | {} | |
66 | ||
60ec1c87 | 67 | void spInterFileContext::AddFile( const wxString& fname, const wxString& content ) |
cecfc5e7 | 68 | { |
60ec1c87 WS |
69 | m_Files.push_back( fname ); |
70 | m_Contents.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 | 94 | |
7942245d WS |
95 | void spInterFileContext::DoAppendSourceFragment( wxString& source, |
96 | wxString& result, | |
fa1af598 | 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 | |
60ec1c87 | 107 | if ( mark.mFileNo == mCurFileNo && |
fa1af598 | 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 | ||
60ec1c87 | 134 | void spInterFileContext::GenerateContextBody( spContext& ctx, |
7942245d WS |
135 | wxString& source, |
136 | wxString& result, | |
fa1af598 WS |
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 | 147 | DoAppendSourceFragment( source, result, |
60ec1c87 | 148 | size_t(lastSavedPos), |
fa1af598 | 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 | 189 | DoAppendSourceFragment( result, source, |
60ec1c87 | 190 | size_t(lastSavedPos), |
fa1af598 | 191 | size_t(lastKnownPos - lastSavedPos) ); |
cecfc5e7 | 192 | |
60ec1c87 | 193 | // add footer |
fa1af598 | 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 | { | |
60ec1c87 | 213 | wxString& fname = ((spFile*)lst[f])->m_FileName; |
cecfc5e7 | 214 | |
fa1af598 | 215 | size_t fileNo = GetFileNo( fname ); |
cecfc5e7 | 216 | |
60ec1c87 | 217 | wxString& source = m_Contents[ fileNo ]; |
cecfc5e7 | 218 | |
60ec1c87 | 219 | wxString 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 | |
60ec1c87 | 230 | lastKnownPos = m_Contents[ fileNo ].length(); |
cecfc5e7 | 231 | |
60ec1c87 | 232 | // append the reminder |
cecfc5e7 | 233 | |
fa1af598 | 234 | DoAppendSourceFragment( source, result, |
60ec1c87 | 235 | size_t(lastSavedPos), |
fa1af598 | 236 | size_t(lastKnownPos - lastSavedPos) ); |
cecfc5e7 | 237 | |
fa1af598 | 238 | // replace original contnet with newly generated one |
cecfc5e7 | 239 | |
60ec1c87 | 240 | m_Contents[ fileNo ] = result; |
fa1af598 | 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 | |
60ec1c87 | 252 | for( size_t i = 0; i != m_Files.size(); ++i ) |
fa1af598 | 253 | { |
60ec1c87 | 254 | wxChar* s = (char*)(m_Contents[i].c_str()); |
cecfc5e7 | 255 | |
60ec1c87 | 256 | spFile* pFCtx = mParser.Parse( s, s + m_Contents[i].length() ); |
cecfc5e7 | 257 | |
60ec1c87 | 258 | pFCtx->m_FileName = m_Files[i]; |
cecfc5e7 | 259 | |
fa1af598 WS |
260 | AddMember( pFCtx ); |
261 | } | |
cecfc5e7 VZ |
262 | } |
263 | ||
264 | void spInterFileContext::WriteToFiles() | |
265 | { | |
60ec1c87 | 266 | for( size_t i = 0; i != m_Files.size(); ++i ) |
fa1af598 | 267 | { |
60ec1c87 | 268 | FILE* fp = fopen( m_Files[i].c_str(), "w+t" ); |
cecfc5e7 | 269 | |
fa1af598 WS |
270 | if ( fp != NULL ) |
271 | { | |
60ec1c87 | 272 | fwrite( m_Contents[i].c_str(), sizeof(char), m_Contents[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 | |
60ec1c87 | 283 | wxString& source = m_Contents[ 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 | |
60ec1c87 | 294 | wxString& source = m_Contents[ GetFileNoOfContext( *pCtx ) ]; |
cecfc5e7 | 295 | |
fa1af598 | 296 | return wxString( source.c_str() + pCtx->mSrcOffset, pCtx->mHeaderLength ); |
cecfc5e7 | 297 | } |