private void FunctionInclude(IDictionary cs, ArrayList args, ref string file, int line)
{
string data;
if(string.IsNullOrEmpty(file)) RaiseError(file, line, ERROR_INCLUDE_UNSUPPORTED);
if(args.Count < 1) RaiseError(file, line, ERROR_INVALID_ARGS);
if(!(args[0] is string)) RaiseError(file, line, ERROR_INVALID_ARGS + " Expected a string for argument 1.");
if((args.Count > 1) && !(args[1] is string)) RaiseError(file, line, ERROR_INVALID_ARGS + " Expected a string for argument 2.");
if(cpErrorResult) return;
// Determine the full path of the file to include
string includefile = Path.GetDirectoryName(file) + Path.DirectorySeparatorChar + args[0].ToString();
try
{
// Load the file contents
FileStream fstream = File.OpenRead(includefile);
byte[] fbuffer = new byte[fstream.Length];
fstream.Read(fbuffer, 0, fbuffer.Length);
fstream.Close();
// Convert byte array to string
data = Encoding.UTF8.GetString(fbuffer);
}
catch(Exception e)
{
RaiseError(file, line, "Unable to include file '" + includefile + "'. " + e.GetType().Name + ": " + e.Message);
return;
}
// Remove returns and tabs because the
// parser only uses newline for new lines.
data = data.Replace("\r", "");
data = data.Replace("\t", "");
// Parse the data
IDictionary inc;
if(cs is ListDictionary) inc = new ListDictionary(); else inc = new Hashtable();
int npos = 0, nline = 1;
InputStructure(inc, ref includefile, ref data, ref npos, ref nline);
if(!cpErrorResult)
{
// Check if a path is given
if((args.Count > 1) && !string.IsNullOrEmpty(args[1].ToString()))
{
IDictionary def;
if(cs is ListDictionary) def = new ListDictionary(); else def = new Hashtable();
if(CheckSetting(inc, args[1].ToString(), DEFAULT_SEPERATOR))
{
inc = (IDictionary)ReadAnySetting(inc, file, line, args[1].ToString(), def, DEFAULT_SEPERATOR);
}
else
{
RaiseError(file, line, "Include missing structure '" + args[1].ToString() + "' in file '" + includefile + "'");
return;
}
}
// Recursively merge the structures with the current structure
IDictionary newcs = Combine(cs, inc, (cs is ListDictionary));
cs.Clear();
foreach(DictionaryEntry de in newcs) cs.Add(de.Key, de.Value);
}
}