home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 January / Chip_1997-01_cd.bin / ms95 / disk21 / dir04 / f015630.re_ / f015630.re
Text File  |  1996-04-02  |  13KB  |  430 lines

  1. /*----------------------------------------------------------------------+
  2. |                                    |
  3. |  Copyright (1993-95) Bentley Systems, Inc., All rights reserved.         |
  4. |                                    |
  5. |  "MicroStation" is a registered trademark and "MDL" and "MicroCSL"    |
  6. |  are trademarks of Bentley Systems, Inc.                    |
  7. |                                    |
  8. |  Limited permission is hereby granted to reproduce and modify this    |
  9. |  copyrighted material provided that the resulting code is used only     |
  10. |  in conjunction with Bentley Systems products under the terms of the    |
  11. |  license agreement provided therein, and that this notice is retained    |
  12. |  in its entirety in any such reproduction or modification.        |
  13. |                                    |
  14. +----------------------------------------------------------------------*/
  15. /*----------------------------------------------------------------------+
  16. |                                    |
  17. |   $Workfile:   listfile.mc  $
  18. |   $Revision:   5.4  $
  19. |       $Date:   26 Jul 1995 07:20:30  $
  20. |                                    |
  21. +----------------------------------------------------------------------*/
  22. /*----------------------------------------------------------------------+
  23. |                                    |
  24. |   Function -                                |
  25. |                                    |
  26. |   listfile.mc -- Standalone MDL file lister                |
  27. |                                    |
  28. |   This program, similar to that found in the built-in command in    |
  29. |   MicroStation under the "files" pulldown menu, lists the contents    |
  30. |   of straight ascii files.  It demonstrates the use of the List    |
  31. |   dialog item.                            |
  32. |                                    |
  33. |   The file name can be entered through the                |
  34. |   MDL LOAD command, through the listfile command LISTFILE, or through |
  35. |   the file/load Menubar pulldown.  If a filename is not specified in  |
  36. |   the load or command line, or when the file/load menubar is selected,|
  37. |   the mdlDialog_fileOpen built-in is called to get the filename.    |
  38. |                                    |
  39. |     examples:                                |
  40. |                                    |
  41. |   mdl load \ustn40\mdl\examples\listfile <fileToList>            |
  42. |   listfile <fileToList>                        |
  43. |                                    |
  44. |    - - - - - - - - - - - - - - - - - - - - - - - - - - - - -    |
  45. |                                    |
  46. |   Public Routine Summary -                        |
  47. |                                    |
  48. |    listfile_command - LISTFILE command function            |
  49. |    listfile_fileContentsListHook - List box hook function        |
  50. |    listfile_fileOpenMenuItemHook - Open menu item hook function    |
  51. |    listfile_getfile - Get file to be listed            |
  52. |    listfile_loadText - Open file and load text into string list    |
  53. |    listfile_setListContents - Set list contents and dialog title    |
  54. |    listfile_unloadHook - Application unload asynch function    |
  55. |    main - main entry point                        |
  56. |                                    |
  57. +----------------------------------------------------------------------*/
  58. /*----------------------------------------------------------------------+
  59. |                                    |
  60. |   Include Files                               |
  61. |                                    |
  62. +----------------------------------------------------------------------*/
  63. #include <mdl.h>
  64. #include <mdlio.h>
  65. #include <dlogbox.h>
  66. #include <dlogitem.h>
  67. #include <rscdefs.h>
  68. #include <userfnc.h>
  69. #include <stdio.h>
  70. #include <stdlib.h>
  71. #include <string.h>
  72.  
  73. #include "listfids.h"    /* contains the dialog ids for this example */
  74. #include "listfcmd.h"    /* contains the command numbers for this example */
  75.  
  76. #include <dlogman.fdf>  /* Dialog Box Manager Function Prototypes */
  77. #include <mselemen.fdf> 
  78. #include <msparse.fdf> 
  79. #include <msoutput.fdf> 
  80. #include <msrsrc.fdf> 
  81. #include <mssystem.fdf> 
  82.  
  83. /*----------------------------------------------------------------------+
  84. |                                    |
  85. |   Local defines                            |
  86. |                                    |
  87. +----------------------------------------------------------------------*/
  88. #define BATCH_SIZE 100
  89.  
  90. /*----------------------------------------------------------------------+
  91. |                                    |
  92. |   Local type definitions                        |
  93. |                                    |
  94. +----------------------------------------------------------------------*/
  95. typedef struct
  96.     {
  97.     char    fileName[MAXFILELENGTH];
  98.     int        numLines;
  99.     char    *fileTextStringListP;
  100.     } FileContents;
  101.  
  102. /*----------------------------------------------------------------------+
  103. |                                    |
  104. |   Private global variables                        |
  105. |                                    |
  106. +----------------------------------------------------------------------*/
  107. Private FileContents    *fileContentsP = NULL;
  108.  
  109. /*----------------------------------------------------------------------+
  110. |                                    |
  111. |   Listfile Code Section                        |
  112. |                                    |
  113. +----------------------------------------------------------------------*/
  114. /*----------------------------------------------------------------------+
  115. |                                    |
  116. | name        listfile_loadText                    |
  117. |                                    |
  118. | author    BSI                    7/90        |
  119. |                                    |
  120. +----------------------------------------------------------------------*/
  121. Private boolean listfile_loadText        /* <= TRUE if error */
  122. (
  123. char    *fileName        /* => name of file to load */
  124. )
  125.     {
  126.     int        numLines, linesAllocated;
  127.     char    inBuffer[80], outBuffer[80], *p;
  128.     long    fileSize;
  129.     FILE    *fp;
  130.  
  131.     /* Determine the file size */
  132.     if (! (fp = fopen (fileName, "r")))
  133.     {
  134.     char    msgString[80+MAXFILELENGTH];
  135.  
  136.     strcpy (msgString, "Unable to Open ");
  137.     strcat (msgString, fileName);
  138.     mdlOutput_error (msgString);
  139.     return (TRUE);
  140.     }
  141.     else
  142.     {
  143.     if (fileContentsP)
  144.         {
  145.         mdlStringList_destroy (fileContentsP->fileTextStringListP);
  146.         free (fileContentsP);
  147.         }
  148.     fileContentsP = malloc (sizeof(*fileContentsP));
  149.     fileContentsP->fileTextStringListP =
  150.         mdlStringList_create (BATCH_SIZE, 1);
  151.  
  152.     numLines    = 0;
  153.     linesAllocated    = BATCH_SIZE;
  154.  
  155.     while (fgets (inBuffer, sizeof(inBuffer), fp))
  156.         {
  157.         if (p = strchr (inBuffer, LF)) *p = '\0';
  158.         if (numLines >= linesAllocated)
  159.         {
  160.         linesAllocated += BATCH_SIZE;
  161.         mdlStringList_insertMember
  162.             (NULL, fileContentsP->fileTextStringListP,
  163.             -1, BATCH_SIZE);   /* Place at end of Stringlist */
  164.         }
  165.         mdlText_expandTabs (outBuffer, inBuffer, sizeof(outBuffer), 8);
  166.         mdlStringList_setMember(fileContentsP->fileTextStringListP,
  167.         numLines, outBuffer, NULL);
  168.         numLines++;
  169.         }
  170.     fileContentsP->numLines = numLines;
  171.     fclose (fp);
  172.     strcpy(fileContentsP->fileName, fileName);
  173.  
  174.     return (FALSE);
  175.     }
  176.     }
  177.  
  178. /*----------------------------------------------------------------------+
  179. |                                    |
  180. | name        listfile_setListContents                |
  181. |                                    |
  182. | comments    sets contents of list item, and title of dialog box     |
  183. |                                    |
  184. | author    BSI                     10/90        |
  185. |                                    |
  186. +----------------------------------------------------------------------*/
  187. Private void listfile_setListContents
  188. (
  189. DialogBox    *db,        /* => dialog box that contains list item */
  190. RawItemHdr  *listP        /* => list to set contents of */
  191. )
  192.     {
  193.     /* set the list contents up for fileContentsP->numLines rows and only
  194.        one column */
  195.     mdlDialog_listBoxSetStrListP (listP,fileContentsP->fileTextStringListP,1);
  196.     mdlWindow_titleSet (db, fileContentsP->fileName);
  197.     }
  198.  
  199. /*----------------------------------------------------------------------+
  200. |                                    |
  201. |   Dialog Item Hook Functions                        |
  202. |                                    |
  203. +----------------------------------------------------------------------*/
  204. /*----------------------------------------------------------------------+
  205. |                                    |
  206. | name        listfile_fileContentsListHook               |
  207. |                                    |
  208. | author    BSI                     10/90        |
  209. |                                    |
  210. +----------------------------------------------------------------------*/
  211. Private void listfile_fileContentsListHook
  212. (
  213. DialogItemMessage   *dimP    /* => dialog item message */
  214. )
  215.     {
  216.     dimP->msgUnderstood = TRUE;
  217.     switch (dimP->messageType)
  218.     {
  219.     case DITEM_MESSAGE_CREATE:
  220.         {
  221.         Rectangle    *dRectP;
  222.  
  223.         dRectP = &dimP->dialogItemP->rect;
  224.         listfile_setListContents (dimP->db, dimP->dialogItemP->rawItemP);
  225.         mdlWindow_extentSet (dimP->db, dRectP->corner.x,
  226.                  dRectP->corner.y);
  227.         break;
  228.         }
  229.  
  230.     case DITEM_MESSAGE_DESTROY:
  231.         {
  232.         mdlStringList_destroy (fileContentsP->fileTextStringListP);
  233.             free (fileContentsP);
  234.         fileContentsP = NULL;
  235.         break;
  236.         }
  237.     default:
  238.         {
  239.         dimP->msgUnderstood = FALSE;
  240.         break;
  241.         }
  242.     }
  243.     }
  244.  
  245. /*----------------------------------------------------------------------+
  246. |                                    |
  247. | name        listfile_getfile                    |
  248. |                                    |
  249. | comments    Examines fileName and gets the name of a file if none    |
  250. |        already entered.  After it has a file name, it attempts    |
  251. |        to open file and load up fileList.            |
  252. |                                    |
  253. |                                    |
  254. | author    BSI                     10/90        |
  255. |                                    |
  256. +----------------------------------------------------------------------*/
  257. Private boolean listfile_getfile    /* <= TRUE if error */
  258. (
  259. char    *tempFileName        /* => name of file to read */
  260. )
  261.     {
  262.     char    fileName[MAXFILELENGTH];
  263.  
  264.     /* See if file name established yet */
  265.     if (strlen(tempFileName))
  266.     {
  267.     strcpy(fileName, tempFileName);
  268.     }
  269.     else
  270.     {
  271.     /* Get file name using built-in file open dialog */
  272.     if (mdlDialog_fileOpen (fileName, NULL, 0, NULL, NULL, NULL,
  273.                 "File to display:"))
  274.         {
  275.         /* cancel button hit in dialog box */
  276.         return (TRUE);
  277.         }
  278.     }
  279.  
  280.     return (listfile_loadText (fileName));
  281.     }
  282.  
  283. /*----------------------------------------------------------------------+
  284. |                                    |
  285. | name        listfile_fileOpenMenuItemHook                |
  286. |                                    |
  287. | comments    Attached to "File / Open" menu item within dialog box    |
  288. |                                    |
  289. | author    BSI                     10/90        |
  290. |                                    |
  291. +----------------------------------------------------------------------*/
  292. Private void listfile_fileOpenMenuItemHook
  293. (
  294. DialogItemMessage   *dimP    /* => dialog item message */
  295. )
  296.     {
  297.     dimP->msgUnderstood = TRUE;
  298.     switch (dimP->messageType)
  299.     {
  300.     case DITEM_MESSAGE_BUTTON:
  301.         {
  302.         if (dimP->u.button.buttonTrans != BUTTONTRANS_UP)
  303.         break;
  304.  
  305.         if (!listfile_getfile(""))
  306.         {
  307.         DialogItem  *diP;
  308.         RawItemHdr  *listP;
  309.  
  310.         /* get pointer to text list item in dialog box */
  311.         diP = mdlDialog_itemGetByTypeAndId (dimP->db,
  312.                     RTYPE_ListBox, LISTBOXID_FileContents, 0);
  313.         listP = diP->rawItemP;
  314.         if (listP)
  315.             {
  316.             listfile_setListContents (dimP->db, listP);
  317.             mdlDialog_itemDraw (dimP->db, diP->itemIndex);
  318.             }
  319.         }
  320.         break;
  321.         }
  322.     default:
  323.         {
  324.         dimP->msgUnderstood = FALSE;
  325.         break;
  326.         }
  327.     }
  328.     }
  329.  
  330. /*----------------------------------------------------------------------+
  331. |                                    |
  332. | name        listfile_unloadHook                     |
  333. |                                    |
  334. | comments    this is used to free the stringlist when this           |
  335. |        application is unloaded.                |
  336. |                                    |
  337. | author    BSI                     10/90        |
  338. |                                    |
  339. +----------------------------------------------------------------------*/
  340. Private int listfile_unloadHook
  341. (
  342. void        /*  listfile_unloadHook ignores the parameters so
  343.         there is no need to declare them */
  344. )
  345.     {
  346.     if (fileContentsP)
  347.     {
  348.     if (fileContentsP->fileTextStringListP)
  349.         mdlStringList_destroy (fileContentsP->fileTextStringListP);
  350.     free (fileContentsP);
  351.     }
  352.     /*  Return 0 to let the unload proceed. */
  353.     return 0;
  354.     }
  355.  
  356. /*----------------------------------------------------------------------+
  357. |                                    |
  358. | name        listfile_command                    |
  359. |                                    |
  360. | comments    The only command in the command table.            |
  361. |                                    |
  362. | author    BSI                     10/90        |
  363. |                                    |
  364. +----------------------------------------------------------------------*/
  365. Private int listfile_command
  366. (
  367. char *fileName            /* => name of file to list */
  368. )
  369. cmdNumber CMD_LISTFILE
  370.     {
  371.     if (!listfile_getfile (fileName))
  372.     {
  373.     /* Open the ListFile dialog box.  The NULL argument indicates that the
  374.        ListFile dialog box resource can be found in this program's
  375.        open resource files.  This argument can also be a
  376.        RscFileHandle if you wish to restrict the search to one file. */
  377.     mdlDialog_open (NULL, DIALOGID_Listfile);
  378.     return (SUCCESS);
  379.     }
  380.     else
  381.     {
  382.     return (ERROR);
  383.     }
  384.     }
  385.  
  386. /*----------------------------------------------------------------------+
  387. |                                    |
  388. | name        main                            |
  389. |                                    |
  390. | author    BSI                    9/90        |
  391. |                                    |
  392. +----------------------------------------------------------------------*/
  393. Private DialogHookInfo uHooks[] =
  394.     {
  395.     {HOOKITEMID_ListBox_FileContents, listfile_fileContentsListHook},
  396.     {HOOKITEMID_MenuItem_FileOpen, listfile_fileOpenMenuItemHook},
  397.     };
  398.  
  399. Public int main
  400. (
  401. int    argc,    /* => Number of arguments passed in argv */
  402. char   *argv[]    /* => Array of pointers to arguments */
  403. )
  404.     {
  405.     RscFileHandle    rfHandle;
  406.     char        *setP;
  407.     char         fileName[MAXFILELENGTH];
  408.  
  409.     /* load our command table, from resources merged into listfile.ma */
  410.     if (!mdlParse_loadCommandTable (NULL))
  411.     mdlOutput_error ("Unable to load command table");
  412.  
  413.     /* Publish the dialog item hooks */
  414.     mdlDialog_hookPublish (sizeof(uHooks)/sizeof(DialogHookInfo), uHooks);
  415.  
  416.     mdlSystem_setFunction (SYSTEM_UNLOAD_PROGRAM, listfile_unloadHook);
  417.  
  418.     /* Open the resource file from which this program was loaded (listfile.ma)
  419.        so we can access (READONLY) our resources */
  420.     mdlResource_openFile (&rfHandle, NULL, FALSE);
  421.  
  422.     /* Something was passed in load statement - assume it was file name  */
  423.     if (argc>2)
  424.     strcpy(fileName, argv[2]);
  425.     else
  426.     fileName[0] = '\0';
  427.  
  428.     return (listfile_command(fileName));
  429.     }
  430.