Example #1
0
        /// <summary>
        /// Exports the specified instance properties to the provided stream.
        /// </summary>
        /// <param name="qif">The <seealso cref="T:QifDom"/> to export.</param>
        /// <param name="stream">Stream.</param>
        /// <param name="encoding">Encoding.</param>
        /// <remarks>This will overwrite an existing file.</remarks>
        public static void ExportStream(QifDom qif, Stream stream, Encoding encoding = null)
        {
            encoding = encoding ?? Encoding.UTF8;

            using (StreamWriter writer = new StreamWriter(stream, encoding, 512, true))
            {
                writer.AutoFlush = true;

                AccountListLogic.Export(writer, qif.AccountListTransactions);
                AssetLogic.Export(writer, qif.AssetTransactions);
                BankLogic.Export(writer, qif.BankTransactions);
                CashLogic.Export(writer, qif.CashTransactions);
                CategoryListLogic.Export(writer, qif.CategoryListTransactions);
                ClassListLogic.Export(writer, qif.ClassListTransactions);
                CreditCardLogic.Export(writer, qif.CreditCardTransactions);
                InvestmentLogic.Export(writer, qif.InvestmentTransactions);
                LiabilityLogic.Export(writer, qif.LiabilityTransactions);
                MemorizedTransactionListLogic.Export(writer, qif.MemorizedTransactionListTransactions);
            }
        }
Example #2
0
File: QifDom.cs Project: rolek/qif
        /// <summary>
        /// Exports the specified instance properties to the specified file.
        /// </summary>
        /// <param name="qif">The <seealso cref="T:QifDom"/> to export.</param>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="encoding">
        /// The encoding to use when exporting the QIF file. This defaults to UTF8
        /// when not specified.
        /// </param>
        /// <remarks>This will overwrite an existing file.</remarks>
        public static void ExportFile(QifDom qif, string fileName, Encoding encoding = null)
        {
            if (File.Exists(fileName))
            {
                File.SetAttributes(fileName, FileAttributes.Normal);
            }

            using (StreamWriter writer = new StreamWriter(fileName, false, encoding ?? Encoding.UTF8))
            {
                writer.AutoFlush = true;

                AccountListLogic.Export(writer, qif.AccountListTransactions, qif.Configuration);
                AssetLogic.Export(writer, qif.AssetTransactions, qif.Configuration);
                BankLogic.Export(writer, qif.BankTransactions, qif.Configuration);
                CashLogic.Export(writer, qif.CashTransactions, qif.Configuration);
                CategoryListLogic.Export(writer, qif.CategoryListTransactions, qif.Configuration);
                ClassListLogic.Export(writer, qif.ClassListTransactions, qif.Configuration);
                CreditCardLogic.Export(writer, qif.CreditCardTransactions, qif.Configuration);
                InvestmentLogic.Export(writer, qif.InvestmentTransactions, qif.Configuration);
                LiabilityLogic.Export(writer, qif.LiabilityTransactions, qif.Configuration);
                MemorizedTransactionListLogic.Export(writer, qif.MemorizedTransactionListTransactions, qif.Configuration);
            }
        }
