LLOOP Index | GSP Language | GSP Library | Framework Classes | Component Classes

define.gsp

This is the verbatim text of the file "define.gsp" part of the LLOOP package. The copyright remains with Michel MEHL. All rights reserved.



/**
 * Pre-processes C #define.
 * Fulfils the same function as a C #define statement. 
 *
 * The static member function "add" allows to add a new define,
 * given the define name and the replacement string
 eg:
 #define NAME "James" => define::add("NAME", "\"James\"");
 */

preprocessor defines include "universal__String.h", <vector>
declare 
{{
  private:
    static std::vector<universal::String> ms_defines;
    static std::vector<universal::String> ms_replace_strings;
    static std::vector<unsigned int> ms_cmp_cursors;
  public:
    static void add(const char *pszDefine, const char* pszReplace = "");
}}
implement
{{
  std::vector<universal::String> defines::ms_defines;
  std::vector<universal::String> defines::ms_replace_strings;
  std::vector<unsigned int> defines::ms_cmp_cursors;
  void defines::add(const char *pszDefine, const char* pszReplace)
  {
    ms_defines.push_back(universal::String(pszDefine));
    ms_replace_strings.push_back(universal::String(pszReplace));
    ms_cmp_cursors.push_back(0);
  }
}}
{{
  unsigned long i = 0;
  for (i = 0 ; i < ms_defines.size(); i++)
    {
      /* Compare the current char with the char pointed
       * in the compared define string. If the char matches
       * we can increment the pointer. Otherwise force the 
       * pointer to zero.
       */

      if (ms_cmp_cursors[i] < ms_defines[i].length())
	{
	  if (c == ms_defines[i][ms_cmp_cursors[i]])
	    {
	      ms_cmp_cursors[i]++;
	    }
	  else
	    {
	      ms_cmp_cursors[i] = 0;
	      continue; // Can continue with next define
	    }
	}

      /* Check whether there is a define match.
       * If so, discard the char and rewind the output stream of the length
       * of the defined string to remove it and replace it
       * the define value.
       * The pre-processing for the current 'c' char
       * must also be exited.
       */

      if (ms_cmp_cursors[i] == ms_defines[i].length())
	{
	  discard();
	  ms_cmp_cursors[i] = 0;
 	  parser.pstream().seekp((std::streampos)1 - (std::streampos)ms_defines[i].length(), ios::cur); 
	  parser.pstream() << ms_replace_strings[i]; 
	  break; // Continue pre-processing with the next char
	}
    }
}}

This file is part of the LLOOP Reversible Object-Oriented Parser Generator. Copyright (c) 2005-2006 Michel MEHL, France. All rights reserved. LLOOP is distributed by the company ERSA SaRL.


Copyright (c) 2005-2006 Michel MEHL, Haguenau, France
LLOOP version 1.1