/*************************************************************************** * * CurlS#arp * * Copyright (c) 2013-2017 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; using CurlSharp.Enums; 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 { /// /// 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; } } internal IntPtr Handle { get; private set; } /// /// Free all internal strings. /// public void Dispose() { GC.SuppressFinalize(this); Dispose(true); } /// /// 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 } 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; } } } #if !USE_LIBCURLSHIM [StructLayout(LayoutKind.Sequential)] private class curl_slist { /// char* [MarshalAs(UnmanagedType.LPStr)] public string data; /// curl_slist* public IntPtr next; } #endif } }