]>
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 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
28 #include "ifcontext.h"
30 /***** Implementation for class spInterFileContext *****/
32 size_t spInterFileContext::GetFileNo( const wxString
& fname
)
34 for ( size_t i
= 0; i
!= m_Files
.size(); ++i
)
36 if ( fname
== m_Files
[i
] )
40 wxFAIL_MSG("File not found in array in function spInterFileContext::GetFileNo()");
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
)->m_FileName
);
55 /*** public interface ***/
57 spInterFileContext::spInterFileContext()
60 spInterFileContext::~spInterFileContext()
63 void spInterFileContext::AddFile( const wxString
& fname
, const wxString
& content
)
65 m_Files
.push_back( fname
);
66 m_Contents
.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( wxString
& 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
122 wxFAIL_MSG("Overstepped the current source fragment in function\nspInterFileContext::DoAppendSourceFragment()");
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() )
137 lastKnownPos
= ctx
.mSrcOffset
;
139 if ( ctx
.IsVirtualContext() )
141 // add fragment accumulated before this context
143 DoAppendSourceFragment( source
, result
,
144 size_t(lastSavedPos
),
145 size_t(lastKnownPos
- lastSavedPos
) );
149 result
+= ctx
.GetVirtualContextBody();
151 lastSavedPos
= lastKnownPos
;
153 if ( ctx
.PositionIsKnown() )
155 if ( ctx
.VitualContextHasChildren() )
157 lastKnownPos
= ctx
.mSrcOffset
+ ctx
.mHeaderLength
;
159 lastSavedPos
= lastKnownPos
;
163 lastKnownPos
= ctx
.mSrcOffset
+ ctx
.mContextLength
;
165 lastSavedPos
= lastKnownPos
;
167 return; // have not children
172 MMemberListT
& lst
= ctx
.GetMembers();
174 for( size_t i
= 0; i
!= lst
.size(); ++i
)
176 GenerateContextBody( *lst
[i
], source
, result
, lastSavedPos
, lastKnownPos
);
179 if ( ctx
.IsVirtualContext() )
181 if ( ctx
.VitualContextHasChildren() && !ctx
.GetFooterOfVirtualContextBody().empty() )
183 // append the reminder space after children of the context
185 DoAppendSourceFragment( result
, source
,
186 size_t(lastSavedPos
),
187 size_t(lastKnownPos
- lastSavedPos
) );
190 result
+= ctx
.GetFooterOfVirtualContextBody();
192 lastKnownPos
= ctx
.mSrcOffset
+ ctx
.mContextLength
;
194 lastSavedPos
= lastKnownPos
;
198 if ( ctx
.PositionIsKnown() )
200 lastKnownPos
= ctx
.mSrcOffset
+ ctx
.mContextLength
;
203 void spInterFileContext::GenrateContents()
205 MMemberListT
& lst
= GetMembers();
207 for( size_t f
= 0; f
!= lst
.size(); ++f
)
209 wxString
& fname
= ((spFile
*)lst
[f
])->m_FileName
;
211 size_t fileNo
= GetFileNo( fname
);
213 wxString
& source
= m_Contents
[ fileNo
];
217 size_t lastKnownPos
= 0, // the begining of the file is always "known"
222 GenerateContextBody( *lst
[f
], source
, result
, lastSavedPos
, lastKnownPos
);
224 // the end of file is always known
226 lastKnownPos
= m_Contents
[ fileNo
].length();
228 // append the reminder
230 DoAppendSourceFragment( source
, result
,
231 size_t(lastSavedPos
),
232 size_t(lastKnownPos
- lastSavedPos
) );
234 // replace original contnet with newly generated one
236 m_Contents
[ fileNo
] = result
;
240 void spInterFileContext::ParseContents( SourceParserPlugin
* pPlugin
)
242 mDeletionMarks
.erase( mDeletionMarks
.begin(), mDeletionMarks
.end() );
244 RemoveChildren(); // clean up top-level context
246 mParser
.SetPlugin( pPlugin
);
248 for( size_t i
= 0; i
!= m_Files
.size(); ++i
)
250 wxChar
* s
= (char*)(m_Contents
[i
].c_str());
252 spFile
* pFCtx
= mParser
.Parse( s
, s
+ m_Contents
[i
].length() );
254 pFCtx
->m_FileName
= m_Files
[i
];
260 void spInterFileContext::WriteToFiles()
262 for( size_t i
= 0; i
!= m_Files
.size(); ++i
)
264 FILE* fp
= fopen( m_Files
[i
].c_str(), "w+t" );
268 fwrite( m_Contents
[i
].c_str(), sizeof(char), m_Contents
[i
].length(), fp
);
275 wxString
spInterFileContext::GetBody( spContext
* pCtx
)
277 wxASSERT( pCtx
->PositionIsKnown() ); // DBG:: should be checked by-user code
279 wxString
& source
= m_Contents
[ GetFileNoOfContext( *pCtx
) ];
281 return wxString( source
.c_str() + pCtx
->mSrcOffset
, pCtx
->mContextLength
);
284 wxString
spInterFileContext::GetHeader( spContext
* pCtx
)
286 wxASSERT( pCtx
->PositionIsKnown() ); // DBG:: should be checked by-user code
288 wxASSERT( pCtx
->mHeaderLength
!= -1 ); // DBG:: -/-
290 wxString
& source
= m_Contents
[ GetFileNoOfContext( *pCtx
) ];
292 return wxString( source
.c_str() + pCtx
->mSrcOffset
, pCtx
->mHeaderLength
);