/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM is free software; you can redistribute it and/or modify it    
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; only version 2 of the License.      
 * 
 * NMM is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 
 * License for more details.  
 * 
 * You should have received a copy of the GNU General Public License 
 * along with NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */
 
#include <string>
#include <iostream>
#include <fstream>

#define YYSTYPE const char *

extern unsigned int lineno;
extern char *yytext;


namespace NMM {
  class NodeDescription;
}

using namespace std;

/** Linux specific helper methods to parse GD files.
 *  This file is included indirect threw ycommon.hpp.
 */

extern "C"
{
  int yylex(void);
  int yyparse(void);
  int yywrap()
  {
    return 1;
  }

  void yyerror(const char *str)
  {
    fprintf(stderr,"error: in line %d: %s at '%s'\n",lineno+1, str, yytext);
    exit(1);
  }

}

extern int yydebug;

char* c_string(const string& s)
{
  char* p = new char[s.length() + 1];
  strcpy(p, s.c_str());
  return p;
}


void __replace(string& src, const string& what, const string& with)
{
  int n = 0;
  while((n = src.find(what, n + what.size())) > 0) {
    src.replace(n, what.size(), with);
  }
}

bool __remove_namespaces(string& s) 
{
  size_t pos = s.rfind("::");
  if(pos != string::npos) {
    s = s.substr(pos+2);
    return true;
  }
  else {
    return false; // no :: were found -> false
  }
}

string __get_extra_in_file(const string& filename, const string& extension)
{
  string extra_in_filename = filename;
  __replace(extra_in_filename, ".idl", extension);

  // Try to open in-file
  ifstream in(extra_in_filename.c_str());
  
  string s = "";
  string line = "";
  if(in) {
    while(getline(in, line)) {
      s += line + "\n";
    }
  }
  return s;
} 

#define __append(old, app) if(old) { ostringstream s; s << old << app; old = c_string(s.str()); } else { old = app; }
#define __set(old, app) ostringstream __s; __s << app; old = c_string(__s.str());

string filename = "";
string suffix   = "";

