/*************************************************************************** * * CurlS#arp * * Copyright (c) 2013 Dr. Masroor Ehsan (masroore@gmail.com) * Portions copyright (c) 2004, 2005 Jeff Phillips (jeff@jeffp.net) * * This software is licensed as described in the file LICENSE, which you * should have received as part of this distribution. * * You may opt to use, copy, modify, merge, publish, distribute and/or sell * copies of this Software, and permit persons to whom the Software is * furnished to do so, under the terms of the LICENSE file. * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF * ANY KIND, either express or implied. * **************************************************************************/ using System; using System.Collections.Generic; using System.Runtime.InteropServices; namespace CurlSharp { /// /// This class wraps a linked list of strings used in cURL. Use it /// to build string lists where they're required, such as when calling /// with /// as the option. /// public class CurlSlist : IDisposable { #if !USE_LIBCURLSHIM [StructLayout(LayoutKind.Sequential)] private class curl_slist { /// char* [MarshalAs(UnmanagedType.LPStr)] public string data; /// curl_slist* public IntPtr next; } #endif private IntPtr _handle; /// /// Constructor /// /// /// This is thrown /// if hasn't bee properly initialized. /// public CurlSlist() { Curl.EnsureCurl(); _handle = IntPtr.Zero; } public CurlSlist(IntPtr handle) { _handle = handle; } /// /// Read-only copy of the strings stored in the SList /// public List Strings { get { if (_handle == IntPtr.Zero) return null; var strings = new List(); #if !USE_LIBCURLSHIM var slist = new curl_slist(); Marshal.PtrToStructure(_handle, slist); while (true) { strings.Add(slist.data); if (slist.next != IntPtr.Zero) Marshal.PtrToStructure(slist.next, slist); else break; } #endif return strings; } } /// /// Destructor /// ~CurlSlist() { Dispose(false); } /// /// Append a string to the list. /// /// The string to append. public void Append(string str) { #if USE_LIBCURLSHIM _handle = NativeMethods.curl_shim_add_string_to_slist(_handle, str); #else _handle = NativeMethods.curl_slist_append(_handle, str); #endif } /// /// Free all internal strings. /// public void Dispose() { GC.SuppressFinalize(this); Dispose(true); } internal IntPtr Handle { get { return _handle; } } private void Dispose(bool disposing) { lock (this) { if (_handle != IntPtr.Zero) { #if USE_LIBCURLSHIM NativeMethods.curl_shim_free_slist(_handle); #else NativeMethods.curl_slist_free_all(_handle); #endif _handle = IntPtr.Zero; } } } } }