STYLE: Fix typo in GetFilenameLastExtension docs
[cmake.git] / Source / cmDependsJavaParserHelper.cxx
blob727bb627e8421d9c58ca91d42ce2147675d5d7d6
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmDependsJavaParserHelper.cxx,v $
5 Language: C++
6 Date: $Date: 2006-05-10 19:01:22 $
7 Version: $Revision: 1.5 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmDependsJavaParserHelper.h"
19 #include "cmSystemTools.h"
20 #include "cmDependsJavaLexer.h"
22 int cmDependsJava_yyparse( yyscan_t yyscanner );
24 cmDependsJavaParserHelper::cmDependsJavaParserHelper()
26 this->CurrentDepth = 0;
28 this->UnionsAvailable = 0;
29 this->LastClassId = 0;
31 CurrentClass tl;
32 tl.Name = "*";
33 this->ClassStack.push_back(tl);
37 cmDependsJavaParserHelper::~cmDependsJavaParserHelper()
39 this->CleanupParser();
42 void cmDependsJavaParserHelper::CurrentClass
43 ::AddFileNamesForPrinting(std::vector<cmStdString> *files,
44 const char* prefix, const char* sep)
46 cmStdString rname = "";
47 if ( prefix )
49 rname += prefix;
50 rname += sep;
52 rname += this->Name;
53 files->push_back(rname);
54 std::vector<CurrentClass>::iterator it;
55 for ( it = this->NestedClasses.begin();
56 it != this->NestedClasses.end();
57 ++ it )
59 it->AddFileNamesForPrinting(files, rname.c_str(), sep);
63 void cmDependsJavaParserHelper::DeallocateParserType(char** pt)
65 if (!pt)
67 return;
69 if (!*pt)
71 return;
73 *pt = 0;
74 this->UnionsAvailable --;
77 void cmDependsJavaParserHelper::AddClassFound(const char* sclass)
79 if( ! sclass )
81 return;
83 std::vector<cmStdString>::iterator it;
84 for ( it = this->ClassesFound.begin();
85 it != this->ClassesFound.end();
86 it ++ )
88 if ( *it == sclass )
90 return;
93 this->ClassesFound.push_back(sclass);
96 void cmDependsJavaParserHelper::AddPackagesImport(const char* sclass)
98 std::vector<cmStdString>::iterator it;
99 for ( it = this->PackagesImport.begin();
100 it != this->PackagesImport.end();
101 it ++ )
103 if ( *it == sclass )
105 return;
108 this->PackagesImport.push_back(sclass);
111 void cmDependsJavaParserHelper::SafePrintMissing(const char* str,
112 int line, int cnt)
114 if ( str )
116 std::cout << line << " String " << cnt << " exists: ";
117 unsigned int cc;
118 for ( cc = 0; cc < strlen(str); cc ++ )
120 unsigned char ch = str[cc];
121 if ( ch >= 32 && ch <= 126 )
123 std::cout << (char)ch;
125 else
127 std::cout << "<" << (int)ch << ">";
128 break;
131 std::cout << "- " << strlen(str) << std::endl;
134 void cmDependsJavaParserHelper::Print(const char* place, const char* str)
136 if ( this->Verbose )
138 std::cout << "[" << place << "=" << str << "]" << std::endl;
142 void cmDependsJavaParserHelper::CombineUnions(char** out,
143 const char* in1, char** in2,
144 const char* sep)
146 size_t len = 1;
147 if ( in1 )
149 len += strlen(in1);
151 if ( *in2 )
153 len += strlen(*in2);
155 if ( sep )
157 len += strlen(sep);
159 *out = new char [ len ];
160 *out[0] = 0;
161 if ( in1 )
163 strcat(*out, in1);
165 if ( sep )
167 strcat(*out, sep);
169 if ( *in2 )
171 strcat(*out, *in2);
173 if ( *in2 )
175 this->DeallocateParserType(in2);
177 this->UnionsAvailable ++;
180 void cmDependsJavaParserHelper
181 ::CheckEmpty(int line, int cnt, cmDependsJavaParserHelper::ParserType* pt)
183 int cc;
184 int kk = -cnt + 1;
185 for ( cc = 1; cc <= cnt; cc ++)
187 cmDependsJavaParserHelper::ParserType* cpt = pt + kk;
188 this->SafePrintMissing(cpt->str, line, cc);
189 kk ++;
193 void cmDependsJavaParserHelper
194 ::PrepareElement(cmDependsJavaParserHelper::ParserType* me)
196 // Inititalize self
197 me->str = 0;
200 void cmDependsJavaParserHelper
201 ::AllocateParserType(cmDependsJavaParserHelper::ParserType* pt,
202 const char* str, int len)
204 pt->str = 0;
205 if ( len == 0 )
207 len = (int)strlen(str);
209 if ( len == 0 )
211 return;
213 this->UnionsAvailable ++;
214 pt->str = new char[ len + 1 ];
215 strncpy(pt->str, str, len);
216 pt->str[len] = 0;
217 this->Allocates.push_back(pt->str);
220 void cmDependsJavaParserHelper::StartClass(const char* cls)
222 CurrentClass cl;
223 cl.Name = cls;
224 this->ClassStack.push_back(cl);
226 this->CurrentDepth ++;
229 void cmDependsJavaParserHelper::EndClass()
231 CurrentClass* parent = 0;
232 CurrentClass* current = 0;
233 if ( this->ClassStack.size() > 0 )
235 current = &(*(this->ClassStack.end() - 1));
236 if ( this->ClassStack.size() > 1 )
238 parent = &(*(this->ClassStack.end() - 2));
241 if ( current == 0 )
243 std::cerr << "Error when parsing. Current class is null" << std::endl;
244 abort();
246 if ( parent == 0 )
248 std::cerr << "Error when parsing. Parent class is null" << std::endl;
249 abort();
251 this->CurrentDepth --;
252 parent->NestedClasses.push_back(*current);
253 this->ClassStack.erase(this->ClassStack.end()-1, this->ClassStack.end());
256 void cmDependsJavaParserHelper::PrintClasses()
258 if ( this->ClassStack.size() == 0 )
260 std::cerr << "Error when parsing. No classes on class stack" << std::endl;
261 abort();
263 std::vector<cmStdString> files = this->GetFilesProduced();
264 std::vector<cmStdString>::iterator sit;
265 for ( sit = files.begin();
266 sit != files.end();
267 ++ sit )
269 std::cout << " " << sit->c_str() << ".class" << std::endl;
273 std::vector<cmStdString> cmDependsJavaParserHelper::GetFilesProduced()
275 std::vector<cmStdString> files;
276 CurrentClass* toplevel = &(*(this->ClassStack.begin()));
277 std::vector<CurrentClass>::iterator it;
278 for ( it = toplevel->NestedClasses.begin();
279 it != toplevel->NestedClasses.end();
280 ++ it )
282 it->AddFileNamesForPrinting(&files, 0, "$");
284 return files;
287 int cmDependsJavaParserHelper::ParseString(const char* str, int verb)
289 if ( !str)
291 return 0;
293 this->Verbose = verb;
294 this->InputBuffer = str;
295 this->InputBufferPos = 0;
296 this->CurrentLine = 0;
299 yyscan_t yyscanner;
300 cmDependsJava_yylex_init(&yyscanner);
301 cmDependsJava_yyset_extra(this, yyscanner);
302 int res = cmDependsJava_yyparse(yyscanner);
303 cmDependsJava_yylex_destroy(yyscanner);
304 if ( res != 0 )
306 std::cout << "JP_Parse returned: " << res << std::endl;
307 return 0;
310 if ( verb )
312 if ( this->CurrentPackage.size() > 0 )
314 std::cout << "Current package is: " <<
315 this->CurrentPackage.c_str() << std::endl;
317 std::cout << "Imports packages:";
318 if ( this->PackagesImport.size() > 0 )
320 std::vector<cmStdString>::iterator it;
321 for ( it = this->PackagesImport.begin();
322 it != this->PackagesImport.end();
323 ++ it )
325 std::cout << " " << it->c_str();
328 std::cout << std::endl;
329 std::cout << "Depends on:";
330 if ( this->ClassesFound.size() > 0 )
332 std::vector<cmStdString>::iterator it;
333 for ( it = this->ClassesFound.begin();
334 it != this->ClassesFound.end();
335 ++ it )
337 std::cout << " " << it->c_str();
340 std::cout << std::endl;
341 std::cout << "Generated files:" << std::endl;
342 this->PrintClasses();
343 if ( this->UnionsAvailable != 0 )
345 std::cout << "There are still " <<
346 this->UnionsAvailable << " unions available" << std::endl;
349 this->CleanupParser();
350 return 1;
353 void cmDependsJavaParserHelper::CleanupParser()
355 std::vector<char*>::iterator it;
356 for ( it = this->Allocates.begin();
357 it != this->Allocates.end();
358 ++ it )
360 delete [] *it;
362 this->Allocates.erase(this->Allocates.begin(),
363 this->Allocates.end());
366 int cmDependsJavaParserHelper::LexInput(char* buf, int maxlen)
368 if ( maxlen < 1 )
370 return 0;
372 if ( this->InputBufferPos < this->InputBuffer.size() )
374 buf[0] = this->InputBuffer[ this->InputBufferPos++ ];
375 if ( buf[0] == '\n' )
377 this->CurrentLine ++;
379 return(1);
381 else
383 buf[0] = '\n';
384 return( 0 );
387 void cmDependsJavaParserHelper::Error(const char* str)
389 unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
390 fprintf(stderr, "JPError: %s (%lu / Line: %d)\n",
391 str, pos, this->CurrentLine);
392 int cc;
393 std::cerr << "String: [";
394 for ( cc = 0;
395 cc < 30 && *(this->InputBuffer.c_str() + this->InputBufferPos + cc);
396 cc ++ )
398 std::cerr << *(this->InputBuffer.c_str() + this->InputBufferPos + cc);
400 std::cerr << "]" << std::endl;
403 void cmDependsJavaParserHelper::UpdateCombine(const char* str1,
404 const char* str2)
406 if ( this->CurrentCombine == "" && str1 != 0)
408 this->CurrentCombine = str1;
410 this->CurrentCombine += ".";
411 this->CurrentCombine += str2;
414 int cmDependsJavaParserHelper::ParseFile(const char* file)
416 if ( !cmSystemTools::FileExists(file))
418 return 0;
420 std::ifstream ifs(file);
421 if ( !ifs )
423 return 0;
426 cmStdString fullfile = "";
427 cmStdString line;
428 while ( cmSystemTools::GetLineFromStream(ifs, line) )
430 fullfile += line + "\n";
432 return this->ParseString(fullfile.c_str(), 0);