dosmain.cpp

来自「算断裂的」· C++ 代码 · 共 276 行

CPP
276
字号
// ------------------------------------------------------------------// dosmain.cpp//// This file contains the entry point for the Windows version of// QMG running under Tcl.  It is a modified version of tclAppInit.c// that is shipped with Tcl/Tk.// ------------------------------------------------------------------// Author: Stephen A. Vavasis// Copyright (c) 1999 by Cornell University.  All rights reserved.// // See the accompanying file 'Copyright' for authorship information,// the terms of the license governing this software, and disclaimers// concerning this software.// ------------------------------------------------------------------// This file is part of the QMG software.  // Version 2.0 of QMG, release date September 3, 1999.// ------------------------------------------------------------------/*  * tclAppInit.c -- * *	Provides a default version of the main program and Tcl_AppInit *	procedure for Tcl applications (without Tk).  Note that this *	program must be built in Win32 console mode to work properly. * * Copyright (c) 1996 by Sun Microsystems, Inc. * * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * * SCCS: @(#) tclAppInit.c 1.12 97/04/30 11:04:50 *//* Modified for QMG by S. Vavasis.  The modifications are:(1) rewrite function arg lists in C++ format instead of K&R C.(2) Call qmg_init. */extern "C" {#include "tcl.h"#define WIN32_LEAN_AND_MEAN#include <windows.h>#include <locale.h>}#ifdef TCL_TESTEXTERN int		Tcltest_Init _ANSI_ARGS_((Tcl_Interp *interp));EXTERN int		TclObjTest_Init _ANSI_ARGS_((Tcl_Interp *interp));#endif /* TCL_TEST */static void		setargv _ANSI_ARGS_((int *argcPtr, char ***argvPtr));/* *---------------------------------------------------------------------- * * main -- * *	This is the main program for the application. * * Results: *	None: Tcl_Main never returns here, so this procedure never *	returns either. * * Side effects: *	Whatever the application does. * *---------------------------------------------------------------------- */intmain(int argc, char** argv){    char *p;    char buffer[MAX_PATH];    /*     * Set up the default locale to be standard "C" locale so parsing     * is performed correctly.     */    setlocale(LC_ALL, "C");    setargv(&argc, &argv);    /*     * Replace argv[0] with full pathname of executable, and forward     * slashes substituted for backslashes.     */    GetModuleFileName(NULL, buffer, sizeof(buffer));    argv[0] = buffer;    for (p = buffer; *p != '\0'; p++) {	if (*p == '\\') {	    *p = '/';	}    }    Tcl_Main(argc, argv, Tcl_AppInit);    return 0;			/* Needed only to prevent compiler warning. */}/* *---------------------------------------------------------------------- * * Tcl_AppInit -- * *	This procedure performs application-specific initialization. *	Most applications, especially those that incorporate additional *	packages, will have their own version of this procedure. * * Results: *	Returns a standard Tcl completion code, and leaves an error *	message in interp->result if an error occurs. * * Side effects: *	Depends on the startup script. * *---------------------------------------------------------------------- */extern "C" {  extern int qmg_init(Tcl_Interp* interp);}intTcl_AppInit(Tcl_Interp* interp){  if (Tcl_Init(interp) == TCL_ERROR) {    return TCL_ERROR;  }  if (qmg_init(interp) != TCL_OK) {    return TCL_ERROR;  }    /*  * Call the init procedures for included packages.  Each call should  * look like this:  *  * if (Mod_Init(interp) == TCL_ERROR) {  *     return TCL_ERROR;  * }  *  * where "Mod" is the name of the module.  */    return TCL_OK;}/* *------------------------------------------------------------------------- * * setargv -- * *	Parse the Windows command line string into argc/argv.  Done here *	because we don't trust the builtin argument parser in crt0.   *	Windows applications are responsible for breaking their command *	line into arguments. * *	2N backslashes + quote -> N backslashes + begin quoted string *	2N + 1 backslashes + quote -> literal *	N backslashes + non-quote -> literal *	quote + quote in a quoted string -> single quote *	quote + quote not in quoted string -> empty string *	quote -> begin quoted string * * Results: *	Fills argcPtr with the number of arguments and argvPtr with the *	array of arguments. * * Side effects: *	Memory allocated. * *-------------------------------------------------------------------------- */static voidsetargv(int* argcPtr, char*** argvPtr){    char *cmdLine, *p, *arg, *argSpace;    char **argv;    int argc, size, inquote, copy, slashes;        cmdLine = GetCommandLine();    /*     * Precompute an overly pessimistic guess at the number of arguments     * in the command line by counting non-space spans.     */    size = 2;    for (p = cmdLine; *p != '\0'; p++) {	if (isspace(*p)) {	    size++;	    while (isspace(*p)) {		p++;	    }	    if (*p == '\0') {		break;	    }	}    }    argSpace = (char *) ckalloc((unsigned) (size * sizeof(char *) 	    + strlen(cmdLine) + 1));    argv = (char **) argSpace;    argSpace += size * sizeof(char *);    size--;    p = cmdLine;    for (argc = 0; argc < size; argc++) {	argv[argc] = arg = argSpace;	while (isspace(*p)) {	    p++;	}	if (*p == '\0') {	    break;	}	inquote = 0;	slashes = 0;	while (1) {	    copy = 1;	    while (*p == '\\') {		slashes++;		p++;	    }	    if (*p == '"') {		if ((slashes & 1) == 0) {		    copy = 0;		    if ((inquote) && (p[1] == '"')) {			p++;			copy = 1;		    } else {			inquote = !inquote;		    }                }                slashes >>= 1;            }            while (slashes) {		*arg = '\\';		arg++;		slashes--;	    }	    if ((*p == '\0') || (!inquote && isspace(*p))) {		break;	    }	    if (copy != 0) {		*arg = *p;		arg++;	    }	    p++;        }	*arg = '\0';	argSpace = arg + 1;    }    argv[argc] = 0   ;    *argcPtr = argc;    *argvPtr = argv;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?