Click here to Skip to main content
15,390,456 members
Articles / General Programming / Usability
Tip/Trick
Posted 5 Aug 2022

Stats

1.8K views
2 bookmarked

Three Useful Programs with Open Storage

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
5 Aug 2022MIT2 min read
Develop programs with open storage using pre-defined format
The modern trends are of big computer resources consumption due to the specific storage of data - we create open storage for everyday use.

Image 1 Image 2 Image 3

Introduction

This tip/trick is about the usage of open storage like comma separated values (CSV). We will provide the reader with necessary parsing scheme of CSV files and further usage in utility program composition.

The open storage provides the user with efficient ability to store and parse it just-in-time (JIT).

Open Storage

The CSV format is defined as the line of values separated by either comma, space or any other separated while the string data are enclosed in string holder values. This CSV format can also effectively store real numbers as well and is supported by almost all office programs.

Using the Code

In this section, we will show how to parse CSV file using the simple and efficient approach which is further reused in the applications for every day use: "Alumni" is a stand-alone web server, "CSVdb" is a NoSQL stand-alone CSV database and "ML+" is a stand-alone program to work with Machine Learning and classification.

The CSVLine class parses the line of open storage file on the fly and is realized as follows below:

C#
/*
 * Created by SharpDevelop.
 * User: Mirzakhmet
 * Date: 6/28/2022
 * Time: 1:52 PM
 * 
 * To change this template, use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;

namespace Alumni
{
	/// <summary>
	/// Line in CSV file.
	/// </summary>
	public class CSVLine
	{
		public List<string> values = new List<string>();
		
		public CSVLine(ParsingStream stream)
		{
			while (!stream.atEnd()) {
				string result = parseValue(stream);
				
				this.values.Add(result);
				
				if (stream.current == '\n') {
					stream.Read();
					
					break;
				}
			}
		}
		
		public string parseValue(ParsingStream stream) {
			string result = "";
			
			stream.parseBlanks();
			
			int delimeter = -1;
			
			if (stream.current == '"' || stream.current == '\'') {
				delimeter = stream.current;
				
				stream.Read();
			}
			
			if (delimeter == -1) {
				while (!stream.atEnd() && ",;".IndexOf((char) 
                        stream.current) == -1 && stream.current != '\n') {
					if (stream.current == '\\' && delimeter != -1) {
						stream.Read();
						
						result += (char) stream.current;	
					} else {
						result += (char) stream.current;
					}
					
					stream.Read();
				}
			} else {
				while (!stream.atEnd() && stream.current != '\n' && 
                        stream.current != delimeter && ",;".IndexOf((char) 
                        stream.current) == -1) {
					if (stream.current == '\\') {
						stream.Read();
						
						result += (char) stream.current;
					} else {
						result += (char) stream.current;
					}
					
					stream.Read();
				}
				
				if (stream.current == delimeter) {
					stream.Read();
				}
			}
			
			stream.parseBlanks();
			
			if (",; \t".IndexOf((char) stream.current) >= 0) {
				stream.Read();
			}
			
			stream.parseBlanks();
			
			return result.Trim();
		}
	}
}

The bunch of lines is stored in the CSVFile class which is also very quick and robust.

We will provide the code of this class in order to demonstrate the power and effectiveness of open storage based on CSV format.

C#
/*
 * Created by SharpDevelop.
 * User: Mirzakhmet
 * Date: 6/28/2022
 * Time: 1:53 PM
 * 
 * To change this template, use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections;
using System.Collections.Generic;

namespace Alumni
{
	/// <summary>
	/// CSV file.
	/// </summary>
	public class CSVFile
	{
		public string name = null;
		public ArrayList names = new ArrayList();
		public Dictionary<string, int> namesIndex = new Dictionary<string, int>();
		public List<CSVLine> lines = new List<CSVLine>();
		
		public CSVFile(ParsingStream stream, string name)
		{
			this.name = name;
			
			CSVLine namesLine = new CSVLine(stream);
			
			int i = 0;
			
			foreach (string s in namesLine.values) {
				this.names.Add(s);
				
				this.namesIndex.Add(s, i++);
			}
			
			while (!stream.atEnd()) {
				CSVLine line = new CSVLine(stream);
				
				if (line.values.Count > 0) {
					this.lines.Add(line);
				}
			}
			
			stream.stream.Close();
		}
	}
}

Basic Principles

As we have defined the necessary bunch of classes in order to read open storage, now we can construct the configuration files along with other information for our projects which use the open storage like web server "Alumni", NoSQL database "CSVdb" and Machine Learning utility "ML+".

Step by Step Walk-throughs

Thus, we develop the open storage parser for use in the web-server "Alumni", sample files can be found in demo project - these files configure the server:

Image 4

The NoSQL database which uses the open storage to make queries:

Image 5

The Machine Learning program to match against the OLAP-like data uses open storage to deal with classified input data within the pre-defined threshold of equality for use queries:

Image 6

Conclusion and Points of Interest

Thus, we have developed open storage based on the CSV file format for effective usage in data queries and configuration storage. This is not limited to the usage of JSON or HTML and XML formats in databases.

History

  • 4th August, 2022: Initial version

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Mirzakhmet Syzdykov
Software Developer M.S.-Developers
Kazakstan Kazakstan
Experienced Java programmer

Comments and Discussions

 
-- There are no messages in this forum --