Example #1
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 #2
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);
        }