root/trunk/AprSharp/dev/src/AprString.cs

Revision 66, 4.5 kB (checked in by DenisG, 3 years ago)

[AprSharp] Fix an implicit cast from void pointer

  • Property svn:eol-style set to native
Line 
1 //  AprSharp, a wrapper library around the Apache Portable Runtime Library
2 #region Copyright (C) 2004 SOFTEC sa.
3 //
4 //  This library is free software; you can redistribute it and/or
5 //  modify it under the terms of the GNU Lesser General Public
6 //  License as published by the Free Software Foundation; either
7 //  version 2.1 of the License, or (at your option) any later version.
8 //
9 //  This library is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 //  Lesser General Public License for more details.
13 //
14 //  You should have received a copy of the GNU Lesser General Public
15 //  License along with this library; if not, write to the Free Software
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 //
18 //  Sources, support options and lastest version of the complete library
19 //  is available from:
20 //              http://www.softec.st/AprSharp
21 //             
22 //
23 //  Initial authors :
24 //              Denis Gervalle
25 //              Olivier Desaive
26 #endregion
27 //
28 using System;
29 using System.Diagnostics;
30 using System.Runtime.InteropServices;
31
32 namespace Softec.AprSharp
33 {
34     public struct AprString : IAprUnmanaged
35     {
36         private IntPtr mString;
37
38         #region Generic embedding functions of an IntPtr
39         public AprString(IntPtr ptr)
40         {
41             mString = ptr;
42         }
43
44         public AprString(string str, AprPool pool)
45         {
46             mString = Apr.apr_pstrdup(pool, str);
47         }
48        
49         public AprString(AprString str, AprPool pool)
50         {
51             mString = Apr.apr_pstrdup(pool, str);
52         }
53
54         public AprString(string str, int size, AprPool pool)
55         {
56             mString = Apr.apr_pstrndup(pool, str, unchecked((uint)size));
57         }
58        
59         public AprString(AprString str, int size, AprPool pool)
60         {
61             mString = Apr.apr_pstrndup(pool, str, unchecked((uint)size));
62         }
63
64         [CLSCompliant(false)]
65         public AprString(string str, uint size, AprPool pool)
66         {
67             mString = Apr.apr_pstrndup(pool, str, size);
68         }
69
70         [CLSCompliant(false)]
71         public AprString(AprString str, uint size, AprPool pool)
72         {
73             mString = Apr.apr_pstrndup(pool, str, size);
74         }
75
76         public bool IsNull
77         {
78                 get
79                 {
80                 return( mString == IntPtr.Zero );
81             }
82         }
83
84         private void CheckPtr()
85         {
86             if( IsNull )
87                 throw new AprNullReferenceException();
88         }
89
90         public void ClearPtr()
91         {
92             mString = IntPtr.Zero;
93         }
94
95         public IntPtr ToIntPtr()
96         {
97             return mString;
98         }
99
100                 public bool ReferenceEquals(IAprUnmanaged obj)
101                 {
102                         return(obj.ToIntPtr() == ToIntPtr());
103                 }
104                
105         public static implicit operator IntPtr(AprString str)
106         {
107             return str.mString;
108         }
109        
110         public static implicit operator AprString(IntPtr ptr)
111         {
112             return new AprString(ptr);
113         }
114
115         public override string ToString()
116         {
117                 if( IsNull )
118                         return("[apr_string:NULL]");
119                 else
120                 return(Marshal.PtrToStringAnsi(mString));
121         }
122         #endregion
123        
124         #region Methods wrappers
125         public static AprString Duplicate(AprPool pool, string str)
126         {
127             return(new AprString(str, pool));
128         }
129
130         public static AprString Duplicate(AprPool pool, AprString str)
131         {
132             return(new AprString(str, pool));
133         }
134
135         public static AprString Duplicate(AprPool pool, string str, int size)
136         {
137             return(new AprString(str, size, pool));
138         }
139
140         [CLSCompliant(false)]
141         public static AprString Duplicate(AprPool pool, string str, uint size)
142         {
143             return(new AprString(str, size, pool));
144         }
145        
146         public static AprString Duplicate(AprPool pool, AprString str, int size)
147         {
148             return(new AprString(str, size, pool));
149         }
150        
151         [CLSCompliant(false)]
152         public static AprString Duplicate(AprPool pool, AprString str, uint size)
153         {
154             return(new AprString(str, size, pool));
155         }
156        
157         public unsafe int Length {
158                 get {
159                         byte *p = (byte *)mString.ToPointer();
160                         while(*p++ != 0);
161                         return(unchecked((int)((uint)p-(uint)mString.ToPointer()-1)));
162                         }
163         }
164         #endregion
165     }
166 }
Note: See TracBrowser for help on using the browser.