]>
git.saurik.com Git - wxWidgets.git/blob - utils/HelpGen/src/ifcontext.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Contrib. demo
4 // Author: Aleksandras Gluchovas
8 // Copyright: (c) Aleskandars Gluchovas
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "ifcontext.h"
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
31 #include "ifcontext.h"
33 /***** Implementation for class spInterFileContext *****/
35 size_t spInterFileContext::GetFileNo( const string
& fname
)
37 for( size_t i
= 0; i
!= mFiles
.size(); ++i
)
39 if ( fname
== mFiles
[i
] ) return i
;
45 size_t spInterFileContext::GetFileNoOfContext( spContext
& ctx
)
47 spContext
* pCtx
= ctx
.GetEnclosingContext( SP_CTX_FILE
);
49 // DBG:: outer-file context should be present
50 wxASSERT( pCtx
&& pCtx
->GetType() == SP_CTX_FILE
);
52 return GetFileNo( ((spFile
*)pCtx
)->mFileName
);
55 /*** public interface ***/
57 spInterFileContext::spInterFileContext()
60 spInterFileContext::~spInterFileContext()
63 void spInterFileContext::AddFile( const string
& fname
, const string
& content
)
65 mFiles
.push_back( fname
);
66 mContents
.push_back( content
);
69 void spInterFileContext::RemoveContext( spContext
& ctx
)
71 wxASSERT( ctx
.PositionIsKnown() ); // DBG:: should be checked by-user code
73 size_t fNo
= GetFileNoOfContext( ctx
);
75 mDeletionMarks
.push_back( spBookmark( ctx
.mSrcOffset
, ctx
.mContextLength
, fNo
) );
78 void spInterFileContext::InsertBookmarkSorted( BookmarkListT
& lst
, spBookmark
& mark
)
80 for( size_t i
= 0; i
!= lst
.size(); ++i
)
82 if ( lst
[i
].mFrom
> mark
.mFrom
)
84 lst
.insert( &lst
[i
], mark
);
88 lst
.push_back( mark
);
91 void spInterFileContext::DoAppendSourceFragment( string
& source
,
93 size_t pos
, size_t len
)
95 mFiltered
.erase( mFiltered
.begin(), mFiltered
.end() );
99 for( i
= 0; i
!= mDeletionMarks
.size(); ++i
)
101 spBookmark
& mark
= mDeletionMarks
[i
];
103 if ( mark
.mFileNo
== mCurFileNo
&&
104 mark
.mFrom
>= pos
&& mark
.mFrom
< pos
+ len
)
106 InsertBookmarkSorted( mFiltered
, mark
);
111 for( i
= 0; i
!= mFiltered
.size(); ++ i
)
113 spBookmark
& mark
= mFiltered
[i
];
115 result
.append( source
, cur
, ( (size_t)mark
.mFrom
- cur
) );
117 cur
= size_t( mark
.mFrom
+ mark
.mLen
);
119 if ( cur
>= pos
+ len
) // check if we've overstepped the current source-fragment
121 wxASSERT(0); // DBG:: with current imp. this should not happen
123 cur
= pos
+ len
; break;
127 result
.append( source
, cur
, ( pos
+ len
) - cur
);
130 void spInterFileContext::GenerateContextBody( spContext
& ctx
,
133 size_t& lastSavedPos
,
134 size_t& lastKnownPos
)
136 if ( ctx
.PositionIsKnown() )
138 lastKnownPos
= ctx
.mSrcOffset
;
140 if ( ctx
.IsVirtualContext() )
142 // add fragment accumulated before this context
144 DoAppendSourceFragment( source
, result
,
145 size_t(lastSavedPos
),
146 size_t(lastKnownPos
- lastSavedPos
) );
150 result
+= ctx
.GetVirtualContextBody();
152 lastSavedPos
= lastKnownPos
;
154 if ( ctx
.PositionIsKnown() )
156 if ( ctx
.VitualContextHasChildren() )
158 lastKnownPos
= ctx
.mSrcOffset
+ ctx
.mHeaderLength
;
160 lastSavedPos
= lastKnownPos
;
164 lastKnownPos
= ctx
.mSrcOffset
+ ctx
.mContextLength
;
166 lastSavedPos
= lastKnownPos
;
168 return; // have not children
173 MMemberListT
& lst
= ctx
.GetMembers();
175 for( size_t i
= 0; i
!= lst
.size(); ++i
)
177 GenerateContextBody( *lst
[i
], source
, result
, lastSavedPos
, lastKnownPos
);
179 if ( ctx
.IsVirtualContext() )
181 if ( ctx
.VitualContextHasChildren() &&
183 ctx
.GetFooterOfVirtualContextBody() != "" )
185 // append the reminder space after children of the context
187 DoAppendSourceFragment( result
, source
,
188 size_t(lastSavedPos
),
189 size_t(lastKnownPos
- lastSavedPos
) );
192 result
+= ctx
.GetFooterOfVirtualContextBody();
194 lastKnownPos
= ctx
.mSrcOffset
+ ctx
.mContextLength
;
196 lastSavedPos
= lastKnownPos
;
200 if ( ctx
.PositionIsKnown() )
202 lastKnownPos
= ctx
.mSrcOffset
+ ctx
.mContextLength
;
205 void spInterFileContext::GenrateContents()
207 MMemberListT
& lst
= GetMembers();
209 for( size_t f
= 0; f
!= lst
.size(); ++f
)
211 string
& fname
= ((spFile
*)lst
[f
])->mFileName
;
213 size_t fileNo
= GetFileNo( fname
);
215 string
& source
= mContents
[ fileNo
];
219 size_t lastKnownPos
= 0, // the begining of the file is always "known"
224 GenerateContextBody( *lst
[f
], source
, result
, lastSavedPos
, lastKnownPos
);
226 // the end of file is always known
228 lastKnownPos
= mContents
[ fileNo
].length();
230 // append the reminder
232 DoAppendSourceFragment( source
, result
,
233 size_t(lastSavedPos
),
234 size_t(lastKnownPos
- lastSavedPos
) );
236 // replace original contnet with newly generated one
238 mContents
[ fileNo
] = result
;
242 void spInterFileContext::ParseContents( SourceParserPlugin
* pPlugin
)
244 mDeletionMarks
.erase( mDeletionMarks
.begin(), mDeletionMarks
.end() );
246 RemoveChildren(); // clean up top-level context
248 mParser
.SetPlugin( pPlugin
);
250 for( size_t i
= 0; i
!= mFiles
.size(); ++i
)
252 char* s
= (char*)(mContents
[i
].c_str());
254 spFile
* pFCtx
= mParser
.Parse( s
, s
+ mContents
[i
].length() );
256 pFCtx
->mFileName
= mFiles
[i
];
262 void spInterFileContext::WriteToFiles()
264 for( size_t i
= 0; i
!= mFiles
.size(); ++i
)
266 FILE* fp
= fopen( mFiles
[i
].c_str(), "w+t" );
270 fwrite( mContents
[i
].c_str(), sizeof(char), mContents
[i
].length(), fp
);
277 string
spInterFileContext::GetBody( spContext
* pCtx
)
279 wxASSERT( pCtx
->PositionIsKnown() ); // DBG:: should be checked by-user code
281 string
& source
= mContents
[ GetFileNoOfContext( *pCtx
) ];
283 return string( source
.c_str() + pCtx
->mSrcOffset
, pCtx
->mContextLength
);
286 string
spInterFileContext::GetHeader( spContext
* pCtx
)
288 wxASSERT( pCtx
->PositionIsKnown() ); // DBG:: should be checked by-user code
290 wxASSERT( pCtx
->mHeaderLength
!= -1 ); // DBG:: -/-
292 string
& source
= mContents
[ GetFileNoOfContext( *pCtx
) ];
294 return string( source
.c_str() + pCtx
->mSrcOffset
, pCtx
->mHeaderLength
);