Code Monkey home page Code Monkey logo

fileparser's Introduction

FileParser

Azure DevOps GitHub Actions Circle CI

Sonar Quality Code coverage Sonar vulnerabilities Sonar bugs Sonar code smells

Nuget

API

FileParser is a .NET library designed to read text files line-by-line, saving each line's content into basic types vars (int, double, string, etc.).

  • .NET Framework 4.6 was supported until v1.4.x.
  • .NET Standard 2.0 and 2.1 were supported until v1.6.x.
  • Nullable is enabled from 1.6.x.

Purpose

This project was born with a very specific purpose: providing a tool with whom easily parse files with a known structure, ideally being as flexible and easy to use as C++ standard IO approach.

For those who don't understand what I mean, here's a simple Use Case (also reposited):

Given the following input.txt, which contains an integer n (>=0) followed by n doubles and a final string,

5   1.1 3.14159265 2.2265       5.5 10              fish

A simple .cpp snippet like the following one could process input.txt, providing that file is selected as standard input source:

./myExecutable < input.txt > output.txt

#include <iostream>
#include <list>
#include <string>

int main()
{
    int _integer;
    std::string _str;
    std::list<double> _list;
    double _auxdouble;

    // Input start;
    std::cin>>_integer;
    for(int i=0; i<_integer; ++i)
    {
        std::cin>>_auxdouble;
        _list.push_back(_auxdouble);
    }
    std::cin>>_str;
    // Input end

    // Data processing

    // Output start
    std::cout<<_integer<<" ";
    for(const double& d : _list)
        std::cout<<d<<" ";
    std::cout<<_str;
    // Output end

    return 0;
}

Seems effortless to process these kind of simple .txt files using C++, right?

Well, using C# things are not so straight-forward, and that's why FileParser was created for:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;

using FileParser;

namespace FileParserSample
{
    class Program
    {
        static void Main(string[] args)
        {
            var cultureInfo = new CultureInfo("en-US");
            CultureInfo.DefaultThreadCurrentCulture = cultureInfo;

            List<double> listDouble = new List<double>();
            string str;

            // Input start
            IParsedFile file = new ParsedFile("SimpleInput.txt");
            IParsedLine firstLine = file.NextLine();

            int _integer = firstLine.NextElement<int>();

            for(int i=0; i<_integer; ++i)
                listDouble.Add(firstLine.NextElement<double>());

            str = firstLine.NextElement<string>();
            // Input end

            // Data Processing

            // Output start
            StreamWriter writer = new StreamWriter("..\\CSharpSimpleOutput.txt");
            using (writer)
            {
                writer.WriteLine(_integer + " " + string.Join(null, listDouble));
            }
            // Output end
        }
    }
}

Documentation

I've done my best to create a WIKI describing FileParser API.

Besides the WIKI, some real (own) projects where it has been used are:

Contributing, issues, suggestions, doubts

If anyone else ever happens to use FileParser, I'll be happy to accept suggestions and solve any doubts.

Just open an issue :)

fileparser's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar eduherminio avatar github-actions[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

fileparser's Issues

Make non-public classes internal

Nowadays only IParsedFile. IParsedLine & FileReader are intended to be directly used.
Last one might not be exposed in the future in favour of more methods of IParsedFile.
Explicit public API needs to be created.

Fix 'Commit and push version increment' step in Azure DevOps

Despite ending up green, git push fails apparently due to status checks.
Tag is pushed correctly, though.
Only known workaround used in AoCHelper is disabling the Required status checks step in repo's configuration.

How about giving admin:repo_hook permissions to that Api Key?

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   Examples/FileParserExample/FileParserExample.csproj
	modified:   NugetTest/NugetTest/NugetTest.csproj
	modified:   src/FileParser/FileParser.csproj

no changes added to commit (use "git add" and/or "git commit -a")
[master 7b94a97] Release v1.4.3
 3 files changed, 3 insertions(+), 3 deletions(-)
remote: error: GH006: Protected branch update failed for refs/heads/master.        
remote: error: 8 of 8 required status checks are expected.        
To https://github.com/eduherminio/FileParser
 ! [remote rejected] master -> master (protected branch hook declined)
error: failed to push some refs to 'https://github.com/eduherminio/FileParser'
To https://github.com/eduherminio/FileParser
 * [new tag]         v1.4.3 -> v1.4.3
Finishing: Commit and push version increment

Allow empty items

Hi,

Great clean library that works except for one flaw, the option to remove white space needs to be optional so that empty values between delimiters match up to the headers, without that it is impossible to know which data value goes with which header column, assuming the first line in csv file is the header column names.

The line to remove the white space in this method should be made optional with a bool parameter.

private static ICollection<string> ProcessLine(string original_line, string? separator)
     {
         List<string> wordsInLine = original_line
             .Split(separator?.ToCharArray())
             .Select(str => str.Trim()).ToList();

         wordsInLine.RemoveAll(string.IsNullOrWhiteSpace);   // Probably not needed, but just in case

         return wordsInLine;
     }

Force IOException in Linux while reading a file

The following test needs to be skipped since it fails in Travis CI env (no exception is thrown).

        [Fact(Skip ="No exception is thrown in Linux (CI env)")]
        void IOException()
        {
            string fileName = "Any.txt";
            StreamWriter writer = new StreamWriter(fileName);
            using (writer)
            {
                Assert.Throws<IOException>(() => FileReader.Parse(fileName));
            }
        }

If no way of forcing IOException in Linux is found, test would better be deleted.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.