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