C++ (Cpp) wxExVariable Example

Introduction

The c++ (cpp) wxexvariable example is extracted from the most popular open source projects, you can refer to the following example for usage.

Programming language: C++ (Cpp)

Class/type: wxExVariable

Example#1

bool wxExViMacros::ExpandTemplate(
  wxExEx* ex, const wxExVariable& v, std::string& expanded)
{
  if (v.GetValue().empty())
  {
    return false;
  }
  
  if (!m_IsExpand)
  {
    m_IsExpand = true;
    AskForInput();
  }

  // Read the file (file name is in v.GetValue()), expand
  // all macro variables in it, and set expanded.
  const wxExFileName filename(wxExConfigDir(), v.GetValue());

  std::ifstream ifs(filename.GetFullPath());
  
  if (!ifs.is_open())
  {
    wxLogError("Could not open template file: %s", filename.GetFullPath().c_str());
    return false;
  }

  // Keep current macro, in case you cancel expanding,
  // this one is restored.
  std::string macro = m_Macro;
  
  while (ifs.good() && !ifs.eof()) 
  {
    const char c = ifs.get();
    
    if (c != '@')
    {
      expanded += c;
    }
    else
    {
      std::string variable;
      bool completed = false;
      
      while (ifs.good() && !ifs.eof() && !completed) 
      {
        const char c = ifs.get();
    
        if (c != '@')
        {
          variable += c;
        }
        else
        {
          completed = true;
        }
      }
      
      if (!completed)
      {
        m_Macro = macro;
        return false;
      }
      
      // Prevent recursion.
      if (variable == v.GetName())
      {
        m_Macro = macro;
        return false;
      }
      
      std::string value;
      
      if (!Expand(ex, variable, value))
      {
        m_Macro = macro;
        return false;
      }
      
      expanded += value;
    }
  }
  
  m_IsExpand = false;

  // Set back to normal value.  
  AskForInput();
    
  if (!m_IsRecording)
  {
    m_Macro = v.GetName();
  }
    
  return true;
}

Example#2
File: vimacros.cppProject: hfvw/wxExtension

bool wxExViMacros::ExpandTemplate(
  wxExEx* ex, const wxExVariable& v, wxString& expanded)
{
  if (!m_IsExpand)
  {
    m_IsExpand = true;
    AskForInput();
  }

  // Read the file (file name is in m_Value), expand
  // all macro variables in it, and set expanded.
  const wxFileName filename(
#ifdef wxExUSE_PORTABLE
      wxPathOnly(wxStandardPaths::Get().GetExecutablePath())
#else
      wxStandardPaths::Get().GetUserDataDir()
#endif
      + wxFileName::GetPathSeparator() + v.GetValue());

  wxFileInputStream input(filename.GetFullPath());
  
  if (!input.IsOk())
  {
    wxLogError("Could not open template file: " + filename.GetFullPath());
    return false;
  }

  // Keep current macro, in case you cancel expanding,
  // this one is restored.
  wxString macro = m_Macro;
  
  wxTextInputStream text(input);
  
  while (input.IsOk() && !input.Eof()) 
  {
    const wxChar c = text.GetChar();
    
    if (c != '@')
    {
      expanded += c;
    }
    else
    {
      wxString variable;
      bool completed = false;
      
      while (input.IsOk() && !input.Eof() && !completed) 
      {
        const wxChar c = text.GetChar();
    
        if (c != '@')
        {
          variable += c;
        }
        else
        {
          completed = true;
        }
      }
      
      if (!completed)
      {
        m_Macro = macro;
        return false;
      }
      
      // Prevent recursion.
      if (variable == v.GetName())
      {
        m_Macro = macro;
        return false;
      }
      
      wxString value;
      
      if (!Expand(ex, variable, value))
      {
        m_Macro = macro;
        return false;
      }
      
      expanded += value;
    }
  }
  
  m_IsExpand = false;

  // Set back to normal value.  
  AskForInput();
    
  if (!m_IsRecording)
  {
    m_Macro = v.GetName();
  }
    
  return true;
}