Code archives/File Utilities/CSV Generator

This code has not been declared by its author to be Public Domain code, and therefore should not be assumed to be so.

Download source code

CSV Generator by Braneloc
(Posted 24 years ago)
Generates nice friendly useful code for your database/spreadsheet files stored in .csv format. (Comma Seperated Values, as exported by Excel and most spreadsheets) Update your code instantly if you add/delete fields. Use multiple CSV files with a single include.

Uses multiple files, please download complete files from http://www.mirex.demon.co.uk/blitz

Enjoy !



============================
CSV Database Generator !
Lousy Documentation
============================

Copyright Keith Hill, Aug 2002
Braneloc@...

Purpose:
Nice friendly CSV data files for your custom TYPE lists
and standard functions to do stuff with them.


Usage:
Stick all your CSV databases in a directory
Have a nice database_code directory
Run this program, it will create....

"db_code/database.bb"
master include for all generated databases

code that does the following... (where TYPE is the name of your csv.database)

TYPE_load Load the entire type list into memory
TYPE_kill Free the entire type list from memory
TYPE_save Save the entire type list to file
TYPE_add Add a TYPE to memory - requires full parameters
TYPE_find Find a type in your list indexed on the first field
TYPE_count How many of these darn things have you loaded ?
TYPE_fadd Find-and-Add - create a new one if it does not exist
TYPE_sort Sorts the memory list into order

Licence:
Use it as you see fit, free of charge for all projects ! (Though I'd
appreciate an email if you use it in your projects)

Ownership and copyright of the generator code and template remain with
Braneloc@... at all times. Please send me any updates(!)

Distribution:
You are quite free to distribute any generated code or executables based
on generated code as you see fit, though the code generator/template
(source or executable) may not be distributed without prior consent of
Braneloc@... at all times for any use. (Especially if you
are making a profit and I am not !)



Template:

{ name of "type"
# first field of type
¬ duplicate line for each field of type
} field in duplicated line
~ entire data structure



Main program code (July03)
("csv-codegen.bb")
;
;
;		CSV Database Generator !!
;
;		(c) K.Hill, Aug 2002
;
;
;
Include "misc.bb"
;Include "db_code/database.bb"

Global qq,name$,topline$
Global codedir$="db_code/"
Global datadir$="database"

;
;		Installation.
;		The two directories above, codedir$ and datadir$ should be created..
;		Put the template file into the CODEdir
;		Put any CSV files into the DATAdir
;		

;
;		Usage: 	Ok, once installation is done, you can auto-gen code to handle CSV files
;				as types by running this file.  In *your* code, just include either the
;				generated code file ( something_csv.bb) or "database.bb" (which contains
;				includes of all includes :)  It should all be fairly simple after that.
;

;
;		Lousy Documentation
;		(in templates)
;
;	{	name of "type"
;	#	first field of type
;	¬	duplicate line for each field of type
;	}	field in duplicated line
;	£	field in duplicated line - without type
;	~	entire data structure
;

master=WriteFile(codedir$+"database.bb")
	Restore base:a$=";"	
		While a$<>"***"
			Read a$
			If a$<>"***"
				WriteLine master,a$
			EndIf
		Wend
		WriteLine master,";Include "+Chr$(34)+"Misc.bb"+Chr$(34)
		WriteLine master,"Global csv_dir$:csv_dir$="+Chr$(34)+datadir$+"/"+Chr$(34)
	
	name$="_"
	dd=ReadDir(datadir$)
		While name$<>""
			;name$="test"
			name$=NextFile$(dd)
			;Print name$
			If name$<>""
				If FileType(name$)<>2
					name$=killext(name$)
					process(name$)
					WriteLine master,"Include "+Chr$(34)+codedir$+name$+"_csv.bb"+Chr$(34)
				EndIf
			EndIf
		Wend
	CloseDir dd

CloseFile master

Function killext$(a$)
	For n=1 To Len(a$)
		b$=Mid$(a$,n,1)
		If b$="."
			Return z$
		Else
			z$=z$+b$
		EndIf
	Next
	Return z$
End Function
Function process(name$)	
	Print "Processing..."+datadir$+"/"+name$+".csv"
	f=ReadFile(datadir$+"/"+name$+".csv")
	qq=WriteFile(codedir$+name$+"_csv.bb")
	
	topline$=ReadLine$(f)
	split_csv(topline$)
	
	For n=1 To 10
;		Print split$(n)
	Next
	
	
	;Restore start:codechunk()
	;pp( "Type "+name$)
	;template("	Field ")
	;pp("End Type")
	CloseFile f
	
	f=ReadFile(codedir$+"csv-template!.bb")
	While Not Eof(f)
		a$=ReadLine(f)
		If Mid$(a$,1,1)="¬"
			For ff=1 To WORDSNUM
				If split$(ff)<>""
					procline(Mid$(a$,2),ff)
				EndIf
			Next
		Else
			procline(a$)
		EndIf
	Wend
	CloseFile f
	CloseFile qq
End Function

