From 7496650cc63a9f0c813ba9387b4addb44b16d4da Mon Sep 17 00:00:00 2001 From: Martin Michlmayr Date: Tue, 28 Apr 2020 09:23:23 +0800 Subject: Remove contrib/ParseCcStmt This tool has limited usefulness and depends on CSVReader.cs whose license is not 100% clear. Fixes #1758 --- contrib/CSVReader.cs | 165 -------------------------------------------- contrib/Makefile | 4 -- contrib/ParseCcStmt.cs | 184 ------------------------------------------------- 3 files changed, 353 deletions(-) delete mode 100644 contrib/CSVReader.cs delete mode 100644 contrib/Makefile delete mode 100644 contrib/ParseCcStmt.cs (limited to 'contrib') diff --git a/contrib/CSVReader.cs b/contrib/CSVReader.cs deleted file mode 100644 index a22eab06..00000000 --- a/contrib/CSVReader.cs +++ /dev/null @@ -1,165 +0,0 @@ -// This code is in the public domain. I can't remember where I found it on the Web, but it -// didn't come with any license. - -using System; -using System.Collections; -using System.IO; -using System.Text; - -namespace CSVReader { - - /// - /// A data-reader style interface for reading CSV files. - /// - public class CSVReader : IDisposable { - - #region Private variables - - private Stream stream; - private StreamReader reader; - - #endregion - - /// - /// Create a new reader for the given stream. - /// - /// The stream to read the CSV from. - public CSVReader(Stream s) : this(s, null) { } - - /// - /// Create a new reader for the given stream and encoding. - /// - /// The stream to read the CSV from. - /// The encoding used. - public CSVReader(Stream s, Encoding enc) { - - this.stream = s; - if (!s.CanRead) { - throw new CSVReaderException("Could not read the given CSV stream!"); - } - reader = (enc != null) ? new StreamReader(s, enc) : new StreamReader(s); - } - - /// - /// Creates a new reader for the given text file path. - /// - /// The name of the file to be read. - public CSVReader(string filename) : this(filename, null) { } - - /// - /// Creates a new reader for the given text file path and encoding. - /// - /// The name of the file to be read. - /// The encoding used. - public CSVReader(string filename, Encoding enc) - : this(new FileStream(filename, FileMode.Open), enc) { } - - /// - /// Returns the fields for the next row of CSV data (or null if at eof) - /// - /// A string array of fields or null if at the end of file. - public string[] GetCSVLine() { - - string data = reader.ReadLine(); - if (data == null) return null; - if (data.Length == 0) return new string[0]; - - ArrayList result = new ArrayList(); - - ParseCSVFields(result, data); - - return (string[])result.ToArray(typeof(string)); - } - - // Parses the CSV fields and pushes the fields into the result arraylist - private void ParseCSVFields(ArrayList result, string data) { - - int pos = -1; - while (pos < data.Length) - result.Add(ParseCSVField(data, ref pos)); - } - - // Parses the field at the given position of the data, modified pos to match - // the first unparsed position and returns the parsed field - private string ParseCSVField(string data, ref int startSeparatorPosition) { - - if (startSeparatorPosition == data.Length-1) { - startSeparatorPosition++; - // The last field is empty - return ""; - } - - int fromPos = startSeparatorPosition + 1; - - // Determine if this is a quoted field - if (data[fromPos] == '"') { - // If we're at the end of the string, let's consider this a field that - // only contains the quote - if (fromPos == data.Length-1) { - fromPos++; - return "\""; - } - - // Otherwise, return a string of appropriate length with double quotes collapsed - // Note that FSQ returns data.Length if no single quote was found - int nextSingleQuote = FindSingleQuote(data, fromPos+1); - startSeparatorPosition = nextSingleQuote+1; - return data.Substring(fromPos+1, nextSingleQuote-fromPos-1).Replace("\"\"", "\""); - } - - // The field ends in the next comma or EOL - int nextComma = data.IndexOf(',', fromPos); - if (nextComma == -1) { - startSeparatorPosition = data.Length; - return data.Substring(fromPos); - } - else { - startSeparatorPosition = nextComma; - return data.Substring(fromPos, nextComma-fromPos); - } - } - - // Returns the index of the next single quote mark in the string - // (starting from startFrom) - private int FindSingleQuote(string data, int startFrom) { - - int i = startFrom-1; - while (++i < data.Length) - if (data[i] == '"') { - // If this is a double quote, bypass the chars - if (i < data.Length-1 && data[i+1] == '"') { - i++; - continue; - } - else - return i; - } - // If no quote found, return the end value of i (data.Length) - return i; - } - - /// - /// Disposes the CSVReader. The underlying stream is closed. - /// - public void Dispose() { - // Closing the reader closes the underlying stream, too - if (reader != null) reader.Close(); - else if (stream != null) - stream.Close(); // In case we failed before the reader was constructed - GC.SuppressFinalize(this); - } - } - - - /// - /// Exception class for CSVReader exceptions. - /// - public class CSVReaderException : ApplicationException { - - /// - /// Constructs a new exception object with the given message. - /// - /// The exception message. - public CSVReaderException(string message) : base(message) { } - } -} diff --git a/contrib/Makefile b/contrib/Makefile deleted file mode 100644 index 6e4d367a..00000000 --- a/contrib/Makefile +++ /dev/null @@ -1,4 +0,0 @@ -all: ParseCcStmt.exe - -ParseCcStmt.exe: ParseCcStmt.cs CSVReader.cs - gmcs -out:ParseCcStmt.exe ParseCcStmt.cs CSVReader.cs diff --git a/contrib/ParseCcStmt.cs b/contrib/ParseCcStmt.cs deleted file mode 100644 index 52fc6f64..00000000 --- a/contrib/ParseCcStmt.cs +++ /dev/null @@ -1,184 +0,0 @@ -/* - * Copyright (c) 2003-2018, John Wiegley. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of New Artisans LLC nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Text.RegularExpressions; - -using CSVReader; - -/** - * @file ParseCcStmt.cs - * - * @brief Provides a .NET way to turn a CSV report into Ledger transactions. - * - * I use this code for converting the statements from my own credit card - * issuer. I realize it's strange for this to be in C#, but I wrote it - * during a phase of C# contracting. The code is solid enough now -- - * and the Mono project is portable enough -- that I haven't seen the - * need to rewrite it into another language like Python. - */ - -namespace JohnWiegley -{ - public class Posting - { - public DateTime Date; - public DateTime PostedDate; - public string Code; - public string Payee; - public Decimal Amount; - } - - public interface IStatementConverter - { - List ConvertRecords(Stream s); - } - - public class ConvertGoldMasterCardStatement : IStatementConverter - { - public List ConvertRecords(Stream s) - { - List posts = new List(); - - using (CSVReader.CSVReader csv = new CSVReader.CSVReader(s)) { - string[] fields; - while ((fields = csv.GetCSVLine()) != null) { - if (fields[0] == "POSTING DATE") - continue; - - Posting post = new Posting(); - - post.Date = DateTime.ParseEpost(fields[0], "mm/dd/yy", null); - post.PostedDate = DateTime.ParseEpost(fields[1], "mm/dd/yy", null); - post.Payee = fields[2].Trim(); - post.Code = fields[3].Trim(); - post.Amount = Convert.ToDecimal(fields[4].Trim()); - - if (post.Code.Length == 0) - post.Code = null; - - posts.Add(post); - } - } - return posts; - } - } - - public class ConvertMastercardStatement : IStatementConverter - { - public List ConvertRecords(Stream s) - { - List posts = new List(); - - using (CSVReader.CSVReader csv = new CSVReader.CSVReader(s)) { - string[] fields; - while ((fields = csv.GetCSVLine()) != null) { - Posting post = new Posting(); - - post.Date = DateTime.ParseEpost(fields[0], "m/dd/yyyy", null); - post.Payee = fields[2].Trim(); - post.Code = fields[3].Trim(); - post.Amount = - Convert.ToDecimal(fields[4].Trim()); - - if (post.Code.Length == 0) - post.Code = null; - - posts.Add(post); - } - } - return posts; - } - } - - public class PrintPostings - { - public string DefaultAccount(Posting post) { - if (Regex.IsMatch(post.Payee, "IGA")) - return "Expenses:Food"; - return "Expenses:Food"; - } - - public void Print(string AccountName, string PayAccountName, - List posts) - { - foreach (Posting post in posts) { - if (post.Amount < 0) { - Console.WriteLine("{0} * {1}{2}", post.Date.ToString("yyyy/mm/dd"), - post.Code != null ? "(" + post.Code + ") " : "", - post.Payee); - Console.WriteLine(" {0,-36}{1,12}", AccountName, - "$" + (- post.Amount).ToString()); - Console.WriteLine(" {0}", PayAccountName); - } else { - Console.WriteLine("{0} {1}{2}", post.Date.ToString("yyyy/mm/dd"), - post.Code != null ? "(" + post.Code + ") " : "", - post.Payee); - Console.WriteLine(" {0,-36}{1,12}", DefaultAccount(post), - "$" + post.Amount.ToString()); - Console.WriteLine(" * {0}", AccountName); - } - Console.WriteLine(); - } - } - } - - public class ParseCcStmt - { - public static int Main(string[] args) - { - StreamReader reader = new StreamReader(args[0]); - string firstLine = reader.ReadLine(); - - string CardAccount = args[1]; - string BankAccount = args[2]; - - IStatementConverter converter; - - if (firstLine.StartsWith("POSTING DATE")) { - converter = new ConvertGoldMasterCardStatement(); - } else { - converter = new ConvertMastercardStatement(); - } - - reader = new StreamReader(args[0]); - List posts = converter.ConvertRecords(reader.BaseStream); - - PrintPostings printer = new PrintPostings(); - printer.Print(CardAccount, BankAccount, posts); - - return 0; - } - } -} -- cgit v1.2.3