On Thu, 3 Jun 2004 lahiru@opensource.lk wrote:
> hi all,
>
> Please have a look at the Astring class implemented below and provide
> your comments. The files are attached with this mail.
First I suggest that either you do not use tabs, or set them
to 8.
1 #if !defined(_ASTRING_H____OF_AXIS_INCLUDED_)
2 #define _ASTRING_H____OF_AXIS_INCLUDED_
Macros beginning with underscore and an uppercase letter are
reserved.
3
4 #include <string.h>
5
6
7 class Astring
8 {
9
10
11 public:
12 static const int npos;
For static const integral types, you are allowed to write
"static const int npos=-1;". Slightly more efficient.
13 Astring();
14 Astring(const char* pcCharacter);
15 Astring(const char ccValue);
16 ~Astring();
17
18 bool operator ==(const Astring & AstrParam);
19 bool operator ==(const char * cpRightVal);
20 void operator =(const char* pcParam);
21 void operator =(const Astring& objAstring);
22 void operator =(const char cValue);
23 const char* c_str() const;
24 // Astring operator +(const Astring & rAstrRight);
25 // Astring operator +(const char * cpValue);
26 friend Astring operator+(Astring & rAstrRight,const char * cpValue);
27 friend Astring operator+(char * cpValue,const Astring & rAstrRight);
Typo. I think you mean "const char *".
28 friend Astring operator+(Astring & rAstrLeft,Astring & rAstrRight);
29 Astring substr(int pos=0,int inelements=npos)const;
30 int length()const;
31 int size()const;
32 void operator +=(const Astring & rAstrRight);
33 void operator +=(const char * ccpRightchar);
34 void operator +=(const char ccValue);
35 bool empty() const;
36 int find(Astring & rAstrRight,int istart=0)const;
37 int find(const char cFind,int istrat=0)const;
38 int find(const char* ccpFind,int istart=0)const;
39 int find_first_of(Astring & rAstrRight,int istart=0)const;
40 int find_first_of(const char* cpValue,int istart=0)const;
41 int rfind(char * cpValue,int ilastval=npos)const;
Typo. I think you mean "const char *".
42 int rfind(const char cValue,int ilastval=npos)const;
43 char at(int ipos)const;
44 Astring& append(const char * cpValue);
45 Astring& append(Astring & rAstrRight);
46 char & operator [](int iIndex);
47
48 /*
49 friend Astring operator+(Astring & rAstrRight,const char * cpValue)
50 {
51 Astring * AstrTemp=new Astring(rAstrRight.m_pcValue);
52 *AstrTemp += cpValue;
53 return *AstrTemp;
54 }
Memory leak. You can put AstrTemp on the stack.
55
56 friend Astring operator+(char * cpValue,const Astring & rAstrRight)
57 {
58 Astring * AstrTemp=new Astring(rAstrRight.m_pcValue);
59 *AstrTemp += cpValue;
60 return *AstrTemp;
61 }
Memory leak.
62
63 friend Astring operator+(Astring & rAstrLeft,Astring & rAstrRight)
64 { Astring * AstrTemp=new Astring(rAstrLeft.m_pcValue);
65 *AstrTemp += rAstrRight;
66 return *AstrTemp;
67 }
Same memory leak.
68 */
69 private:
70 char* m_pcValue;
71
72 };
73
74
75
76 #endif
1 #include <malloc.h>
2 #include <stdlib.h>
3 #include <iostream.h>
The <iostream.h> header file is deprecated. Use <iostream>
instead.
4 #include "Astring.h"
5
6 const int Astring::npos=-1;
7
8 /*
9 * Constructor
10 */
11 Astring::Astring() {
12 m_pcValue = '\0';
13 }
14
15 Astring::Astring(const char* pcCharacter)
16 {
17 if(pcCharacter=="")
I don't think this comparison does what you want it to do.
You are comparing two addresses, not two strings.
18 m_pcValue = '\0';
19 else
20 {
21 m_pcValue = (char*)malloc(strlen(pcCharacter)+1);
22 strcpy(m_pcValue, pcCharacter);
23 }
24 }
25
26 Astring::Astring(const char ccValue)
27 {
28 m_pcValue=(char*)malloc(2);
29 *m_pcValue=ccValue;
30 *(m_pcValue+1)='\0';
31 }
32
33 Astring::~Astring() {
34 if (m_pcValue)
35 {
36 free(m_pcValue);
37 m_pcValue = NULL;
38 }
39 }
40
41 bool Astring::operator ==(const Astring &AstrParam)
42 {
43
44 int result=strcmp(m_pcValue ,AstrParam.m_pcValue );
45 if(result==0)
46 {
47 return true;
48 }
49 else
50 {
51 return false;
52 }
Suggest replacing with:
return strcmp(m_pcValue ,AstrParam.m_pcValue ) == 0;
53
54 }
55
56 bool Astring::operator ==(const char * cpRightVal)
57 {
58 int result=strcmp(m_pcValue ,cpRightVal );
59 if(result==0)
60 {
61 return true;
62 }
63 else
64 {
65 return false;
66 }
67
68
69 }
70
71
72 void Astring::operator = (const char* pcParam)
73 {
74 m_pcValue = (char*)malloc(strlen(pcParam)+1);
75 strcpy(m_pcValue, pcParam);
76 }
Memory leak. Need to check previous value of m_pcValue.
Same for below.
77
78 void Astring::operator = (const Astring& objAstring)
79 {
80 m_pcValue = (char*)malloc(strlen(objAstring.m_pcValue)+1);
81 strcpy(m_pcValue, objAstring.m_pcValue);
82
83 }
84
85 void Astring::operator =(const char cValue)
86 {
87 m_pcValue=(char*)malloc(2);
88 *m_pcValue=cValue;
89 *(m_pcValue+1)='\0';
90 }
I'll look at the rest later if I have time.
>
> rgds,
> Lahiru
>
> > At 10:13 AM 5/6/2004 +0530, you wrote:
> >>What I would suggest -
> >>1. expose a small set of classes and make the interface stl-free
> >>2. in your internal classes you can still use stl
> >>
> >>Why do you want to remove stl all-together?
> >>
> >> > -----Original Message-----
> >> > From: Kenneth Chiu [mailto:chiuk@cs.indiana.edu]
> >> > Sent: Thursday, May 06, 2004 9:41 AM
> >> > To: Apache AXIS C Developers List
> >
>
|