/////////////////////////////////////////////////////////////////////////////// 
// Copyright (C) 2003-2012, Open Design Alliance (the "Alliance"). 
// All rights reserved. 
// 
// This software and its documentation and related materials are owned by 
// the Alliance. The software may only be incorporated into application 
// programs owned by members of the Alliance, subject to a signed 
// Membership Agreement and Supplemental Software License Agreement with the
// Alliance. The structure and organization of this software are the valuable  
// trade secrets of the Alliance and its suppliers. The software is also 
// protected by copyright law and international treaty provisions. Application  
// programs incorporating this software must include the following statement 
// with their copyright notices:
//   
//   This application incorporates Teigha(R) software pursuant to a license 
//   agreement with Open Design Alliance.
//   Teigha(R) Copyright (C) 2003-2012 by Open Design Alliance. 
//   All rights reserved.
//
// By use of this software, its documentation or related materials, you 
// acknowledge and accept the above terms.
///////////////////////////////////////////////////////////////////////////////

#ifndef _EXEDINPUTPARSER_H_INCLUDED_
#define _EXEDINPUTPARSER_H_INCLUDED_

#include "TD_PackPush.h"

/** Description:
    This class implements InputParser objects.
    Remarks:
    Characters will be appended to the *result* buffer of an InputParser object until either
    
    o A '\r' or '\n' character is encountered. 
    o A ' ' or '\t' charactered is encountered outside a quoted or parenthesized string,
      and allowSpaces is false.
          
    {group:ExServices_Classes} 
    Library: Source *code* provided.
*/
class ExEdInputParser
{
  OdString  m_res;
  int       m_nParenthesis;
  bool      m_bQuote;
  bool      m_bAllowSpaces;
public:
  ExEdInputParser()
    : m_nParenthesis(0)
    , m_bQuote(false)
  {
    reset(false);
  }

  ~ExEdInputParser()
  {
  }

  /** Description:
    Resets the input parser.
    Arguments:
    allowSpaces (I) If true, the *result* will not be terminated by a ' ' or '\t' character.
    reserve (I) Initial size of buffer (ignored).
  */
  void reset(bool allowSpaces, int reserve = 128)
  {
    m_res.empty();
    m_bAllowSpaces = allowSpaces;
  }

  /** Description:
    Appends the specified character to the *result* buffer of this InputParser object.
    
    Remarks:
    Returns true if and only if the character has been appended.

    Characters will be appended to the *result* buffer of an InputParser object until either 
    
    o A '\r' or '\n' character is encountered. 
    o A ' ' or '\t' charactered is encountered outside a quoted or parenthesized string,
      and allowSpaces is false.

    Arguments:
    ch (I) Character to append.
  */
  bool addChar(OdChar ch)
  {
    switch(ch)
    {
    case L'(':
      if(!m_bQuote)
        ++m_nParenthesis;
      break;

    case L')':
      if(!m_bQuote)
        --m_nParenthesis;
      break;

    case L'\"':
      m_bQuote = !m_bQuote;
      break;

    case L' ':
    case L'\t':
      if(m_bQuote || m_nParenthesis || m_bAllowSpaces)
      {
        m_res += ch;
        return true;
      }
      //no break
    case L'\r':
    case L'\n':
      return false;
    }
    m_res += ch;
    return true;
  }
  /** Description:
    Removes the last character from the *result* buffer of this InputParser object.
    Remarks:
    Returns true if and only if the *result* buffer is not empty.
  */
  bool eraseChar()
  {
    if(!m_res.isEmpty())
    {
      OdChar ch = m_res.right(1)[0];
      m_res.deleteChars(m_res.getLength()-1, 1);
      switch(ch)
      {
      case L')':
        if(!m_bQuote)
          ++m_nParenthesis;
        break;
        
      case L'(':
        if(!m_bQuote)
          --m_nParenthesis;
        break;
        
      case L'\"':
        m_bQuote = !m_bQuote;
        break;
      }
    }
    return !m_res.isEmpty();
  }
  /** Description:
    Returns the *result* buffer of this InputParser object.
  */
  const OdString& result() const
  {
    return m_res;
  }
};

#include "TD_PackPop.h"

#endif //_EXEDINPUTPARSER_H_INCLUDED_