Example #3
0
File: QifDom.cs Project: rolek/qif
        /// <summary>
        /// Imports a QIF file stream reader and returns a QifDom object.
        /// </summary>
        /// <param name="reader">The stream reader pointing to an underlying QIF file to import.</param>
        /// <param name="config">The configuration to use while importing raw data</param>
        /// <returns>A QifDom object of transactions imported.</returns>
        public static QifDom ImportFile(StreamReader reader, Configuration config = null)
        {
            QifDom result = new QifDom(config);

            // Read the entire file
            string input = reader.ReadToEnd();

            // Split the file by header types
            string[] transactionTypes = Regex.Split(input, @"^(!.*)$", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);

            // Loop through the transaction types
            for (int i = 0; i < transactionTypes.Length; i++)
            {
                // Get the exact transaction type
                string transactionType = transactionTypes[i].Replace("\r", "").Replace("\n", "").Trim();

                // If the string has a value
                if (transactionType.Length > 0)
                {
                    // Check the transaction type
                    switch (transactionType)
                    {
                    case Headers.Bank:
                        // Increment the array counter
                        i++;

                        // Extract the transaction items
                        string bankItems = transactionTypes[i];

                        // Import all transaction types
                        result.BankTransactions.AddRange(BankLogic.Import(bankItems, result.Configuration));

                        // All done
                        break;

                    case Headers.AccountList:
                        // Increment the array counter
                        i++;

                        // Extract the transaction items
                        string accountListItems = transactionTypes[i];

                        // Import all transaction types
                        result.AccountListTransactions.AddRange(AccountListLogic.Import(accountListItems, result.Configuration));

                        // All done
                        break;

                    case Headers.Asset:
                        // Increment the array counter
                        i++;

                        // Extract the transaction items
                        string assetItems = transactionTypes[i];

                        // Import all transaction types
                        result.AssetTransactions.AddRange(AssetLogic.Import(assetItems, result.Configuration));

                        // All done
                        break;

                    case Headers.Cash:
                        // Increment the array counter
                        i++;

                        // Extract the transaction items
                        string cashItems = transactionTypes[i];

                        // Import all transaction types
                        result.CashTransactions.AddRange(CashLogic.Import(cashItems, result.Configuration));

                        // All done
                        break;

                    case Headers.CategoryList:
                        // Increment the array counter
                        i++;

                        // Extract the transaction items
                        string catItems = transactionTypes[i];

                        // Import all transaction types
                        result.CategoryListTransactions.AddRange(CategoryListLogic.Import(catItems, result.Configuration));

                        // All done
                        break;

                    case Headers.ClassList:
                        // Increment the array counter
                        i++;

                        // Extract the transaction items
                        string classItems = transactionTypes[i];

                        // Import all transaction types
                        result.ClassListTransactions.AddRange(ClassListLogic.Import(classItems, result.Configuration));

                        // All done
                        break;

                    case Headers.CreditCard:
                        // Increment the array counter
                        i++;

                        // Extract the transaction items
                        string ccItems = transactionTypes[i];

                        // Import all transaction types
                        result.CreditCardTransactions.AddRange(CreditCardLogic.Import(ccItems, result.Configuration));

                        // All done
                        break;

                    case Headers.Investment:
                        // Increment the array counter
                        i++;

                        // Extract the transaction items
                        string investItems = transactionTypes[i];

                        // Import all transaction types
                        result.InvestmentTransactions.AddRange(InvestmentLogic.Import(investItems, result.Configuration));

                        // All done
                        break;

                    case Headers.Liability:
                        // Increment the array counter
                        i++;

                        // Extract the transaction items
                        string liabilityItems = transactionTypes[i];

                        // Import all transaction types
                        result.LiabilityTransactions.AddRange(LiabilityLogic.Import(liabilityItems, result.Configuration));

                        // All done
                        break;

                    case Headers.MemorizedTransactionList:
                        // Increment the array counter
                        i++;

                        // Extract the transaction items
                        string memItems = transactionTypes[i];

                        // Import all transaction types
                        result.MemorizedTransactionListTransactions.AddRange(MemorizedTransactionListLogic.Import(memItems, result.Configuration));

                        // All done
                        break;

                    default:
                        // Don't do any processing
                        break;
                    }
                }
            }

            return(result);
        }
