Qucs-GUI  0.0.19
/home/travis/build/Qucs/qucs/qucs/qucs/components/vafile.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                               vafile.cpp
00003                              ------------
00004     begin                : Sun Oct 26 2009
00005     copyright            : (C) 2009 by Stefan Jahn
00006     email                : stefa@lkcc.org
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include <QString>
00019 #include <QRegExp>
00020 #include <QFile>
00021 #include <QFileInfo>
00022 
00023 #include "vafile.h"
00024 
00025 // -------------------------------------------------------
00026 VerilogA_File_Info::VerilogA_File_Info () {
00027   ModuleName = "";
00028   PortNames = "";
00029 }
00030 
00031 // -------------------------------------------------------
00032 VerilogA_File_Info::VerilogA_File_Info (QString File, bool isfile)
00033 {
00034   if (isfile) {
00035     QFile f (File);
00036     if (!f.open (QIODevice::ReadOnly))
00037       File = "";
00038     else {
00039       QByteArray FileContent = f.readAll ();
00040       File = QString (FileContent);
00041     }
00042     f.close();
00043   }
00044   
00045   QString s;
00046   int i=0, j, k=0;
00047   while((i=File.indexOf("//", i)) >= 0) { // remove all Verilog-A comments
00048     j = File.indexOf('\n', i+2);          // (This also finds "//" within a ...
00049     if(j < 0)                          //  string, but as no strings are ...
00050       File = File.left(i);             //  allowed in module headers, it ...
00051     else                               //  does not matter.)
00052       File.remove(i, j-i);
00053   }
00054 
00055   i=0;
00056   while((i=File.indexOf("/*", i)) >= 0) { // remove all Verilog-A comments
00057     j = File.indexOf("*/", i+2);          // (This also finds "/*" within a ...
00058     if(j < 0)                          //  string, but as no strings are ...
00059       File = File.left(i);             //  allowed in module headers, it ...
00060     else                               //  does not matter.)
00061       File.remove(i, j-i+2);
00062   }
00063 
00064   QRegExp Expr,Expr1;
00065   Expr.setCaseSensitive(true);
00066   Expr1.setCaseSensitive(true);
00067   k--;
00068   Expr.setPattern("\\bmodule\\b");  // start of last module
00069   k = File.findRev(Expr, k);
00070   if(k < 0)
00071     return;
00072 
00073   Expr.setPattern("\\bendmodule\\b");    // end of last module
00074   i = File.indexOf(Expr, k+7);
00075   if(i < 0)
00076     return;
00077   s = File.mid(k+7, i-k-7);  // cut out module declaration
00078 
00079   Expr.setPattern("\\b");
00080   i = s.indexOf(Expr);
00081   if(i < 0)
00082     return;
00083   j = s.indexOf(Expr, i+1);
00084   if(j < 0)
00085     return;
00086   ModuleName = s.mid(i, j-i);  // save module name
00087 
00088   i = s.indexOf('(', j);
00089   if(i < 0)
00090     return;
00091 
00092   j = s.indexOf(')', i);
00093   if(j < 0)
00094     return;
00095   s = s.mid(i+1, j-i-1);
00096 
00097   // parse ports, i.e. network connections
00098   PortNames = parsePorts (s, 0);
00099 }
00100 
00101 // -------------------------------------------------------
00102 QString VerilogA_File_Info::parsePorts(QString s, int i)
00103 {
00104   QRegExp Expr,Expr1;
00105   Expr.setCaseSensitive(true);
00106   Expr1.setCaseSensitive(true);
00107 
00108   int j;
00109   i = 0;    // remove all Verilog-A identifiers (e.g. "input")
00110   Expr.setPattern("(\\binput\\b|\\boutput\\b|\\binout\\b)");
00111   Expr1.setPattern("(\\b)");
00112   while((i=s.indexOf(Expr, i)) >= 0) {
00113     j = s.indexOf(Expr1, i+1);
00114     if(j < 0)
00115       s = s.left(i);
00116     else
00117       s.remove(i, j-i);
00118   }
00119 
00120   s.remove(' ');
00121   s.remove('\n');
00122   s.remove('\t');
00123   return s;
00124 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines