From e2f709add051f432af7b0f59b5297d085bfc67dd Mon Sep 17 00:00:00 2001 From: Eugene Wang <8755753+soukoku@users.noreply.github.com> Date: Wed, 21 Apr 2021 13:04:22 -0400 Subject: [PATCH] Moved CSV to own file. --- NTwain/TWAINWorkingGroup/CSV.cs | 217 ++++++++++++++++++++++++++++++ NTwain/TWAINWorkingGroup/TWAIN.cs | 174 +----------------------- 2 files changed, 219 insertions(+), 172 deletions(-) create mode 100644 NTwain/TWAINWorkingGroup/CSV.cs diff --git a/NTwain/TWAINWorkingGroup/CSV.cs b/NTwain/TWAINWorkingGroup/CSV.cs new file mode 100644 index 0000000..cd0c530 --- /dev/null +++ b/NTwain/TWAINWorkingGroup/CSV.cs @@ -0,0 +1,217 @@ +/////////////////////////////////////////////////////////////////////////////////////// +// +// TwainWorkingGroup.TWAIN +// +// This is a wrapper class for basic TWAIN functionality. It establishes +// behavior that every application should adhere to. It also hides OS +// specific details, so that toolkits or applications can use one unified +// interface to TWAIN. +// +/////////////////////////////////////////////////////////////////////////////////////// +// Author Date TWAIN Comment +// M.McLaughlin 13-Mar-2019 2.4.0.3 Add language code page support for strings +// M.McLaughlin 13-Nov-2015 2.4.0.0 Updated to latest spec +// M.McLaughlin 13-Sep-2015 2.3.1.2 DsmMem bug fixes +// M.McLaughlin 26-Aug-2015 2.3.1.1 Log fix and sync with TWAIN Direct +// M.McLaughlin 13-Mar-2015 2.3.1.0 Numerous fixes +// M.McLaughlin 13-Oct-2014 2.3.0.4 Added logging +// M.McLaughlin 24-Jun-2014 2.3.0.3 Stability fixes +// M.McLaughlin 21-May-2014 2.3.0.2 64-Bit Linux +// M.McLaughlin 27-Feb-2014 2.3.0.1 AnyCPU support +// M.McLaughlin 21-Oct-2013 2.3.0.0 Initial Release +/////////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2013-2020 Kodak Alaris Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +/////////////////////////////////////////////////////////////////////////////////////// + +using System; + +namespace TWAINWorkingGroup +{ + /// + /// A quick and dirty CSV reader/writer... + /// + public class CSV + { + /////////////////////////////////////////////////////////////////////////////// + // Public Functions... + /////////////////////////////////////////////////////////////////////////////// + #region Public Functions... + + /// + /// Start with an empty string... + /// + public CSV() + { + m_szCsv = ""; + } + + /// + /// Add an item to a CSV string... + /// + /// Something to add to the CSV string + public void Add(string a_szItem) + { + // If the item has commas, we need to do work... + if (a_szItem.Contains(",")) + { + // If the item has quotes, replace them with paired quotes, then + // quote it and add it... + if (a_szItem.Contains("\"")) + { + m_szCsv += ((m_szCsv != "") ? "," : "") + "\"" + a_szItem.Replace("\"", "\"\"") + "\""; + } + + // Otherwise, just quote it and add it... + else + { + m_szCsv += ((m_szCsv != "") ? "," : "") + "\"" + a_szItem + "\""; + } + } + + // If the item has quotes, replace them with escaped quotes, then + // quote it and add it... + else if (a_szItem.Contains("\"")) + { + m_szCsv += ((m_szCsv != "") ? "," : "") + "\"" + a_szItem.Replace("\"", "\"\"") + "\""; + } + + // Otherwise, just add it... + else + { + m_szCsv += ((m_szCsv != "") ? "," : "") + a_szItem; + } + } + + /// + /// Clear the record... + /// + public void Clear() + { + m_szCsv = ""; + } + + /// + /// Get the current CSV string... + /// + /// The current value of the CSV string + public string Get() + { + return (m_szCsv); + } + + /// + /// Parse a CSV string... + /// + /// A CSV string to parse + /// An array if items (some can be CSV themselves) + public static string[] Parse(string a_szCsv) + { + int ii; + bool blEnd; + string[] aszCsv; + string[] aszLeft; + string[] aszRight; + + // Validate... + if ((a_szCsv == null) || (a_szCsv == "")) + { + return (new string[] { "" }); + } + + // If there are no quotes, then parse it fast... + if (!a_szCsv.Contains("\"")) + { + return (a_szCsv.Split(new char[] { ',' })); + } + + // There's no opening quote, so split and recurse... + if (a_szCsv[0] != '"') + { + aszLeft = new string[] { a_szCsv.Substring(0, a_szCsv.IndexOf(',')) }; + aszRight = Parse(a_szCsv.Remove(0, a_szCsv.IndexOf(',') + 1)); + aszCsv = new string[aszLeft.Length + aszRight.Length]; + aszLeft.CopyTo(aszCsv, 0); + aszRight.CopyTo(aszCsv, aszLeft.Length); + return (aszCsv); + } + + // Handle the quoted string... + else + { + // Find the terminating quote... + blEnd = true; + for (ii = 0; ii < a_szCsv.Length; ii++) + { + if (a_szCsv[ii] == '"') + { + blEnd = !blEnd; + } + else if (blEnd && (a_szCsv[ii] == ',')) + { + break; + } + } + ii -= 1; + + // We have a problem... + if (!blEnd) + { + throw new Exception("Error in CSV string..."); + } + + // This is the last item, remove any escaped quotes and return it... + if (((ii + 1) >= a_szCsv.Length)) + { + return (new string[] { a_szCsv.Substring(1, a_szCsv.Length - 2).Replace("\"\"", "\"") }); + } + + // We have more data... + if (a_szCsv[ii + 1] == ',') + { + aszLeft = new string[] { a_szCsv.Substring(1, ii - 1).Replace("\"\"", "\"") }; + aszRight = Parse(a_szCsv.Remove(0, ii + 2)); + aszCsv = new string[aszLeft.Length + aszRight.Length]; + aszLeft.CopyTo(aszCsv, 0); + aszRight.CopyTo(aszCsv, aszLeft.Length); + return (aszCsv); + } + + // We have a problem... + throw new Exception("Error in CSV string..."); + } + } + + #endregion + + + /////////////////////////////////////////////////////////////////////////////// + // Private Attributes... + /////////////////////////////////////////////////////////////////////////////// + #region Private Attributes... + + /// + /// Our working string for creating or parsing... + /// + private string m_szCsv; + + #endregion + } +} diff --git a/NTwain/TWAINWorkingGroup/TWAIN.cs b/NTwain/TWAINWorkingGroup/TWAIN.cs index 7989831..6afd130 100644 --- a/NTwain/TWAINWorkingGroup/TWAIN.cs +++ b/NTwain/TWAINWorkingGroup/TWAIN.cs @@ -208,6 +208,8 @@ namespace TWAINWorkingGroup m_autoreseteventThreadStarted = new AutoResetEvent(false); m_lockTwain = new Object(); + ms_platform = PlatformTools.GetPlatform(); + // Windows only... if (ms_platform == Platform.WINDOWS) { @@ -14318,8 +14320,6 @@ namespace TWAINWorkingGroup /// The platform we're running on... /// private static Platform ms_platform; - private static Processor ms_processor; - private static bool ms_blFirstPassGetPlatform = true; /// /// Delegates for DAT_CALLBACK... @@ -14635,174 +14635,4 @@ namespace TWAINWorkingGroup #endregion } - - /// - /// A quick and dirty CSV reader/writer... - /// - public class CSV - { - /////////////////////////////////////////////////////////////////////////////// - // Public Functions... - /////////////////////////////////////////////////////////////////////////////// - #region Public Functions... - - /// - /// Start with an empty string... - /// - public CSV() - { - m_szCsv = ""; - } - - /// - /// Add an item to a CSV string... - /// - /// Something to add to the CSV string - public void Add(string a_szItem) - { - // If the item has commas, we need to do work... - if (a_szItem.Contains(",")) - { - // If the item has quotes, replace them with paired quotes, then - // quote it and add it... - if (a_szItem.Contains("\"")) - { - m_szCsv += ((m_szCsv != "") ? "," : "") + "\"" + a_szItem.Replace("\"", "\"\"") + "\""; - } - - // Otherwise, just quote it and add it... - else - { - m_szCsv += ((m_szCsv != "") ? "," : "") + "\"" + a_szItem + "\""; - } - } - - // If the item has quotes, replace them with escaped quotes, then - // quote it and add it... - else if (a_szItem.Contains("\"")) - { - m_szCsv += ((m_szCsv != "") ? "," : "") + "\"" + a_szItem.Replace("\"", "\"\"") + "\""; - } - - // Otherwise, just add it... - else - { - m_szCsv += ((m_szCsv != "") ? "," : "") + a_szItem; - } - } - - /// - /// Clear the record... - /// - public void Clear() - { - m_szCsv = ""; - } - - /// - /// Get the current CSV string... - /// - /// The current value of the CSV string - public string Get() - { - return (m_szCsv); - } - - /// - /// Parse a CSV string... - /// - /// A CSV string to parse - /// An array if items (some can be CSV themselves) - public static string[] Parse(string a_szCsv) - { - int ii; - bool blEnd; - string[] aszCsv; - string[] aszLeft; - string[] aszRight; - - // Validate... - if ((a_szCsv == null) || (a_szCsv == "")) - { - return (new string[] { "" }); - } - - // If there are no quotes, then parse it fast... - if (!a_szCsv.Contains("\"")) - { - return (a_szCsv.Split(new char[] { ',' })); - } - - // There's no opening quote, so split and recurse... - if (a_szCsv[0] != '"') - { - aszLeft = new string[] { a_szCsv.Substring(0, a_szCsv.IndexOf(',')) }; - aszRight = Parse(a_szCsv.Remove(0, a_szCsv.IndexOf(',') + 1)); - aszCsv = new string[aszLeft.Length + aszRight.Length]; - aszLeft.CopyTo(aszCsv, 0); - aszRight.CopyTo(aszCsv, aszLeft.Length); - return (aszCsv); - } - - // Handle the quoted string... - else - { - // Find the terminating quote... - blEnd = true; - for (ii = 0; ii < a_szCsv.Length; ii++) - { - if (a_szCsv[ii] == '"') - { - blEnd = !blEnd; - } - else if (blEnd && (a_szCsv[ii] == ',')) - { - break; - } - } - ii -= 1; - - // We have a problem... - if (!blEnd) - { - throw new Exception("Error in CSV string..."); - } - - // This is the last item, remove any escaped quotes and return it... - if (((ii + 1) >= a_szCsv.Length)) - { - return (new string[] { a_szCsv.Substring(1, a_szCsv.Length - 2).Replace("\"\"", "\"") }); - } - - // We have more data... - if (a_szCsv[ii + 1] == ',') - { - aszLeft = new string[] { a_szCsv.Substring(1, ii - 1).Replace("\"\"", "\"") }; - aszRight = Parse(a_szCsv.Remove(0, ii + 2)); - aszCsv = new string[aszLeft.Length + aszRight.Length]; - aszLeft.CopyTo(aszCsv, 0); - aszRight.CopyTo(aszCsv, aszLeft.Length); - return (aszCsv); - } - - // We have a problem... - throw new Exception("Error in CSV string..."); - } - } - - #endregion - - - /////////////////////////////////////////////////////////////////////////////// - // Private Attributes... - /////////////////////////////////////////////////////////////////////////////// - #region Private Attributes... - - /// - /// Our working string for creating or parsing... - /// - private string m_szCsv; - - #endregion - } }