Example #4
0
        /// <summary>
        /// Imports a QIF file stream reader and returns a QifDom object.
        /// </summary>
        /// <param name="reader">The stream reader pointing to an underlying QIF file to import.</param>
        /// <param name="config">The configuration to use while importing raw data</param>
        /// <returns>A QifDom object of transactions imported.</returns>
        public static QifDom ImportFile(TextReader reader, Configuration config = null)
        {
            QifDom result = new QifDom(config);

            // Read the entire file
            string input = reader.ReadToEnd();

            // Split the file by header types
            string[] transactionTypes = Regex.Split(input, @"^(!.*)$", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);

            // Remember the last account name we saw so we can link its transactions to it.
            string currentAccountName = string.Empty;

            // Loop through the transaction types
            for (int i = 0; i < transactionTypes.Length; i++)
            {
                // Get the exact transaction type
                string transactionType = transactionTypes[i].Replace("\r", "").Replace("\n", "").Trim();

                // If the string has a value
                if (transactionType.Length > 0)
                {
                    // Check the transaction type
                    switch (transactionType)
                    {
                    case Headers.Bank:
                        // Increment the array counter
                        i++;

                        // Extract the transaction items
                        string bankItems = transactionTypes[i];

                        // Import all transaction types
                        var transactions = BankLogic.Import(bankItems, result.Configuration);

                        // Associate the transactions with last account we saw.
                        foreach (var transaction in transactions)
                        {
                            transaction.AccountName = currentAccountName;
                        }

                        result.BankTransactions.AddRange(transactions);

                        // All done
                        break;

                    case Headers.AccountList:
                        // Increment the array counter
                        i++;

                        // Extract the transaction items
                        string accountListItems = transactionTypes[i];

                        // Import all transaction types
                        var accounts = AccountListLogic.Import(accountListItems, result.Configuration);

                        // Remember account so transaction following can be linked to it.
                        currentAccountName = accounts.Last().Name;

                        result.AccountListTransactions.AddRange(accounts);

                        // All done
                        break;

                    case Headers.Asset:
                        // Increment the array counter
                        i++;

                        // Extract the transaction items
                        string assetItems = transactionTypes[i];

                        // Import all transaction types
                        result.AssetTransactions.AddRange(AssetLogic.Import(assetItems, result.Configuration));

                        // All done
                        break;

                    case Headers.Cash:
                        // Increment the array counter
                        i++;

                        // Extract the transaction items
                        string cashItems = transactionTypes[i];

                        // Import all transaction types
                        result.CashTransactions.AddRange(CashLogic.Import(cashItems, result.Configuration));

                        // All done
                        break;

                    case Headers.CategoryList:
                        // Increment the array counter
                        i++;

                        // Extract the transaction items
                        string catItems = transactionTypes[i];

                        // Import all transaction types
                        result.CategoryListTransactions.AddRange(CategoryListLogic.Import(catItems, result.Configuration));

                        // All done
                        break;

                    case Headers.ClassList:
                        // Increment the array counter
                        i++;

                        // Extract the transaction items
                        string classItems = transactionTypes[i];

                        // Import all transaction types
                        result.ClassListTransactions.AddRange(ClassListLogic.Import(classItems, result.Configuration));

                        // All done
                        break;

                    case Headers.TagList:
                        // Increment the array counter
                        i++;

                        // Extract the transaction items
                        string tagListItems = transactionTypes[i];

                        // Import all transaction types
                        result.TagListTransactions.AddRange(TagListLogic.Import(tagListItems, result.Configuration));

                        // All done
                        break;

                    case Headers.CreditCard:
                        // Increment the array counter
                        i++;

                        // Extract the transaction items
                        string ccItems = transactionTypes[i];

                        // Import all transaction types
                        result.CreditCardTransactions.AddRange(CreditCardLogic.Import(ccItems, result.Configuration));

                        // All done
                        break;

                    case Headers.Investment:
                        // Increment the array counter
                        i++;

                        // Extract the transaction items
                        string investItems = transactionTypes[i];

                        // Import all transaction types
                        result.InvestmentTransactions.AddRange(InvestmentLogic.Import(investItems, result.Configuration));

                        // All done
                        break;

                    case Headers.Liability:
                        // Increment the array counter
                        i++;

                        // Extract the transaction items
                        string liabilityItems = transactionTypes[i];

                        // Import all transaction types
                        result.LiabilityTransactions.AddRange(LiabilityLogic.Import(liabilityItems, result.Configuration));

                        // All done
                        break;

                    case Headers.MemorizedTransactionList:
                        // Increment the array counter
                        i++;

                        // Extract the transaction items
                        string memItems = transactionTypes[i];

                        // Import all transaction types
                        result.MemorizedTransactionListTransactions.AddRange(MemorizedTransactionListLogic.Import(memItems, result.Configuration));

                        // All done
                        break;

                    default:
                        // Don't do any processing
                        break;
                    }
                }
            }

            return(result);
        }