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 >
Wrap
C/C++ Source or Header
|
1996-07-09
|
12KB
|
580 lines
/*
* 初期化ルーチン
*
* Copyright T.Kobayashi 1994.7.9
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <io.h>
#include <assert.h>
#ifdef X68K
# include <doslib.h>
#endif
#include "lib.h"
#include "inlib.h"
#include "matrix.h"
#include "reader.h"
#include "matclass.h"
#include "ml.h"
#include "model.h"
#include "graph.h"
#include "input.h"
static char *GetWord( char*, char* );
static char *SearchPath( char*, char*, char* );
static void SetPalet( void );
static int ModelBreakCheck( void );
char **ModelrcName ;
char **ModelrcValue ;
int ModelrcSize;
/*char *Version = "1.1.0" ;*/
extern char *Version;
#ifdef X68K
char *Environment = "X68k" ;
#endif
#ifdef PC98
char *Environment = "PC98" ;
#endif
#ifdef PC
char *Environment = "IBMPC" ;
#endif
#ifdef WINDOWS
char *Environment = "WINDOWS" ;
#endif
#ifdef UNIX
char *Environment = "UNIX" ;
#endif
#ifdef X68K
char CommandFile[64] ;
#endif
extern char **Argv;
extern int Argc;
char ExecPath[512] ;
static char modelrc[512];
static char modelrc_old[512];
static char modelrc_new[512];
/* 初期化 */
void ModelInit()
{
int i, flag, line ;
FILE *fp ;
char str[256] ;
char path[256], buf[256], *p ;
int codesize ;
#if defined(WINDOWS)
char *file = "PolyEdit.ini" ;
#elif defined(UNIX)
char *file = ".polyeditrc";
#else
char *file = "Model.rc" ;
#endif
char pathenv[1024] ;
int bufsize = 128 ;
int maxobj = 16 ;
int maxatr = 256 ;
int maxver = 256 ;
if (Argc > 1 && strcmp(Argv[1], "--dump") == 0) {
for (i = 2; i < Argc; ++i) {
Argv[i-1] = Argv[i];
}
Argc--;
DumpMode( TRUE );
}
strcpy(ExecPath, Argv[0]);
if ((p = strrchr(ExecPath, '\\')) != NULL) {
p[1] = '\0';
} else if (ExecPath[0] != '\0' && ExecPath[1] == ':') {
ExecPath[2] = '\0';
} else {
ExecPath[0] = '\0';
}
#ifdef X68K
strcpy( CommandFile, "A:\\COMMAND.X" );
#endif
LibraryInit();
MatrixInit();
ReaderInit();
MLInit();
if ( getenv( "MODELPATH" ) != NULL ) {
strcpy(pathenv, getenv("MODELPATH")) ;
} else {
#ifndef UNIX
if (getenv( "HOME" ) != NULL) {
sprintf(pathenv, "%s;%s;%sMACRO;%sPOLYEDIT;%sMODEL",
getenv("HOME"),ExecPath, ExecPath, ExecPath, ExecPath);
} else if (getenv( "home" ) != NULL) {
sprintf(pathenv, "%s;%s;%sMACRO;%sPOLYEDIT;%sMODEL",
getenv("home"),ExecPath, ExecPath, ExecPath, ExecPath);
} else {
sprintf(pathenv, "%s;%sMACRO;%sPOLYEDIT;%sMODEL",
ExecPath, ExecPath, ExecPath, ExecPath);
}
#else
if (getenv( "HOME") != NULL) {
strcpy(pathenv, getenv("HOME"));
} else if (getenv( "home") != NULL) {
strcpy(pathenv, getenv("home"));
} else {
pathenv[0] = '\0';
}
#endif
}
/* 設定ファイルの読み込み */
if ( SearchPath( path, pathenv, file ) == NULL )
{
sprintf( buf, "ファイル %s が存在しません。", file );
MessageError( buf );
}
strcpy(modelrc, path);
strcpy(modelrc_new, path);
strcpy(modelrc_old, path);
p = strrchr(modelrc, '.');
if (p == NULL) {
i = strlen(modelrc);
} else {
i = p - modelrc;
}
strcpy(modelrc_old + i, ".bak");
strcpy(modelrc_new + i, ".tmp");
fp = fopen( path, "r" );
assert( fp != NULL );
line = 0 ;
while( fgets( buf, 255, fp ) != NULL )
{
p = GetWord( buf, str );
if ( strcmp( str, "CodeSize" ) == 0 )
SetMaxCodeSize( atoi( p ) * 1024 );
else if ( strcmp( str, "Global" ) == 0 )
SetMaxGlobals( atoi( p ) );
else if ( strcmp( str, "Function" ) == 0 )
SetMaxFunctions( atoi( p ) );
else if ( strcmp( str, "Stack" ) == 0 )
SetMaxStacks( atoi( p ) );
else if ( strcmp( str, "BufferSize" ) == 0 )
bufsize = atoi( p );
else if ( strcmp( str, "Attr" ) == 0 )
maxatr = atoi( p );
else if ( strcmp( str, "Object" ) == 0 )
maxobj = atoi( p );
else if ( strcmp( str, "Vertexs" ) == 0 )
maxver = atoi( p );
else if ( strcmp( str, "Path" ) == 0)
strcpy(pathenv, p);
#if X68K
else if ( strcmp( str, "CommandFile" ) == 0 )
GetWord( p, CommandFile );
#endif
line ++ ;
}
fclose( fp );
ParseInit();
MatrixClassInit();
PolyLibInit();
EventLibInit();
ViewLibInit();
EdgeLibInit();
VertexSetLibInit();
MenuLibInit();
DlogLibInit();
SysLibInit();
ButtonLibInit();
/* 設定ファイルの読み込み(もういちど) */
fp = fopen( path, "r" );
assert( fp != NULL );
ModelrcName = (char**)MemoryAlloc( sizeof( char* ) * ( line + 1 ) );
ModelrcValue = (char**)MemoryAlloc( sizeof( char* ) * line );
ModelrcSize = line;
line = 0 ;
while( fgets( buf, 255, fp ) != NULL )
{
p = GetWord( buf, str );
if (p != NULL && *p) {
ModelrcName[line] = MemoryAlloc( strlen( str ) + 1 );
ModelrcValue[line] = MemoryAlloc( strlen( p ) + 1 );
strcpy( ModelrcName[line], str);
strcpy( ModelrcValue[line], p );
line ++ ;
}
}
ModelrcName[line] = NULL ;
fclose(fp);
fp = fopen( path, "r" );
assert( fp != NULL );
printf( "\nCode Used\n" );
flag = FALSE ;
while( fgets( buf, 255, fp ) != NULL )
{
p = GetWord( buf, str );
if ( strcmp( str, "SourceFile" ) == 0 )
{
p = GetWord( p, str );
if ( SearchPath( path, pathenv, str ) == NULL )
{
char sbuf[256] ;
sprintf( sbuf, "ファイル %s が存在しません。", str );
MessageError( sbuf );
}
codesize = ReportCodeSize();
ParseSentenseAll( path );
printf( "%-10s : %d\n", str, ReportCodeSize() - codesize );
flag = TRUE ;
}
}
fclose( fp );
printf( "%-10s : %d\n", "Total", ReportCodeSize() );
if ( flag == FALSE )
MessageError( "ソースファイルが読み込まれていません。" );
ExecInit();
graph_init();
InitInput();
SetPalet();
EventInit();
graph_cls( 0 );
PolyInit( bufsize*1024, maxobj, maxatr, maxver );
ButtonInit();
ViewInit( DISPLAY_X - CTRL_WIDTH - ButtonAreaWidth, DISPLAY_Y );
MenuInit();
SetBreakCheck(ModelBreakCheck);
}
/* 終了処理 */
void ModelExit()
{
int global, func ;
graph_cls( 0 );
graph_exit();
ExitInput();
PolyExit();
printf( "Code Used : %d Kbytes\n", ReportCodeSize() / 1024 + 1 );
printf( "Stack Used : %d\n", StackMaxUsed() );
ReportIdent( &global, &func );
printf( "Globals : %d\n", global );
printf( "Functions : %d\n", func );
printf( "\n" );
printf( "DoGA CGA System Polygon Editor. Version %s\n", Version );
printf( "Copyright (C) T.Kobayashi 1993,1994. All rights reserved.\n" );
ParseExit();
ExecExit();
}
#ifdef WINDOWS
static char *GetWord( buf, word )
char *buf, *word ;
{
int i;
char *p ;
while( isspace( *buf ) && *buf != '\0' )
buf ++ ;
if ( *buf == '\0' || *buf == '#' )
{
word[0] = '\0' ;
return buf ;
}
p = buf ;
while( ! isspace( *p ) && *p != '=' && *p != '\0' )
{
*word++ = *p++ ;
}
*word = '\0' ;
while( isspace( *p ) && *p != '=' && *p != '\0' )
p ++ ;
if (*p == '=') {
p++;
}
while( isspace( *p ) && *p != '\0' )
p ++ ;
i = strlen(p);
for (i--; i >= 0 && isspace(p[i]); --i) {
p[i] = '\0';
}
return p ;
}
#else
static char *GetWord( buf, word )
char *buf, *word ;
{
int i;
char *p ;
while( isspace( *buf ) && *buf != '\0' )
buf ++ ;
if ( *buf == '\0' || *buf == '#' )
{
word[0] = '\0' ;
return buf ;
}
p = buf ;
while( ! isspace( *p ) && *p != '\0' )
{
*word++ = *p++ ;
}
*word = '\0' ;
while( isspace( *p ) && *p != '\0' )
p ++ ;
i = strlen(p);
for (i--; i >= 0 && isspace(p[i]); --i) {
p[i] = '\0';
}
return p ;
}
#endif
/* パスにしたがってファイルを検索 */
static char *SearchPath( path, env, file )
char *path ;
char *env ;
char *file ;
{
char *p, *pp ;
if ( access( file, 0 ) != 0 )
{
p = env;
while( p != NULL )
{
if ( *p == ';' )
p ++ ;
pp = strchr( p, ';' );
if ( pp != NULL )
*pp = '\0' ;
strcpy( path, p );
if ( strchr( "\\/:", path[ strlen( path )-1 ] ) == NULL )
strcat( path, "\\" );
strcat( path, file );
if ( pp != NULL )
*pp = ';' ;
if ( access( path, 0 ) == 0 )
break ;
p = pp ;
}
if ( p == NULL )
return NULL ;
else
return path ;
}
else
{
int i;
getcwd(path, 256);
i = strlen(path);
if (i > 0 && path[i-1] != '\\') {
path[i] = '\\';
path[i+1] = '\0';
i++;
}
strcpy(path+i, file);
return path;
}
}
static void SetPalet()
{
graph_palet( 8, 255, 0, 255 );
/* graph_palet( GRID_COLOR, 80, 80, 127 );*/
graph_palet( GRID_COLOR, 0, 0, 127 );
graph_palet( INVISIBLE_COLOR, 127, 0, 127);
graph_palet( FRAME_COLOR, 180, 180, 180 );
graph_palet( BG_COLOR, 100, 100, 100 );
graph_palet( SELECT_COLOR, 255, 255, 0 );
graph_palet( 15, 255, 255, 255 );
}
#ifndef WINDOWS
/*
* 子プロセスを呼び出す。
*
*/
void ChildExec(char *comline, int waitflag, int iconify )
{
#ifndef WINDOWS
ViewCursor( OFF );
graph_cls( 0 );
graph_exit();
ExitInput();
printf( "ChildExec %s\n", comline );
#ifdef X68K
{
struct COMLINE buf ;
strcpy( (char*)buf.buffer, comline );
buf.len = strlen( comline ) - 1 ;
LOADEXEC( (UBYTE*)CommandFile, &buf, NULL );
}
#endif
#ifdef GO32
system( comline );
LibraryInit();
#endif
graph_init();
InitInput();
graph_cls( 0 );
SetPalet();
CursorOFF();
ViewFrame();
ViewLineAll( 0, 0, DISPLAY_X, DISPLAY_Y, TRUE );
DrawTitleBar();
DrawUserButtonAll();
DrawControlPanel();
ViewCursor( ON );
#else
/* WinExec(comline, SW_SHOWDEFAULT);*/
#endif
}
#endif
/*
MODEL.RC or POLYEDIT.INIへの書き出し
*/
void WriteModelrc(char *name, char *value)
{
int i;
FILE *infp, *outfp;
char buf[256], str[256];
char buf2[256];
int writeflag = FALSE;
/*
unlink(modelrc_old);
rename(modelrc, modelrc_old);
printf("delete %s\n", modelrc_old);
printf("rename %s %s\n", modelrc, modelrc_old);
*/
if ((infp = fopen(modelrc, "r")) == NULL) {
/* printf("設定ファイル '%s' を読み込めません。\n", modelrc);*/
return;
}
if ((outfp = fopen(modelrc_new, "w")) == NULL) {
/* printf("設定ファイル '%s' を書き込めません。\n", modelrc_new);*/
fclose(infp);
return;
}
writeflag= FALSE;
while (fgets(buf, 1024, infp)) {
char *p;
strcpy(buf2, buf);
p = GetWord( buf, str );
if (writeflag == FALSE && p != NULL && *p && strcmp(str, name) == 0) {
#ifdef WINDOWS
sprintf(buf2, "%s\t= %s\n", name, value);
#else
sprintf(buf2, "%s\t%s\n", name, value);
#endif
writeflag = TRUE;
}
fputs(buf2, outfp);
}
if (writeflag == FALSE) {
#ifdef WINDOWS
fprintf(outfp, "%s\t= %s\n", name, value);
#else
fprintf(outfp, "%s\t%s\n", name, value);
#endif
}
fclose(infp);
fclose(outfp);
if ((infp = fopen(modelrc_new, "r")) != NULL
&& (outfp = fopen(modelrc, "w")) != NULL) {
while (fgets(buf, 1024, infp)) {
fputs(buf, outfp);
}
}
if (infp) fclose(infp);
if (outfp) fclose(outfp);
unlink(modelrc_new);
for (i = 0; ModelrcName[i] != NULL; i++) {
if (strcmp(ModelrcName[i], name) == 0) {
MemoryFree(ModelrcValue[i]);
ModelrcValue[i] = MemoryAlloc( strlen( value ) + 1 );
strcpy( ModelrcValue[i], value );
break;
}
}
if (ModelrcName[i] == NULL) {
#define ALLOCUNIT 64
if (i == ModelrcSize) {
char **p;
p = MemoryAlloc(sizeof(char*) * (ModelrcSize + ALLOCUNIT + 1));
memcpy(p, ModelrcName, sizeof(char*) * (ModelrcSize + 1));
MemoryFree(ModelrcName);
ModelrcName = p;
p = MemoryAlloc(sizeof(char*) * (ModelrcSize + ALLOCUNIT));
memcpy(p, ModelrcValue, sizeof(char*) * ModelrcSize);
MemoryFree(ModelrcValue);
ModelrcValue = p;
ModelrcSize += ALLOCUNIT;
}
ModelrcName[i] = MemoryAlloc( strlen( name) + 1 );
ModelrcValue[i] = MemoryAlloc( strlen( value ) + 1 );
strcpy( ModelrcName[i], name);
strcpy( ModelrcValue[i], value );
ModelrcName[i+1] = NULL;
}
}
int ModelBreakCheck(void)
{
PeekInput();
if (((ShiftStat & 3) == 3) && (KeyCode == KEY_DEL)) {
WaitInput();
return TRUE;
}
return FALSE;
}