]>
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"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
32 #include "ifcontext.h"
34 /***** Implementation for class spInterFileContext *****/
36 size_t spInterFileContext::GetFileNo( const wxString
& fname
)
38 for ( size_t i
= 0; i
!= m_Files
.size(); ++i
)
40 if ( fname
== m_Files
[i
] )
44 wxFAIL_MSG("File not found in array in function spInterFileContext::GetFileNo()");
49 size_t spInterFileContext::GetFileNoOfContext( spContext
& ctx
)
51 spContext
* pCtx
= ctx
.GetEnclosingContext( SP_CTX_FILE
);
53 // DBG:: outer-file context should be present
54 wxASSERT( pCtx
&& pCtx
->GetType() == SP_CTX_FILE
);
56 return GetFileNo( ((spFile
*)pCtx
)->m_FileName
);
59 /*** public interface ***/
61 spInterFileContext::spInterFileContext()
64 spInterFileContext::~spInterFileContext()
67 void spInterFileContext::AddFile( const wxString
& fname
, const wxString
& content
)
69 m_Files
.push_back( fname
);
70 m_Contents
.push_back( content
);
73 void spInterFileContext::RemoveContext( spContext
& ctx
)
75 wxASSERT( ctx
.PositionIsKnown() ); // DBG:: should be checked by-user code
77 size_t fNo
= GetFileNoOfContext( ctx
);
79 mDeletionMarks
.push_back( spBookmark( ctx
.mSrcOffset
, ctx
.mContextLength
, fNo
) );
82 void spInterFileContext::InsertBookmarkSorted( BookmarkListT
& lst
, spBookmark
& mark
)
84 for( size_t i
= 0; i
!= lst
.size(); ++i
)
86 if ( lst
[i
].mFrom
> mark
.mFrom
)
88 lst
.insert( &lst
[i
], mark
);
92 lst
.push_back( mark
);
95 void spInterFileContext::DoAppendSourceFragment( wxString
& source
,
97 size_t pos
, size_t len
)
99 mFiltered
.erase( mFiltered
.begin(), mFiltered
.end() );
103 for( i
= 0; i
!= mDeletionMarks
.size(); ++i
)
105 spBookmark
& mark
= mDeletionMarks
[i
];
107 if ( mark
.mFileNo
== mCurFileNo
&&
108 mark
.mFrom
>= pos
&& mark
.mFrom
< pos
+ len
)
110 InsertBookmarkSorted( mFiltered
, mark
);
115 for( i
= 0; i
!= mFiltered
.size(); ++ i
)
117 spBookmark
& mark
= mFiltered
[i
];
119 result
.append( source
, cur
, ( (size_t)mark
.mFrom
- cur
) );
121 cur
= size_t( mark
.mFrom
+ mark
.mLen
);
123 if ( cur
>= pos
+ len
) // check if we've overstepped the current source-fragment
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;
131 result
.append( source
, cur
, ( pos
+ len
) - cur
);
134 void spInterFileContext::GenerateContextBody( spContext
& ctx
,
137 size_t& lastSavedPos
,
138 size_t& lastKnownPos
)
140 if ( ctx
.PositionIsKnown() )
141 lastKnownPos
= ctx
.mSrcOffset
;
143 if ( ctx
.IsVirtualContext() )
145 // add fragment accumulated before this context
147 DoAppendSourceFragment( source
, result
,
148 size_t(lastSavedPos
),
149 size_t(lastKnownPos
- lastSavedPos
) );
153 result
+= ctx
.GetVirtualContextBody();
155 lastSavedPos
= lastKnownPos
;
157 if ( ctx
.PositionIsKnown() )
159 if ( ctx
.VitualContextHasChildren() )
161 lastKnownPos
= ctx
.mSrcOffset
+ ctx
.mHeaderLength
;
163 lastSavedPos
= lastKnownPos
;
167 lastKnownPos
= ctx
.mSrcOffset
+ ctx
.mContextLength
;
169 lastSavedPos
= lastKnownPos
;
171 return; // have not children
176 MMemberListT
& lst
= ctx
.GetMembers();
178 for( size_t i
= 0; i
!= lst
.size(); ++i
)
180 GenerateContextBody( *lst
[i
], source
, result
, lastSavedPos
, lastKnownPos
);
183 if ( ctx
.IsVirtualContext() )
185 if ( ctx
.VitualContextHasChildren() && !ctx
.GetFooterOfVirtualContextBody().empty() )
187 // append the reminder space after children of the context
189 DoAppendSourceFragment( result
, source
,
190 size_t(lastSavedPos
),
191 size_t(lastKnownPos
- lastSavedPos
) );
194 result
+= ctx
.GetFooterOfVirtualContextBody();
196 lastKnownPos
= ctx
.mSrcOffset
+ ctx
.mContextLength
;
198 lastSavedPos
= lastKnownPos
;
202 if ( ctx
.PositionIsKnown() )
204 lastKnownPos
= ctx
.mSrcOffset
+ ctx
.mContextLength
;
207 void spInterFileContext::GenrateContents()
209 MMemberListT
& lst
= GetMembers();
211 for( size_t f
= 0; f
!= lst
.size(); ++f
)
213 wxString
& fname
= ((spFile
*)lst
[f
])->m_FileName
;
215 size_t fileNo
= GetFileNo( fname
);
217 wxString
& source
= m_Contents
[ fileNo
];
221 size_t lastKnownPos
= 0, // the begining of the file is always "known"
226 GenerateContextBody( *lst
[f
], source
, result
, lastSavedPos
, lastKnownPos
);
228 // the end of file is always known
230 lastKnownPos
= m_Contents
[ fileNo
].length();
232 // append the reminder
234 DoAppendSourceFragment( source
, result
,
235 size_t(lastSavedPos
),
236 size_t(lastKnownPos
- lastSavedPos
) );
238 // replace original contnet with newly generated one
240 m_Contents
[ fileNo
] = result
;
244 void spInterFileContext::ParseContents( SourceParserPlugin
* pPlugin
)
246 mDeletionMarks
.erase( mDeletionMarks
.begin(), mDeletionMarks
.end() );
248 RemoveChildren(); // clean up top-level context
250 mParser
.SetPlugin( pPlugin
);
252 for( size_t i
= 0; i
!= m_Files
.size(); ++i
)
254 wxChar
* s
= (char*)(m_Contents
[i
].c_str());
256 spFile
* pFCtx
= mParser
.Parse( s
, s
+ m_Contents
[i
].length() );
258 pFCtx
->m_FileName
= m_Files
[i
];
264 void spInterFileContext::WriteToFiles()
266 for( size_t i
= 0; i
!= m_Files
.size(); ++i
)
268 FILE* fp
= fopen( m_Files
[i
].c_str(), "w+t" );
272 fwrite( m_Contents
[i
].c_str(), sizeof(char), m_Contents
[i
].length(), fp
);
279 wxString
spInterFileContext::GetBody( spContext
* pCtx
)
281 wxASSERT( pCtx
->PositionIsKnown() ); // DBG:: should be checked by-user code
283 wxString
& source
= m_Contents
[ GetFileNoOfContext( *pCtx
) ];
285 return wxString( source
.c_str() + pCtx
->mSrcOffset
, pCtx
->mContextLength
);
288 wxString
spInterFileContext::GetHeader( spContext
* pCtx
)
290 wxASSERT( pCtx
->PositionIsKnown() ); // DBG:: should be checked by-user code
292 wxASSERT( pCtx
->mHeaderLength
!= -1 ); // DBG:: -/-
294 wxString
& source
= m_Contents
[ GetFileNoOfContext( *pCtx
) ];
296 return wxString( source
.c_str() + pCtx
->mSrcOffset
, pCtx
->mHeaderLength
);