Using the tinycdb index files.
using System;
using System.IO;
using matrix_science.msparser;
namespace MsParserExamples
{
public class tools_tinycdb
{
public static void Main(string[] argv)
{
if (argv.Length < 1)
{
Console.WriteLine("The location of the source file for this application (tools_tinycdb.cs) should be specified as a parameter");
return;
}
string version = "1.1";
string sourceFile = System.IO.Path.GetFileName((new tools_tinycdb()).GetType().Assembly.Location);
string cdbname = sourceFile + ".cdb";
ms_tinycdb cdb = new ms_tinycdb(cdbname, version, sourceFile);
if (!cdb.isValid())
{
showErrors(cdb);
return;
}
if (!cdb.openIndexFile(true))
{
if (cdb.isPossibleToCreate())
{
Console.WriteLine("Creating file because: {0}", cdb.getLastErrorString());
cdb.prepareToCreate();
cdb.saveValueForKey("Hello", "world");
cdb.saveValueForKey("HELP", "me");
cdb.saveValueForKey("HELP", "me too");
using (StreamReader fh = new StreamReader(argv[0]))
{
long _pos = -1;
string line;
while ((line = fh.ReadLine()) != null)
{
if (line.Contains("// and saves an offset to THIS line!"))
{
break;
}
_pos = GetActualPosition(fh);
}
cdb.saveFileOffsetForKey("Line", _pos);
}
cdb.finishCreate();
}
}
if (cdb.isValid())
{
Console.WriteLine("Retrieving value for Hello : {0}", cdb.getValueFromKey("Hello"));
Console.WriteLine("Retrieving first value for HELP : {0}", cdb.getValueFromKey("HELP", 0));
Console.WriteLine("Retrieving second value for HELP: {0}", cdb.getValueFromKey("HELP", 1));
long pos = cdb.getFileOffsetFromKey("Line");
if (pos != -1)
{
using (FileStream fs = new FileStream(argv[0], FileMode.Open, FileAccess.Read))
{
fs.Seek(pos, SeekOrigin.Begin);
using (StreamReader sr = new StreamReader(fs))
{
Console.WriteLine("Testing file offset. Offset {0}: {1}", pos, sr.ReadLine());
}
}
}
else
{
Console.WriteLine("Target line was not found during .cdb file creation");
}
}
else
{
showErrors(cdb);
}
}
private static long GetActualPosition(StreamReader reader)
{
char[] charBuffer = (char[])reader.GetType().InvokeMember("charBuffer"
, System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetField
, null, reader, null);
int charPos = (int)reader.GetType().InvokeMember("charPos"
, System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetField
, null, reader, null);
int numReadBytes = reader.CurrentEncoding.GetByteCount(charBuffer, 0, charPos);
int byteLen = (int)reader.GetType().InvokeMember("byteLen"
, System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetField
, null, reader, null);
return reader.BaseStream.Position - byteLen + numReadBytes;
}
private static void showErrors(ms_tinycdb cdb)
{
ms_errs err = cdb.getErrorHandler();
for (int i = 1; i <= err.getNumberOfErrors(); i++)
{
Console.WriteLine("Error number: {0} ({1} times) : {2}", err.getErrorNumber(i), err.getErrorRepeats(i) + 1, err.getErrorString(i));
}
}
}
}