Function procline(a$,ff=1)
	z$=""
	For n=1 To Len(a$)
		b$=Mid$(a$,n,1)
		Select b$
		Case "{"
			z$=z$+name$
		Case "#"
			z$=z$+split$(1)
		Case "}"
			z$=z$+split$(ff)
		Case "£"
			z$=z$+notype$(split$(ff))
		Case "~"
			z$=z$+topline$
		Default
			z$=z$+b$
		End Select
	Next
	pp(z$)
End Function
Function notype$(a$)
	z$=Right$(a$,1)
	If z$="#" Or z$="%" Or z$="$"
		Return Mid$(a$,1,Len(a$)-1)
	Else
		Return a$
	EndIf
End Function	
Function codechunk()
	a$=";"
	While a$<>"***"
		Read a$
		If a$<>"***"
			pp( a$)
		EndIf
	Wend
	Print ";"
End Function
Function template(a$="",b$="")
	z$=""
	For n=1 To WORDSNUM
		If split$(n)<>""
			pp( a$+split$(n)+b$)
		EndIf
	Next
End Function
Function pp(a$)
;	Print a$
	WriteLine qq,a$
End Function


.start
Data ";		Generated code
Data ";		CSV Database system
Data ";		By Braneloc, Aug 2002
Data ";
Data ";
Data ";		Global csv_dir$
Data "****
.base
Data ";
Data ";
Data ";		Generated database include
Data ";
Data ";		(c) K.Hill, Aug 2002
Data ";			Contains all database includes for the project
Data ";
Data ";
Data "****


TEMPLATE code (july03)
("db_code/csv-template!.bb")
;
;
;		Generated code
;		CSV Database system
;		By Braneloc, Aug 2002
;
;
;		{.csv
;		~
;
;include "misc.bb"
;Global csv_dir$:csv_dir$="database/"

Type {
¬	Field }
End Type

Function {_pack$(q.{)
	a$=""
¬	a$=a$+q\}+","
	Return a$
End Function
Function {_data.{(a$)
	If a$<>""
		split_csv(a$)
		tag=1
		q.{=New {
¬		q\}=split$(tag):tag=tag+1
	EndIf
	Return q.{
End Function
Function {_load()
	f=ReadFile(csv_dir$+"{.csv")
	a$=ReadLine(f)	; header line
	While Not Eof(f)
		a$=ReadLine(f)
		If a$<>""
			split_csv(a$)
			tag=1
			q.{=New {
¬			q\}=split$(tag):tag=tag+1
		EndIf
	Wend
	CloseFile(f)
End Function
Function {_kill()
	For n.{=Each {
		Delete n
	Next
End Function
Function {_save()
	f=WriteFile(csv_dir$+"{.csv")
	WriteLine f,"~"
	For q.{=Each {
		a$=""
¬		a$=a$+q\}+","
		WriteLine f,a$
	Next
	CloseFile(f)
End Function
Function {_saveXML()
	f=WriteFile(csv_dir$+"{.XML")
	WriteLine f,"<?xml version="+z$+"1.0"+z$+" encoding="+z$+"utf-8"+z$+" ?>"
	WriteLine f,"<{_list>"
	For q.{=Each {
		WriteLine f,"<{>"
¬		WriteLine f,"    <£>"+q\£+"</£>"	
		WriteLine f,"</{>"	
	Next
	WriteLine f,"</{_list>"
	WriteFile f
	CloseFile (f)
End Function
Function {_add(~)
	q.{=New {
¬	q\}=}
End Function
Function {_find.{(#)
	For n.{=Each {
		If n\#=#
			Return n
		EndIf
	Next
	Return Null
End Function
Function {_count()
	tag=0
	For n.{=Each {
		tag=tag+1
	Next
	Return tag
End Function
Function {_fadd.{(#)
	tag.{=Null
	For n.{=Each {
		If n\#=#
			tag=n
		EndIf
	Next
	If tag=Null
		tag=New {
	EndIf
	Return tag
End Function
Function {_sort()
	Repeat
		swapped=False
		For {.{ = Each {
			afterType.{=After {
			If (afterType<>Null)
				If (afterType\# < {\#)
					Insert afterType Before {
					swapped=True
				EndIf
			EndIf
		Next
	Until Not swapped
End Function


("misc.bb")
;
;
;		(edited) Miscellaneous stuff
;
;		(c) K.Hill, June 2001
;			Braneloc@...
;


Const WORDSNUM = 100
Dim split$(WORDSNUM )


Function split_csv(a$)
	;# splits up a line of text into words
	s=1
	For n=1 To WORDSNUM 
		split$(n)=""
	Next
	For n=1 To Len(a$)
		z$=Mid$(a$,n,1)
		If z$=","
			split$(s)=Trim (split$(s))
			s=s+1
		Else
			split$(s)=split$(s)+z$
		EndIf
	Next
	split$(s)=Trim (split$(s))
End Function




Sample CSV file...
("database/test.csv")

Note the first line contains the TYPENAMEs
name$,email$,comment$
Braneloc,Braneloc@mirex.demon.co.uk,Author of this stuff