/*Delimited File Editor v0.01.
This is a console-based ascii delimited file editor. I've tried to keep it pretty flexible
so that you can decide how to structure your files. The only conditions that your files
must comply with are:
* Each record is seperated by CR/LF.
* The first record/line defines the name of the fields.
* The first line can be no more than 512 characters in length.
* Field delimiters may only be between 1 and 9 characters in length.
* Files cannot be any larger than 64k. I just didn't feel like messing around with dynamic strings.
** Note that the first line is seperate from the rest of the file when in memory. So the body
may be 64k and the first line (which defines the records) can be up to 512 bytes.
Commands("cmd")
Syntax "<cmd> [param1] [param2] [...]"
add_column <name_of_field>
Adds a new field.
del_column <name_of_field>
Remove a field.
get_heading
Displays the first record/line which holds the name of the fields.
set_delim <delim>
Sets the delimiter between fields. Keep in mind the heading is not updated when this
changes and that it may be no longer than 9 characters. If you do more than that you're overwriting
memory. Also note that spaces may be used, just type "set_delim ".
get_delim
Displays the delimiter.
set_filename <filename>
Sets the default filename. Keep in mind that spaces mean "new argument" which means you need to use
the short file name in the case of spaces in the path.
get_entry <index>
Displays the specified entry.
del_entry <index>
Removes the specified entry. Honestly this will only remove the first instance that looks exactly the
same as the entry you specify. I don't see this as being a huge deal and it was so much easier to write
this way.
cls
Clears the console.
get_fieldcount
Displays the number of fields in the first record/line which holds the name of the fields.
get_entrycount
Displays the number of records/entries.
get_data
Displays all of the records but not the first line that contains the name of the fields.
del_file [<filename>]
Either deletes the default file or it deletes the file that was passed to it.
save [<filename>]
Either saves the delimited file to the default file or to the filename that was passed to it.
load [<filename>]
Either loads the delimited file from the default file or from the filename that was passed to it.
done
Saves to default file then quits. If default filename is blank it reports an error and remains open.
bye|quit|exit|abort
Exits without saving.
*/
AutoDefine "Off"
Dim cmd,entry,filename As String
Dim heading[513],delim[10],delim_str[65537] As IString
Dim i As Int
Do
Input ">",cmd
Select LCase$(NthField(cmd," ",1))
Case "add_column"
If fieldcount(cmd," ")>1 Then
If heading="" Then
heading=NthField(cmd," ",2)
Else
heading+=delim+NthField(cmd," ",2)
End If
Else
Print("cmd \"add_column\" requires 1 parameter.")
End If
Case "del_column"
If InStr(heading,delim)=0 And InStr(heading,NthField(cmd," ",2))<>0 Then
heading=""
Else
If fieldcount(cmd," ")>1 Then
If InStr(heading,NthField(cmd," ",2))=1 Then
heading=Remove$(heading,NthField(cmd," ",2)+delim)
Else
heading=Remove$(heading,delim+NthField(cmd," ",2))
End If
Else
Print "cmd \"del_column\" requires 1 parameter."
End If
End If
Case "add_entry"
entry=""
For i=0 to fieldcount(heading,delim)-1
Input NthField(heading,delim,i+1)+">",cmd
entry+=cmd+delim
Next i
entry[Len(entry)-Len(delim)]=0
If delim_str="" Then delim_str=entry Else delim_str+="\n"+entry
Case "get_heading"
Print heading
Case "set_delim"
If fieldcount(cmd," ")>1 Then
If fieldcount(cmd," ")=3 And NthField(cmd," ",2)="" Then
delim=" "
Else
delim=NthField(cmd," ",2)
End If
Else
Print "cmd \"set_delim\" requires 1 parameter."
End If
Case "get_delim"
Print delim
Case "set_filename"
If fieldcount(cmd," ")>1 Then
filename=Mid$(cmd,InStr(cmd," ")+1)
Else
Print "cmd \"set_filename\" requires 1 parameter."
End If
Case "get_filename"
Print filename
Case "get_entry"
If fieldcount(cmd," ")>1 Then
If fieldcount(delim_str,"\n")<Val(NthField(cmd," ",2)) Or Val(NthField(cmd," ",2))<1 Then
Print "No entry #",NthField(cmd," ",2),"."
Else
If fieldcount(delim_str,delim)=fieldcount(heading,delim) Then
Print delim_str
Else
Print NthField(delim_str,"\n",Val(NthField(cmd," ",2)))
End If
End If
Else
Print "cmd \"get_entry\" requires 1 parameter."
End If
Case "del_entry"
If fieldcount(cmd," ")>1 Then
If fieldcount(delim_str,delim)=fieldcount(heading,delim) Then
delim_str=""
Else
If InStr(delim_str,NthField(delim_str,"\n",Val(NthField(cmd," ",2)))) Then
delim_str=Remove$(delim_str,NthField(delim_str,"\n",Val(NthField(cmd," ",2)))+"\n")
Else
delim_str=Remove$(delim_str,"\n"+NthField(delim_str,"\n",Val(NthField(cmd," ",2))))
End If
End If
Else
Print "cmd \"del_entry\" requires 1 parameter."
End If
Case "cls"
Cls
Case "get_fieldcount"
Print fieldcount(heading,delim)
Case "get_entrycount"
Print fieldcount(delim_str,"\n")
Case "get_data"
Print delim_str
Case "del_file"
If fieldcount(cmd," ")>1 Then
DeleteFile NthField(cmd," ",2)
Else
If filename<>"" Then
DeleteFile filename
Else
Print "cmd \"del_file\" requires either 1 parameter or to \"set_filename\" prior to use."
End If
End If
Case "save"
If fieldcount(cmd," ")>1 Then
Save(Mid$(cmd,InStr(cmd," ")+1))
Else
Save(filename)
End If
Case "load"
If fieldcount(cmd," ")>1 Then
Load(Mid$(cmd,InStr(cmd," ")+1))
Else
Load(filename)
End If
Case "done"
If filename="" Then
Print "Must \"set_filename\" prior to using \"done\" command."
Else
Save(filename)
End
End If
Case "bye"
Case& "quit"
Case& "exit"
Case& "abort"
End
End Select
Until False
Sub Save(fn As String)
Dim f As File
If OpenFile(f,fn,"W")=0 Then
Write(f,heading)
Write(f,delim_str)
CloseFile(f)
End If
Return
End Sub
Sub Load(fn As String)
Dim f As File
Dim buf[32*1024] As IString
If OpenFile(f,fn,"R")=0 Then
delim_str=""
buf=""
Read(f,buf)
heading=buf
Read(f,buf)
delim_str=buf
Do
Read(f,buf)
delim_str+="\n"+buf
buf=""
Until Eof(f)
CloseFile(f)
If delim_str[Len(delim_str)-2]="\x0D" Then
delim_str[Len(delim_str)-2]=0
End If
End If
Return
End Sub
Sub Remove$(source As String,target As String,Opt startpos=0 As Int),String
Dim pos As Int
pos=InStr(source,target,startpos)
If pos Then
Return Left$(source,pos-1)+Mid$(source,pos+Len(target))
End If
Return source
End Sub
Sub NthField(source As String,delimiter As String,fieldnumber As Int),String
Dim delpos,nexpos,count As Int
count=1
delpos=0
nexpos=InStr(source,delimiter)
While nexpos
If count=fieldnumber Then
If count=1 Then delpos=1-Len(delimiter)
Return Mid$(source,delpos+Len(delimiter),nexpos-delpos-Len(delimiter))
Else
delpos=nexpos
nexpos=InStr(source,delimiter,delpos+1)
count++
End If
Wend
Return Mid$(source,delpos+Len(delimiter))
End Sub
Sub fieldcount(source As String,delimiter As String),Int
Dim delpos,nexpos,count As Int
count=0
delpos=0
nexpos=InStr(source,delimiter)
While nexpos
delpos=nexpos
nexpos=InStr(source,delimiter,delpos+1)
count++
Wend
Return count+1
End Sub