ZNC  trunk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pstring.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2014 ZNC, see the NOTICE file for details.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 class PString : public CString {
20  public:
21  enum EType {
23  INT,
25  NUM,
27  };
28 
29  PString() : CString() { m_eType = STRING; }
30  PString(const char* c) : CString(c) { m_eType = STRING; }
31  PString(const CString& s) : CString(s) { m_eType = STRING; }
32  PString(int i) : CString(i) { m_eType = INT; }
33  PString(u_int i) : CString(i) { m_eType = UINT; }
34  PString(long i) : CString(i) { m_eType = INT; }
35  PString(u_long i) : CString(i) { m_eType = UINT; }
36  PString(long long i) : CString(i) { m_eType = INT; }
37  PString(unsigned long long i) : CString(i) { m_eType = UINT; }
38  PString(double i) : CString(i) { m_eType = NUM; }
39  PString(bool b) : CString((b ? "1" : "0")) { m_eType = BOOL; }
40  PString(SV* sv) {
41  STRLEN len = SvCUR(sv);
42  char* c = SvPV(sv, len);
43  char* c2 = new char[len+1];
44  memcpy(c2, c, len);
45  c2[len] = 0;
46  *this = c2;
47  delete[] c2;
48  }
49 
50  virtual ~PString() {}
51 
52  EType GetType() const { return m_eType; }
53  void SetType(EType e) { m_eType = e; }
54 
55  SV* GetSV(bool bMakeMortal = true) const
56  {
57  SV* pSV = NULL;
58  switch (GetType()) {
59  case NUM:
60  pSV = newSVnv(ToDouble());
61  break;
62  case INT:
63  pSV = newSViv(ToLongLong());
64  break;
65  case UINT:
66  case BOOL:
67  pSV = newSVuv(ToULongLong());
68  break;
69  case STRING:
70  default:
71  pSV = newSVpv(data(), length());
72  break;
73  }
74 
75  if (bMakeMortal) {
76  pSV = sv_2mortal(pSV);
77  }
78 
79  return pSV;
80  }
81 
82  private:
83  EType m_eType;
84 };
85 
86 
PString(SV *sv)
Definition: pstring.h:40
PString(unsigned long long i)
Definition: pstring.h:37
unsigned long long ToULongLong() const
PString(const CString &s)
Definition: pstring.h:31
Definition: pstring.h:24
Definition: pstring.h:26
PString()
Definition: pstring.h:29
long long ToLongLong() const
PString(const char *c)
Definition: pstring.h:30
Definition: pstring.h:25
PString(double i)
Definition: pstring.h:38
void SetType(EType e)
Definition: pstring.h:53
double ToDouble() const
String class that is used inside ZNC.
Definition: ZNCString.h:67
PString(u_long i)
Definition: pstring.h:35
Definition: pstring.h:22
PString(int i)
Definition: pstring.h:32
Definition: pstring.h:19
SV * GetSV(bool bMakeMortal=true) const
Definition: pstring.h:55
EType
Definition: pstring.h:21
EType GetType() const
Definition: pstring.h:52
virtual ~PString()
Definition: pstring.h:50
PString(long i)
Definition: pstring.h:34
PString(long long i)
Definition: pstring.h:36
PString(u_int i)
Definition: pstring.h:33
PString(bool b)
Definition: pstring.h:39
Definition: pstring.h:23