CheckFileObjs.sml

  Download

More scripts: SML Fundamentals

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
# CheckFileObjs.sml
# Script to check Project Files in a directory for:
# 1) file contains only one raster object
# 2) raster name = file name
# Errors are recorded in a log file written to the input directory. 
class STRING inDir$;				# path to input directory selected
class STRINGLIST filelist;		# list of Project Fies in the directory
class STRING filePath$;			# filepath string for current Project File
class STRING fileNameRoot$;		# name of Project File without the extension
class RVC_OBJECT rvcFile;	# the current Project File
class RVC_OBJITEM objItemList[];		# hash of objitems in current Project File
class STRING rasterName$;	# name of the current raster object
numeric errorsFound;		# flag to indicate errors have been found
class FILE logfile;
numeric i;			# counter
class GUI_DLG dlgwin;			# the dialog window
class GUI_CTRL_PUSHBUTTON runBtn;		# handle for the Run button
class GUI_CTRL_EDIT_STRING indirtext;		# handle for the directory text field
class GUI_CTRL_EDIT_STRING statustext;
class GUI_CTRL_LISTBOX filelistbox, resultlistbox;	# handles for the listboxes
numeric err;
##### procedure prototypes
proc GetInDir();
proc onRun();
proc onCancel();
proc onClose();
##################################################
#################   Main Program   #####################
clear();
class STRING xml$ = '<?xml version="1.0"?>
<!DOCTYPE root SYSTEM "smlforms.dtd">
<root>
	<dialog id="checkNames" Title="Check File-Object Name Match" Buttons="">
		<pane Orientation="Verticall" HorizResize="Fixed">
			<pushbutton Name=" Input Directory " OnPressed="GetInDir()"/>
			<edittext id="indirtext" width="35" ReadOnly="true"/>
		</pane>
		<pane Orientation="Horizontal" HorizResize="Fixed">
			<pane Orientation="Vertical" HorizResize="Fixed" VertResize="Fixed">
				<label>Files</label>
				<listbox id="filelistbox" Width="15"/>
			</pane>
			<pane Orientation="Vertical" HorizResize="Fixed" VertResize="Fixed">
				<label>Result</label>
				<listbox id="resultlistbox" Width="15"/>
			</pane>
		</pane>
		<pane Orientation="Horizontal" HorizAlign="left">
			<edittext id="statustext" Width="20" ReadOnly="true"/>
		</pane>
		<pane Orientation="Horizontal" HorizAlign="right">
			<pushbutton id="runBtn" Name=" Run " Enabled="false" OnPressed="onRun()"/>
			<pushbutton id="closeBtn" Name=" Close " OnPressed="onClose()"/>
		</pane>
	</dialog>
</root>';
### parse XML text for the dialog into memory
class XMLDOC dlgdoc;
err = dlgdoc.Parse(xml$); 
### get the dialog element from the parsed XML document
class XMLNODE dlgnode;
dlgnode = dlgdoc.GetElementByID("checkNames");
### set the dialog XML element as the source for the GUI_DLG class instance
dlgwin.SetXMLNode(dlgnode);
dlgwin.CreateModeless();
### get handles for dialog controls
indirtext = dlgwin.GetCtrlByID("indirtext");
runBtn = dlgwin.GetCtrlByID("runBtn");
filelistbox = dlgwin.GetCtrlByID("filelistbox");
resultlistbox = dlgwin.GetCtrlByID("resultlistbox");
statustext = dlgwin.GetCtrlByID("statustext");
dlgwin.Open();
WaitForExit();
################    USER-DEFINED FUNCTIONS    ########################
### procedure to choose the input directory
proc GetInDir()
	{ 
	inDir$ = GetDirectory(_context.ScriptDir, "Choose directory with Project Files to check:");
	class FILEPATH inDirPath(inDir$);
	indirtext.SetValueStr(inDir$);				# write directory name to dialog
	# clear dialog listboxes if they are still populated from a previous run
	if (filelistbox.GetCount() > 0) then filelistbox.DeleteAllItems();
	if (resultlistbox.GetCount() >0) then resultlistbox.DeleteAllItems();
	# clear status text field
	statustext.SetValueStr("");		# clear status message text field
	# get list of RVC Files in the directory
	filelist = inDirPath.GetFileList("*.rvc");
	for i = 1 to filelist.GetNumItems()
		{
		filelistbox.AddItem( filelist[i-1] );
		}
	runBtn.SetEnabled(1);
	}
############################
### procedure called by the Close button
proc onClose()
	{
	dlgwin.Close(0);
	Exit();
	}
### procedure to create the log file
proc makeLogfile()
	{
	errorsFound = 1;
	local class DATETIME dt;
	dt.SetCurrent();
	dt.ConvertToLocal();
	local class STRING date$ = sprintf("%d", dt.GetDateYYYYMMDD() );
	local class STRING logfilename$ = sprintf("%s/NameCheck_%s.log", inDir$, date$);
	logfile = fopen(logfilename$, "a");
	fprintf(logfile, "Log for check of match of file/object names in directory %s\n", inDir$);
	}
### procedure executed by the Run button
proc onRun()
	{
	errorsFound = 0;
	for i = 1 to filelist.GetNumItems()
		{
		fileNameRoot$ = GetToken(filelist[i-1], ".", 1); 
		filePath$ = inDir$ + "/" + filelist[i-1];
		class FILEPATH rvcFilepath(filePath$);
		rvcFile.OpenFile(rvcFilepath, "Read");
		# get list of raster objects in the file
		rvcFile.GetChildList("RASTER", objItemList);
		if (objItemList.GetNumItems() > 1)		# error: more than one raster object
			{
			resultlistbox.AddItem( sprintf("%d rasters in file", objItemList.GetNumItems()) );
			if (!errorsFound) then makeLogfile();
			
			fprintf(logfile, "File %s: %d rasters in file\n", filelist[i-1], objItemList.GetNumItems() );
			}
		else			# only one raster object in file, check if names match
			{
			rasterName$ = objItemList[1].GetDescriptor().GetFullName();
			if (rasterName$ <> fileNameRoot$)		# error:  names don't match
				{
				resultlistbox.AddItem("name mismatch");
				if (!errorsFound) then makeLogfile();
 
				fprintf(logfile, "File %s: name mismatch\n", filelist[i-1]);			
				}	
			else if (rasterName$ == fileNameRoot$) then
				resultlistbox.AddItem("OK");			# no error
			}
		}
	if (errorsFound)
		{
		statustext.SetValueStr("Errors found, written to logfile.");
		fclose(logfile);
		}
	else
		statustext.SetValueStr("No errors found in directory.");
	runBtn.SetEnabled(0);
	}  # end onRun()