home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / VPR_DATA / DOGA / SOURCES / POLYEDIT.LZH / MODEL / MDLINIT.C < prev    next >
C/C++ Source or Header  |  1996-07-09  |  12KB  |  580 lines

  1. /*
  2.  *        初期化ルーチン
  3.  *
  4.  *        Copyright T.Kobayashi    1994.7.9
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11. #include <io.h>
  12. #include <assert.h>
  13. #ifdef    X68K
  14. #    include <doslib.h>
  15. #endif
  16.  
  17. #include "lib.h"
  18. #include "inlib.h"
  19. #include "matrix.h"
  20. #include "reader.h"
  21. #include "matclass.h"
  22. #include "ml.h"
  23.  
  24. #include "model.h"
  25.  
  26. #include "graph.h"
  27. #include "input.h"
  28.  
  29. static    char    *GetWord( char*, char* );
  30. static    char    *SearchPath( char*, char*, char* );
  31. static    void    SetPalet( void );
  32. static    int        ModelBreakCheck( void );
  33.  
  34. char    **ModelrcName ;
  35. char    **ModelrcValue ;
  36. int        ModelrcSize;
  37.  
  38. /*char    *Version = "1.1.0" ;*/
  39. extern    char *Version;
  40. #ifdef    X68K
  41.     char    *Environment = "X68k" ;
  42. #endif
  43. #ifdef    PC98
  44.     char    *Environment = "PC98" ;
  45. #endif
  46. #ifdef    PC
  47.     char    *Environment = "IBMPC" ;
  48. #endif
  49. #ifdef    WINDOWS
  50.     char    *Environment = "WINDOWS" ;
  51. #endif
  52. #ifdef    UNIX
  53.     char    *Environment = "UNIX" ;
  54. #endif
  55.  
  56. #ifdef    X68K
  57.     char    CommandFile[64] ;
  58. #endif
  59. extern    char **Argv;
  60. extern    int     Argc;
  61.     char    ExecPath[512] ;
  62.  
  63. static char modelrc[512];
  64. static char modelrc_old[512];
  65. static char modelrc_new[512];
  66.  
  67. /*    初期化    */
  68. void    ModelInit()
  69. {
  70.     int        i, flag, line ;
  71.     FILE    *fp ;
  72.     char    str[256] ;
  73.     char    path[256], buf[256], *p ;
  74.     int        codesize ;
  75. #if defined(WINDOWS)
  76.     char    *file = "PolyEdit.ini" ;
  77. #elif defined(UNIX)
  78.     char    *file = ".polyeditrc";
  79. #else
  80.     char    *file = "Model.rc" ;
  81. #endif
  82.     char    pathenv[1024] ;
  83.     int        bufsize = 128 ;
  84.     int        maxobj = 16 ;
  85.     int        maxatr = 256 ;
  86.     int        maxver = 256 ;
  87.  
  88.     if (Argc > 1 && strcmp(Argv[1], "--dump") == 0) {
  89.         for (i = 2; i < Argc; ++i) {
  90.             Argv[i-1] = Argv[i];
  91.         }
  92.         Argc--;
  93.         DumpMode( TRUE );
  94.     }
  95.  
  96.     strcpy(ExecPath, Argv[0]);
  97.     if ((p = strrchr(ExecPath, '\\')) != NULL) {
  98.         p[1] = '\0';
  99.     } else if (ExecPath[0] != '\0' && ExecPath[1] == ':') {
  100.         ExecPath[2] = '\0';
  101.     } else {
  102.         ExecPath[0] = '\0';
  103.     }
  104.  
  105. #ifdef    X68K
  106.     strcpy( CommandFile, "A:\\COMMAND.X" );
  107. #endif
  108.  
  109.     LibraryInit();
  110.     MatrixInit();
  111.     ReaderInit();
  112.     MLInit();
  113.  
  114.     if ( getenv( "MODELPATH" ) != NULL ) {
  115.         strcpy(pathenv, getenv("MODELPATH")) ;
  116.     } else {
  117.  
  118. #ifndef UNIX
  119.         if (getenv( "HOME" ) != NULL) {
  120.             sprintf(pathenv, "%s;%s;%sMACRO;%sPOLYEDIT;%sMODEL",
  121.                         getenv("HOME"),ExecPath, ExecPath, ExecPath, ExecPath);
  122.         } else if (getenv( "home" ) != NULL) {
  123.             sprintf(pathenv, "%s;%s;%sMACRO;%sPOLYEDIT;%sMODEL",
  124.                         getenv("home"),ExecPath, ExecPath, ExecPath, ExecPath);
  125.         } else {
  126.             sprintf(pathenv, "%s;%sMACRO;%sPOLYEDIT;%sMODEL",
  127.                         ExecPath, ExecPath, ExecPath, ExecPath);
  128.         }
  129. #else
  130.         if (getenv( "HOME") != NULL) {
  131.             strcpy(pathenv, getenv("HOME"));
  132.         } else if (getenv( "home") != NULL) {
  133.             strcpy(pathenv, getenv("home"));
  134.         } else {
  135.             pathenv[0] = '\0';
  136.         }
  137. #endif
  138.     }
  139.  
  140.     /*    設定ファイルの読み込み    */
  141.     if ( SearchPath( path, pathenv, file ) == NULL )
  142.     {
  143.         sprintf( buf, "ファイル %s が存在しません。", file );
  144.         MessageError( buf );
  145.     }
  146.     strcpy(modelrc, path);
  147.     strcpy(modelrc_new, path);
  148.     strcpy(modelrc_old, path);
  149.     p = strrchr(modelrc, '.');
  150.     if (p == NULL) {
  151.         i = strlen(modelrc);
  152.     } else {
  153.         i = p - modelrc;
  154.     }
  155.     strcpy(modelrc_old + i, ".bak");
  156.     strcpy(modelrc_new + i, ".tmp");
  157.  
  158.     fp = fopen( path, "r" );
  159.     assert( fp != NULL );
  160.     line = 0 ;
  161.     while( fgets( buf, 255, fp ) != NULL )
  162.     {
  163.         p = GetWord( buf, str );
  164.         if ( strcmp( str, "CodeSize" ) == 0 )
  165.             SetMaxCodeSize( atoi( p ) * 1024 );
  166.         else if ( strcmp( str, "Global" ) == 0 )
  167.             SetMaxGlobals( atoi( p ) );
  168.         else if ( strcmp( str, "Function" ) == 0 )
  169.             SetMaxFunctions( atoi( p ) );
  170.         else if ( strcmp( str, "Stack" ) == 0 )
  171.             SetMaxStacks( atoi( p ) );
  172.         else if ( strcmp( str, "BufferSize" ) == 0 )
  173.             bufsize = atoi( p );
  174.         else if ( strcmp( str, "Attr" ) == 0 )
  175.             maxatr = atoi( p );
  176.         else if ( strcmp( str, "Object" ) == 0 )
  177.             maxobj = atoi( p );
  178.         else if ( strcmp( str, "Vertexs" ) == 0 )
  179.             maxver = atoi( p );
  180.         else if ( strcmp( str, "Path" ) == 0)
  181.             strcpy(pathenv, p);
  182. #if    X68K
  183.         else if ( strcmp( str, "CommandFile" ) == 0 )
  184.             GetWord( p, CommandFile );
  185. #endif
  186.         line ++ ;
  187.     }
  188.     fclose( fp );
  189.  
  190.     ParseInit();
  191.     MatrixClassInit();
  192.     PolyLibInit();
  193.     EventLibInit();
  194.     ViewLibInit();
  195.     EdgeLibInit();
  196.     VertexSetLibInit();
  197.     MenuLibInit();
  198.     DlogLibInit();
  199.     SysLibInit();
  200.     ButtonLibInit();
  201.  
  202.     /*    設定ファイルの読み込み(もういちど)    */
  203.     fp = fopen( path, "r" );
  204.     assert( fp != NULL );
  205.     ModelrcName = (char**)MemoryAlloc( sizeof( char* ) * ( line + 1 ) );
  206.     ModelrcValue = (char**)MemoryAlloc( sizeof( char* ) * line );
  207.     ModelrcSize = line;
  208.     line = 0 ;
  209.     while( fgets( buf, 255, fp ) != NULL )
  210.     {
  211.         p = GetWord( buf, str );
  212.         if (p != NULL && *p) {
  213.             ModelrcName[line] = MemoryAlloc( strlen( str ) + 1 );
  214.             ModelrcValue[line] = MemoryAlloc( strlen( p ) + 1 );
  215.             strcpy( ModelrcName[line], str);
  216.             strcpy( ModelrcValue[line], p );
  217.             line ++ ;
  218.         }
  219.     }
  220.     ModelrcName[line] = NULL ;
  221.     fclose(fp);
  222.  
  223.     fp = fopen( path, "r" );
  224.     assert( fp != NULL );
  225.  
  226.     printf( "\nCode Used\n" );
  227.     flag = FALSE ;
  228.     while( fgets( buf, 255, fp ) != NULL )
  229.     {
  230.         p = GetWord( buf, str );
  231.         if ( strcmp( str, "SourceFile" ) == 0 )
  232.         {
  233.             p = GetWord( p, str );
  234.             if ( SearchPath( path, pathenv, str ) == NULL )
  235.             {
  236.                 char    sbuf[256] ;
  237.  
  238.                 sprintf( sbuf, "ファイル %s が存在しません。", str );
  239.                 MessageError( sbuf );
  240.             }
  241.             codesize = ReportCodeSize();
  242.  
  243.             ParseSentenseAll( path );
  244.             printf( "%-10s : %d\n", str, ReportCodeSize() - codesize );
  245.             flag = TRUE ;
  246.         }
  247.     }
  248.     fclose( fp );
  249.     printf( "%-10s : %d\n", "Total", ReportCodeSize() );
  250.     if ( flag == FALSE )
  251.         MessageError( "ソースファイルが読み込まれていません。" );
  252.  
  253.     ExecInit();
  254.  
  255.     graph_init();
  256.     InitInput();
  257.     SetPalet();
  258.  
  259.     EventInit();
  260.     graph_cls( 0 );
  261.     PolyInit( bufsize*1024, maxobj, maxatr, maxver );
  262.     ButtonInit();
  263.     ViewInit( DISPLAY_X - CTRL_WIDTH - ButtonAreaWidth, DISPLAY_Y );
  264.     MenuInit();
  265.  
  266.     SetBreakCheck(ModelBreakCheck);
  267. }
  268.  
  269. /*    終了処理    */
  270. void    ModelExit()
  271. {
  272.     int        global, func ;
  273.  
  274.     graph_cls( 0 );
  275.  
  276.     graph_exit();
  277.     ExitInput();
  278.  
  279.     PolyExit();
  280.  
  281.     printf( "Code Used  : %d Kbytes\n", ReportCodeSize() / 1024 + 1 );
  282.     printf( "Stack Used : %d\n", StackMaxUsed() );
  283.     ReportIdent( &global, &func );
  284.     printf( "Globals    : %d\n", global );
  285.     printf( "Functions  : %d\n", func );
  286.  
  287.     printf( "\n" );
  288.     printf( "DoGA CGA System Polygon Editor.  Version %s\n", Version );
  289.     printf( "Copyright (C) T.Kobayashi 1993,1994.  All rights reserved.\n" );
  290.  
  291.     ParseExit();
  292.     ExecExit();
  293. }
  294.  
  295.  
  296. #ifdef WINDOWS
  297. static    char    *GetWord( buf, word )
  298. char    *buf, *word ;
  299. {
  300.     int i;
  301.     char    *p ;
  302.  
  303.     while( isspace( *buf ) && *buf != '\0' )
  304.         buf ++ ;
  305.  
  306.     if ( *buf == '\0' || *buf == '#' )
  307.     {
  308.         word[0] = '\0' ;
  309.         return buf ;
  310.     }
  311.  
  312.     p = buf ;
  313.     while( ! isspace( *p ) && *p != '=' && *p != '\0' )
  314.     {
  315.         *word++ = *p++ ;
  316.     }
  317.     *word = '\0' ;
  318.  
  319.     while( isspace( *p ) && *p != '=' && *p != '\0' )
  320.         p ++ ;
  321.     if (*p == '=') {
  322.         p++;
  323.     }
  324.     while( isspace( *p ) && *p != '\0' )
  325.         p ++ ;
  326.  
  327.     i = strlen(p);
  328.     for (i--; i >= 0 && isspace(p[i]); --i) {
  329.         p[i] = '\0';
  330.     }
  331.  
  332.     return p ;
  333. }
  334. #else
  335. static    char    *GetWord( buf, word )
  336. char    *buf, *word ;
  337. {
  338.     int i;
  339.     char    *p ;
  340.  
  341.     while( isspace( *buf ) && *buf != '\0' )
  342.         buf ++ ;
  343.  
  344.     if ( *buf == '\0' || *buf == '#' )
  345.     {
  346.         word[0] = '\0' ;
  347.         return buf ;
  348.     }
  349.  
  350.     p = buf ;
  351.     while( ! isspace( *p ) && *p != '\0' )
  352.     {
  353.         *word++ = *p++ ;
  354.     }
  355.     *word = '\0' ;
  356.     while( isspace( *p ) && *p != '\0' )
  357.         p ++ ;
  358.     i = strlen(p);
  359.     for (i--; i >= 0 && isspace(p[i]); --i) {
  360.         p[i] = '\0';
  361.     }
  362.  
  363.     return p ;
  364. }
  365. #endif
  366.  
  367. /*    パスにしたがってファイルを検索    */
  368. static    char    *SearchPath( path, env, file )
  369. char    *path ;
  370. char    *env ;
  371. char    *file ;
  372. {
  373.     char    *p, *pp ;
  374.  
  375.     if ( access( file, 0 ) != 0 )
  376.     {
  377.         p = env;
  378.         while( p != NULL )
  379.         {
  380.             if ( *p == ';' )
  381.                 p ++ ;
  382.             pp = strchr( p, ';' );
  383.             if ( pp != NULL )
  384.                 *pp = '\0' ;
  385.             strcpy( path, p );
  386.             if ( strchr( "\\/:", path[ strlen( path )-1 ] ) == NULL )
  387.                 strcat( path, "\\" );
  388.             strcat( path, file );
  389.             if ( pp != NULL )
  390.                 *pp = ';' ;
  391.             if ( access( path, 0 ) == 0 )
  392.                 break ;
  393.             p = pp ;
  394.         }
  395.         if ( p == NULL )
  396.             return NULL ;
  397.         else
  398.             return path ;
  399.     }
  400.     else
  401.     {
  402.         int i;
  403.         getcwd(path, 256);
  404.         i =  strlen(path);
  405.         if (i > 0 && path[i-1] != '\\') {
  406.             path[i] = '\\';
  407.             path[i+1] = '\0';
  408.             i++;
  409.         }
  410.         strcpy(path+i, file);
  411.         return path;
  412.     }
  413. }
  414.  
  415. static    void    SetPalet()
  416. {
  417.     graph_palet( 8, 255, 0, 255 );
  418. /*    graph_palet( GRID_COLOR, 80, 80, 127 );*/
  419.     graph_palet( GRID_COLOR, 0, 0, 127 );
  420.     graph_palet( INVISIBLE_COLOR, 127, 0, 127);
  421.     graph_palet( FRAME_COLOR, 180, 180, 180 );
  422.     graph_palet( BG_COLOR, 100, 100, 100 );
  423.     graph_palet( SELECT_COLOR, 255, 255, 0 );
  424.     graph_palet( 15, 255, 255, 255 );
  425. }
  426.  
  427. #ifndef WINDOWS
  428. /*
  429.  *    子プロセスを呼び出す。
  430.  *
  431.  */
  432. void    ChildExec(char *comline, int waitflag, int iconify )
  433. {
  434. #ifndef WINDOWS
  435.     ViewCursor( OFF );
  436.  
  437.     graph_cls( 0 );
  438.     graph_exit();
  439.     ExitInput();
  440.  
  441.     printf( "ChildExec %s\n", comline );
  442.  
  443. #ifdef    X68K
  444.     {
  445.         struct    COMLINE    buf ;
  446.         strcpy( (char*)buf.buffer, comline );
  447.         buf.len = strlen( comline ) - 1 ;
  448.         LOADEXEC( (UBYTE*)CommandFile, &buf, NULL );
  449.     }
  450. #endif
  451.  
  452. #ifdef    GO32
  453.     system( comline );
  454.     LibraryInit();
  455. #endif
  456.  
  457.     graph_init();
  458.     InitInput();
  459.     graph_cls( 0 );
  460.     SetPalet();
  461.  
  462.     CursorOFF();
  463.     ViewFrame();
  464.     ViewLineAll( 0, 0, DISPLAY_X, DISPLAY_Y, TRUE );
  465.     DrawTitleBar();
  466.     DrawUserButtonAll();
  467.     DrawControlPanel();
  468.  
  469.     ViewCursor( ON );
  470. #else
  471. /*    WinExec(comline, SW_SHOWDEFAULT);*/
  472. #endif
  473. }
  474. #endif
  475.  
  476. /*
  477.     MODEL.RC or POLYEDIT.INIへの書き出し
  478. */
  479. void WriteModelrc(char *name, char *value)
  480. {
  481.     int i;
  482.     FILE *infp, *outfp;
  483.     char buf[256], str[256];
  484.     char buf2[256];
  485.     int writeflag = FALSE;
  486.  
  487. /*
  488.     unlink(modelrc_old);
  489.     rename(modelrc, modelrc_old);
  490. printf("delete %s\n", modelrc_old);
  491. printf("rename %s %s\n", modelrc, modelrc_old);
  492. */
  493.  
  494.     if ((infp = fopen(modelrc, "r")) == NULL) {
  495. /*        printf("設定ファイル '%s' を読み込めません。\n", modelrc);*/
  496.         return;
  497.     }
  498.     if ((outfp = fopen(modelrc_new, "w")) == NULL) {
  499. /*        printf("設定ファイル '%s' を書き込めません。\n", modelrc_new);*/
  500.         fclose(infp);
  501.         return;
  502.     }
  503.  
  504.     writeflag= FALSE;
  505.     while (fgets(buf, 1024, infp)) {
  506.         char *p;
  507.         strcpy(buf2, buf);
  508.         p = GetWord( buf, str );
  509.         if (writeflag == FALSE && p != NULL && *p && strcmp(str, name) == 0) {
  510. #ifdef WINDOWS
  511.             sprintf(buf2, "%s\t= %s\n", name, value);
  512. #else
  513.             sprintf(buf2, "%s\t%s\n", name, value);
  514. #endif
  515.             writeflag = TRUE;
  516.         }
  517.         fputs(buf2, outfp);
  518.     }
  519.     if (writeflag == FALSE) {
  520. #ifdef WINDOWS
  521.         fprintf(outfp, "%s\t= %s\n", name, value);
  522. #else
  523.         fprintf(outfp, "%s\t%s\n", name, value);
  524. #endif
  525.     }
  526.     fclose(infp);
  527.     fclose(outfp);
  528.  
  529.     if ((infp  = fopen(modelrc_new, "r")) != NULL
  530.      && (outfp = fopen(modelrc,     "w")) != NULL) {
  531.         while (fgets(buf, 1024, infp)) {
  532.             fputs(buf, outfp);
  533.         }
  534.     }
  535.     if (infp) fclose(infp);
  536.     if (outfp) fclose(outfp);
  537.     unlink(modelrc_new);
  538.  
  539.     for (i = 0; ModelrcName[i] != NULL; i++) {
  540.         if (strcmp(ModelrcName[i], name) == 0) {
  541.             MemoryFree(ModelrcValue[i]);
  542.             ModelrcValue[i] = MemoryAlloc( strlen( value ) + 1 );
  543.             strcpy( ModelrcValue[i], value );
  544.             break;
  545.         }
  546.     }
  547.     if (ModelrcName[i] == NULL) {
  548. #define ALLOCUNIT 64
  549.         if (i == ModelrcSize) {
  550.             char **p;
  551.             p = MemoryAlloc(sizeof(char*) * (ModelrcSize + ALLOCUNIT + 1));
  552.             memcpy(p, ModelrcName, sizeof(char*) * (ModelrcSize + 1));
  553.             MemoryFree(ModelrcName);
  554.             ModelrcName = p;
  555.  
  556.             p = MemoryAlloc(sizeof(char*) * (ModelrcSize + ALLOCUNIT));
  557.             memcpy(p, ModelrcValue, sizeof(char*) * ModelrcSize);
  558.             MemoryFree(ModelrcValue);
  559.             ModelrcValue = p;
  560.  
  561.             ModelrcSize += ALLOCUNIT;
  562.         }
  563.         ModelrcName[i] = MemoryAlloc( strlen( name) + 1 );
  564.         ModelrcValue[i] = MemoryAlloc( strlen( value ) + 1 );
  565.         strcpy( ModelrcName[i], name);
  566.         strcpy( ModelrcValue[i], value );
  567.         ModelrcName[i+1] = NULL;
  568.     }
  569. }
  570.  
  571. int ModelBreakCheck(void)
  572. {
  573.     PeekInput();
  574.     if (((ShiftStat & 3) == 3) && (KeyCode == KEY_DEL)) {
  575.         WaitInput();
  576.         return TRUE;
  577.     }
  578.     return FALSE;
  579. }
  580.