Blame vhdlparser/vhdlstring.h

Packit 1c1d7e
#ifndef VHDLSTRING_H
Packit 1c1d7e
#define VHDLSTRING_H
Packit 1c1d7e
Packit 1c1d7e
#include <stdio.h>
Packit 1c1d7e
#include <stdlib.h>
Packit 1c1d7e
#include <string.h>
Packit 1c1d7e
Packit 1c1d7e
Packit 1c1d7e
/** @brief Minimal string class with std::string like behaviour that fulfills the JavaCC
Packit 1c1d7e
 *  string requirements.
Packit 1c1d7e
 */
Packit 1c1d7e
class VhdlString
Packit 1c1d7e
{
Packit 1c1d7e
  public:
Packit 1c1d7e
    VhdlString()
Packit 1c1d7e
    {
Packit 1c1d7e
      init();
Packit 1c1d7e
    }
Packit 1c1d7e
    VhdlString(const VhdlString &other)
Packit 1c1d7e
    {
Packit 1c1d7e
      m_str = (char*)malloc(other.m_len+1);
Packit 1c1d7e
      memcpy(m_str,other.m_str,other.m_len);
Packit 1c1d7e
      m_len = other.m_len;
Packit 1c1d7e
      m_str[m_len]=0;
Packit 1c1d7e
    }
Packit 1c1d7e
    VhdlString &operator=(const VhdlString &other)
Packit 1c1d7e
    {
Packit 1c1d7e
      if (this!=&other)
Packit 1c1d7e
      {
Packit 1c1d7e
        free(m_str);
Packit 1c1d7e
        m_str = (char*)malloc(other.m_len+1);
Packit 1c1d7e
        memcpy(m_str,other.m_str,other.m_len);
Packit 1c1d7e
        m_len = other.m_len;
Packit 1c1d7e
        m_str[m_len]=0;
Packit 1c1d7e
      }
Packit 1c1d7e
      return *this;
Packit 1c1d7e
    }
Packit 1c1d7e
    VhdlString(const char *s)
Packit 1c1d7e
    {
Packit 1c1d7e
      m_len = strlen(s);
Packit 1c1d7e
      m_str=(char*)malloc(m_len+1);
Packit 1c1d7e
      memcpy(m_str,s,m_len+1);
Packit 1c1d7e
    }
Packit 1c1d7e
    VhdlString(const char *s,int size)
Packit 1c1d7e
    {
Packit 1c1d7e
      m_str = (char*)malloc(size+1);
Packit 1c1d7e
      memcpy(m_str,s,size);
Packit 1c1d7e
      m_str[size]=0;
Packit 1c1d7e
      m_len=size;
Packit 1c1d7e
    }
Packit 1c1d7e
   ~VhdlString()
Packit 1c1d7e
    {
Packit 1c1d7e
      free(m_str);
Packit 1c1d7e
    }
Packit 1c1d7e
    VhdlString& append(const char *s,int size)
Packit 1c1d7e
    {
Packit 1c1d7e
      int oldlen = m_len;
Packit 1c1d7e
      m_len+=size+1;
Packit 1c1d7e
      if (m_len)
Packit 1c1d7e
      {
Packit 1c1d7e
        m_str = (char*)realloc(m_str,m_len);
Packit 1c1d7e
        memcpy(m_str+oldlen,s,m_len-oldlen-1);
Packit 1c1d7e
        m_str[m_len-1]=0;
Packit 1c1d7e
      }
Packit 1c1d7e
      return *this;
Packit 1c1d7e
    }
Packit 1c1d7e
    VhdlString& append(const char *s)
Packit 1c1d7e
    {
Packit 1c1d7e
      return append(s,strlen(s));
Packit 1c1d7e
    }
Packit 1c1d7e
    VhdlString& append(const VhdlString &other)
Packit 1c1d7e
    {
Packit 1c1d7e
      return append(other.m_str,other.m_len);
Packit 1c1d7e
    }
Packit 1c1d7e
    VhdlString substr(int pos=0,int len=-1)
Packit 1c1d7e
    {
Packit 1c1d7e
      return VhdlString(m_str?m_str+pos:0,len==-1?m_len-pos:m_len);
Packit 1c1d7e
    }
Packit 1c1d7e
    int copy(char *s,int len,int pos=0) const
Packit 1c1d7e
    {
Packit 1c1d7e
      if (len==0) return 0;
Packit 1c1d7e
      if (pos>=m_len) { s[0]=0; return 0; }
Packit 1c1d7e
      int r=m_len
Packit 1c1d7e
      memcpy(s,m_str+pos,r);
Packit 1c1d7e
      return r;
Packit 1c1d7e
    }
Packit 1c1d7e
    const char *c_str() const           { return m_str; }
Packit 1c1d7e
    const char *data() const            { return m_str; }
Packit 1c1d7e
    int         size() const            { return m_len; }
Packit 1c1d7e
    int         length() const          { return m_len; }
Packit 1c1d7e
    char &      operator[](int i)       { return m_str[i]; }
Packit 1c1d7e
    const char &operator[](int i) const { return m_str[i]; }
Packit 1c1d7e
    void        clear()                 { free(m_str); init(); }
Packit 1c1d7e
    VhdlString  &operator+=(char c)      { char s[2]; s[0]=c; s[1]=0; return append(s); }
Packit 1c1d7e
    VhdlString  &operator+=(const char *s) { return append(s); }
Packit 1c1d7e
Packit 1c1d7e
  private:
Packit 1c1d7e
    void init() { m_str=(char*)calloc(1,1); m_len=0; }
Packit 1c1d7e
    char *m_str;
Packit 1c1d7e
    int   m_len;
Packit 1c1d7e
};
Packit 1c1d7e
Packit 1c1d7e
#endif