home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1997 January
/
Chip_1997-01_cd.bin
/
ms95
/
disk21
/
dir04
/
f015630.re_
/
f015630.re
Wrap
Text File
|
1996-04-02
|
13KB
|
430 lines
/*----------------------------------------------------------------------+
| |
| Copyright (1993-95) Bentley Systems, Inc., All rights reserved. |
| |
| "MicroStation" is a registered trademark and "MDL" and "MicroCSL" |
| are trademarks of Bentley Systems, Inc. |
| |
| Limited permission is hereby granted to reproduce and modify this |
| copyrighted material provided that the resulting code is used only |
| in conjunction with Bentley Systems products under the terms of the |
| license agreement provided therein, and that this notice is retained |
| in its entirety in any such reproduction or modification. |
| |
+----------------------------------------------------------------------*/
/*----------------------------------------------------------------------+
| |
| $Workfile: listfile.mc $
| $Revision: 5.4 $
| $Date: 26 Jul 1995 07:20:30 $
| |
+----------------------------------------------------------------------*/
/*----------------------------------------------------------------------+
| |
| Function - |
| |
| listfile.mc -- Standalone MDL file lister |
| |
| This program, similar to that found in the built-in command in |
| MicroStation under the "files" pulldown menu, lists the contents |
| of straight ascii files. It demonstrates the use of the List |
| dialog item. |
| |
| The file name can be entered through the |
| MDL LOAD command, through the listfile command LISTFILE, or through |
| the file/load Menubar pulldown. If a filename is not specified in |
| the load or command line, or when the file/load menubar is selected,|
| the mdlDialog_fileOpen built-in is called to get the filename. |
| |
| examples: |
| |
| mdl load \ustn40\mdl\examples\listfile <fileToList> |
| listfile <fileToList> |
| |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| |
| Public Routine Summary - |
| |
| listfile_command - LISTFILE command function |
| listfile_fileContentsListHook - List box hook function |
| listfile_fileOpenMenuItemHook - Open menu item hook function |
| listfile_getfile - Get file to be listed |
| listfile_loadText - Open file and load text into string list |
| listfile_setListContents - Set list contents and dialog title |
| listfile_unloadHook - Application unload asynch function |
| main - main entry point |
| |
+----------------------------------------------------------------------*/
/*----------------------------------------------------------------------+
| |
| Include Files |
| |
+----------------------------------------------------------------------*/
#include <mdl.h>
#include <mdlio.h>
#include <dlogbox.h>
#include <dlogitem.h>
#include <rscdefs.h>
#include <userfnc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "listfids.h" /* contains the dialog ids for this example */
#include "listfcmd.h" /* contains the command numbers for this example */
#include <dlogman.fdf> /* Dialog Box Manager Function Prototypes */
#include <mselemen.fdf>
#include <msparse.fdf>
#include <msoutput.fdf>
#include <msrsrc.fdf>
#include <mssystem.fdf>
/*----------------------------------------------------------------------+
| |
| Local defines |
| |
+----------------------------------------------------------------------*/
#define BATCH_SIZE 100
/*----------------------------------------------------------------------+
| |
| Local type definitions |
| |
+----------------------------------------------------------------------*/
typedef struct
{
char fileName[MAXFILELENGTH];
int numLines;
char *fileTextStringListP;
} FileContents;
/*----------------------------------------------------------------------+
| |
| Private global variables |
| |
+----------------------------------------------------------------------*/
Private FileContents *fileContentsP = NULL;
/*----------------------------------------------------------------------+
| |
| Listfile Code Section |
| |
+----------------------------------------------------------------------*/
/*----------------------------------------------------------------------+
| |
| name listfile_loadText |
| |
| author BSI 7/90 |
| |
+----------------------------------------------------------------------*/
Private boolean listfile_loadText /* <= TRUE if error */
(
char *fileName /* => name of file to load */
)
{
int numLines, linesAllocated;
char inBuffer[80], outBuffer[80], *p;
long fileSize;
FILE *fp;
/* Determine the file size */
if (! (fp = fopen (fileName, "r")))
{
char msgString[80+MAXFILELENGTH];
strcpy (msgString, "Unable to Open ");
strcat (msgString, fileName);
mdlOutput_error (msgString);
return (TRUE);
}
else
{
if (fileContentsP)
{
mdlStringList_destroy (fileContentsP->fileTextStringListP);
free (fileContentsP);
}
fileContentsP = malloc (sizeof(*fileContentsP));
fileContentsP->fileTextStringListP =
mdlStringList_create (BATCH_SIZE, 1);
numLines = 0;
linesAllocated = BATCH_SIZE;
while (fgets (inBuffer, sizeof(inBuffer), fp))
{
if (p = strchr (inBuffer, LF)) *p = '\0';
if (numLines >= linesAllocated)
{
linesAllocated += BATCH_SIZE;
mdlStringList_insertMember
(NULL, fileContentsP->fileTextStringListP,
-1, BATCH_SIZE); /* Place at end of Stringlist */
}
mdlText_expandTabs (outBuffer, inBuffer, sizeof(outBuffer), 8);
mdlStringList_setMember(fileContentsP->fileTextStringListP,
numLines, outBuffer, NULL);
numLines++;
}
fileContentsP->numLines = numLines;
fclose (fp);
strcpy(fileContentsP->fileName, fileName);
return (FALSE);
}
}
/*----------------------------------------------------------------------+
| |
| name listfile_setListContents |
| |
| comments sets contents of list item, and title of dialog box |
| |
| author BSI 10/90 |
| |
+----------------------------------------------------------------------*/
Private void listfile_setListContents
(
DialogBox *db, /* => dialog box that contains list item */
RawItemHdr *listP /* => list to set contents of */
)
{
/* set the list contents up for fileContentsP->numLines rows and only
one column */
mdlDialog_listBoxSetStrListP (listP,fileContentsP->fileTextStringListP,1);
mdlWindow_titleSet (db, fileContentsP->fileName);
}
/*----------------------------------------------------------------------+
| |
| Dialog Item Hook Functions |
| |
+----------------------------------------------------------------------*/
/*----------------------------------------------------------------------+
| |
| name listfile_fileContentsListHook |
| |
| author BSI 10/90 |
| |
+----------------------------------------------------------------------*/
Private void listfile_fileContentsListHook
(
DialogItemMessage *dimP /* => dialog item message */
)
{
dimP->msgUnderstood = TRUE;
switch (dimP->messageType)
{
case DITEM_MESSAGE_CREATE:
{
Rectangle *dRectP;
dRectP = &dimP->dialogItemP->rect;
listfile_setListContents (dimP->db, dimP->dialogItemP->rawItemP);
mdlWindow_extentSet (dimP->db, dRectP->corner.x,
dRectP->corner.y);
break;
}
case DITEM_MESSAGE_DESTROY:
{
mdlStringList_destroy (fileContentsP->fileTextStringListP);
free (fileContentsP);
fileContentsP = NULL;
break;
}
default:
{
dimP->msgUnderstood = FALSE;
break;
}
}
}
/*----------------------------------------------------------------------+
| |
| name listfile_getfile |
| |
| comments Examines fileName and gets the name of a file if none |
| already entered. After it has a file name, it attempts |
| to open file and load up fileList. |
| |
| |
| author BSI 10/90 |
| |
+----------------------------------------------------------------------*/
Private boolean listfile_getfile /* <= TRUE if error */
(
char *tempFileName /* => name of file to read */
)
{
char fileName[MAXFILELENGTH];
/* See if file name established yet */
if (strlen(tempFileName))
{
strcpy(fileName, tempFileName);
}
else
{
/* Get file name using built-in file open dialog */
if (mdlDialog_fileOpen (fileName, NULL, 0, NULL, NULL, NULL,
"File to display:"))
{
/* cancel button hit in dialog box */
return (TRUE);
}
}
return (listfile_loadText (fileName));
}
/*----------------------------------------------------------------------+
| |
| name listfile_fileOpenMenuItemHook |
| |
| comments Attached to "File / Open" menu item within dialog box |
| |
| author BSI 10/90 |
| |
+----------------------------------------------------------------------*/
Private void listfile_fileOpenMenuItemHook
(
DialogItemMessage *dimP /* => dialog item message */
)
{
dimP->msgUnderstood = TRUE;
switch (dimP->messageType)
{
case DITEM_MESSAGE_BUTTON:
{
if (dimP->u.button.buttonTrans != BUTTONTRANS_UP)
break;
if (!listfile_getfile(""))
{
DialogItem *diP;
RawItemHdr *listP;
/* get pointer to text list item in dialog box */
diP = mdlDialog_itemGetByTypeAndId (dimP->db,
RTYPE_ListBox, LISTBOXID_FileContents, 0);
listP = diP->rawItemP;
if (listP)
{
listfile_setListContents (dimP->db, listP);
mdlDialog_itemDraw (dimP->db, diP->itemIndex);
}
}
break;
}
default:
{
dimP->msgUnderstood = FALSE;
break;
}
}
}
/*----------------------------------------------------------------------+
| |
| name listfile_unloadHook |
| |
| comments this is used to free the stringlist when this |
| application is unloaded. |
| |
| author BSI 10/90 |
| |
+----------------------------------------------------------------------*/
Private int listfile_unloadHook
(
void /* listfile_unloadHook ignores the parameters so
there is no need to declare them */
)
{
if (fileContentsP)
{
if (fileContentsP->fileTextStringListP)
mdlStringList_destroy (fileContentsP->fileTextStringListP);
free (fileContentsP);
}
/* Return 0 to let the unload proceed. */
return 0;
}
/*----------------------------------------------------------------------+
| |
| name listfile_command |
| |
| comments The only command in the command table. |
| |
| author BSI 10/90 |
| |
+----------------------------------------------------------------------*/
Private int listfile_command
(
char *fileName /* => name of file to list */
)
cmdNumber CMD_LISTFILE
{
if (!listfile_getfile (fileName))
{
/* Open the ListFile dialog box. The NULL argument indicates that the
ListFile dialog box resource can be found in this program's
open resource files. This argument can also be a
RscFileHandle if you wish to restrict the search to one file. */
mdlDialog_open (NULL, DIALOGID_Listfile);
return (SUCCESS);
}
else
{
return (ERROR);
}
}
/*----------------------------------------------------------------------+
| |
| name main |
| |
| author BSI 9/90 |
| |
+----------------------------------------------------------------------*/
Private DialogHookInfo uHooks[] =
{
{HOOKITEMID_ListBox_FileContents, listfile_fileContentsListHook},
{HOOKITEMID_MenuItem_FileOpen, listfile_fileOpenMenuItemHook},
};
Public int main
(
int argc, /* => Number of arguments passed in argv */
char *argv[] /* => Array of pointers to arguments */
)
{
RscFileHandle rfHandle;
char *setP;
char fileName[MAXFILELENGTH];
/* load our command table, from resources merged into listfile.ma */
if (!mdlParse_loadCommandTable (NULL))
mdlOutput_error ("Unable to load command table");
/* Publish the dialog item hooks */
mdlDialog_hookPublish (sizeof(uHooks)/sizeof(DialogHookInfo), uHooks);
mdlSystem_setFunction (SYSTEM_UNLOAD_PROGRAM, listfile_unloadHook);
/* Open the resource file from which this program was loaded (listfile.ma)
so we can access (READONLY) our resources */
mdlResource_openFile (&rfHandle, NULL, FALSE);
/* Something was passed in load statement - assume it was file name */
if (argc>2)
strcpy(fileName, argv[2]);
else
fileName[0] = '\0';
return (listfile_command(fileName));
}