Git Product home page Git Product logo

read-basic-ini-file's Introduction

read-basic-ini-file

Description

Professional-level sample source code in various programming languages for reading text files with error handling, using basic .ini file format parsing as an example.

  • Go - read_basic_ini_file.go
  • Perl5 - read_basic_ini_file.pl
  • PHP - read_basic_ini_file.php
  • Python 2.5+ and 3 - read_basic_ini_file.py
  • Ruby - read_basic_ini_file.rb
  • Bash4 - read_basic_ini_file.sh

Each file is a complete program that reads config.ini, parses it and displays the key-values.

Usage

  1. Pick your preferred sample source code file according to programming language
  2. Run it to see it in action
  3. Then copy and paste the source code and customize for your purpose.

Demo .ini Parsing Rules

Although simple .ini parsing is done for demonstration purposes, you should use an official .ini parsing module if required.

The simple parsing rules from input lines to key-value pairs are:

  • empty lines are skipped
  • lines starting with comments (; or #) are skipped
  • [section] lines are skipped
  • values containing extra = signs are allowed
  • multi-line settings are not recognized
  • substitutions are not performed
  • key and value strings are trimmed on left-hand and right-hand sides
  • missing value default is empty string
  • missing newline at EOF should not matter
  • return a hash (aka dict or associative array) or hash handle of key-value pairs if possible

Sample config.ini provided for testing

<blank line>
; comment
# comment
[section]
camel=2
dog=4
 cat=3
ant = 1000
llama = 2 = a
aardvark

Example

$ ./read_basic_ini_file.pl

aardvark=
ant=1000
camel=2
cat=3
dog=4
llama=2 = a

FAQ

Q: Why?

A: It's not as simple as you might think to write reliable, portable code for file processing, especially in an unfamiliar programming language. As complete, tested sample programs, these are more useful than typical online programming answers.

Copyright

James Briggs, USA. 2019.

License

MIT License.

read-basic-ini-file's People

Contributors

jamesbriggs avatar

Watchers

 avatar  avatar

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.