⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 insert.c

📁 Dos6.0
💻 C
📖 第 1 页 / 共 2 页
字号:
/*********************************************************************
 * Microsoft Diagnostics Version 2.0
 *
 * A diagnostic utility to detect as much useful information about a
 *   customer's computer system as is possible.
 *
 * Microsoft Diagnostics:  We detect the World.
 *
 * INSERT.C - Source file for the Insert Command function.
 ********************************************************************/


/* Include Files */

#include "msd.h"

extern char EditBuf[256];

/*********************************************************************
 * InsertCommand - Performs the Insert Command function.
 ********************************************************************/

VOID InsertCommand (VOID)
{
  BOOL fSearchFlags;        /* Search flags for FindFile */
  FILE_INFO FAR *pfiNext;   /* File info structure       */
  CHAR chBuffer[80];        /* String buffer             */


  /* Bring up the dialog box */
  if (DialogBox (&dlgInsertCmd1, InsertCmdDlg1) != IDOK)
    return;


  /* Boot files are to be found on the boot drive */
  if (stricmp (pszInsertFilename, "CONFIG.SYS") == 0 ||
      stricmp (pszInsertFilename, "AUTOEXEC.BAT") == 0)
    {
      if (wDosMajor >= 4 && wDosMajor < 10)
        fSearchFlags = SEARCH_BOOT_DRIVE;
      else
        fSearchFlags = SEARCH_BOOT_DRIVE   |
                       SEARCH_FLOPPIES     |
                       SEARCH_LOCAL_DRIVES |
                       SEARCH_ROOT;
    }
  else
    fSearchFlags = SEARCH_LOCAL_DRIVES   |
                   SEARCH_ROOT           |
                   RECURSE_INTO_SUB_DIRS;


  if (strchr (pszInsertFilename, '\\') || strchr (pszInsertFilename, ':'))
    {
      /* A pathname was given */
      strcpy (EditBuf, pszInsertFilename);
    }
  else
    {
      /* Search for the file */
      sprintf (chBuffer, "Searching for %s ...", pszInsertFilename);
      ShowStatus (chBuffer);
      pfiDlg = FindFile (pszInsertFilename, NULL, fSearchFlags, '\0');
      PostMessage (pwndStatusLine, WM_PAINT, NULL, NULL);

      /* Were any files found */
      if (pfiDlg == NULL || pfiDlg->fpNextFileInfo == NULL)
        {
          MessageBox (pszInsertFilename, "Could not be found", NULL, MB_OK | 0x8000);
          return;
        }

      pfiNext = (FILE_INFO FAR *) pfiDlg->fpNextFileInfo;

      /* Give the user a choice if more than one file was found */
      if (pfiNext != NULL && pfiNext->fpNextFileInfo != NULL)
        {
          if (DialogBox (&dlgInsertCmd3, InsertCmdDlg3) != IDOK)
            return;
        }
      else
        {
          _fstrcpy (EditBuf, pfiDlg->fpszPathToFile);
          pszInsertFilename = EditBuf;
        }
    }

  /* Search for and add or replace strings as necessary. */
  /*   Actual code to accomplish this will be in the     */
  /*   dialog box code.                                  */

  DialogBox (&dlgInsertCmd4, InsertCmdDlg4);
  FreeFileInfo (pfiDlg);
}


/*********************************************************************
 * ChangeFile - Performs the requested change to the file.
 *
 * pszFilename     - Filename to change.
 * pszSection      - Section to change.
 * pszCommand      - Command to add or replace.
 * pszSearchString - String to search for.
 * wReplaceLine    - Line to replace.  HD_REPLACE_ALL to replace all
 *                   occurances of the search string.
 *
 * Returns:  TRUE if an error occured.
 *********************************************************************/

BOOL ChangeFile (PSZ  pszFilename,
                 PSZ  pszSection,
                 PSZ  pszCommand,
                 PSZ  pszSearchString,
                 WORD wReplaceLine)
{
  FILE *fpInsertFile;               /* File handle for the input file  */
  FILE *fpOutputFile;               /* File handle for the output file */
  CHAR szTempPathname[_MAX_PATH];   /* Path to .TMP file               */
  CHAR szBakPathname[_MAX_PATH];    /* Path to .BAK file               */
  INT  i;                           /* Looping variable                */
  BOOL fReturnValue;                /* Return value from various calls */


  /* Create temporary file */

  strcpy (szTempPathname, pszFilename);

  /* Clear out the original file's name */

  for (i = strlen (szTempPathname) - 1; i >= 0 &&
                                        szTempPathname[i] != '\\'; --i)
    ;

  szTempPathname[i + 1] = '\0';

  if (CreateTempFile (szTempPathname) != 0)
    {
      MessageBox, ("Error Creating File", szTempPathname, NULL,
                   MB_OK | 0x8000);
      return (TRUE);
    }


  if ((fpOutputFile = OpenFile (szTempPathname, "w", TRUE)) == NULL)
    return (TRUE);

  if ((fpInsertFile = OpenFile (pszFilename, "rb", TRUE)) == NULL)
    {
      CloseFile (fpOutputFile);
      return (TRUE);
    }

  if (wReplaceLine == 0)  /* Add Line */
    {
      if (pszSection[0] != '\0' &&
          FindSection (pszSection, fpInsertFile, fpOutputFile) == TRUE)
        {
          /* We are adding the section to the file */
          fReturnValue = OutputLine ("", fpOutputFile);
          if (fReturnValue)
            return (fReturnValue);

          fReturnValue = OutputLine (pszSection, fpOutputFile);
          if (fReturnValue)
            return (fReturnValue);
        }

      fReturnValue = OutputLine (pszCommand, fpOutputFile);
      if (fReturnValue)
        return (fReturnValue);

      WriteThrough (fpInsertFile, fpOutputFile);
    }
  /* Replace All */
  else if (wReplaceLine == HD_REPLACE_ALL)
    {
      if (pszSection[0] != '\0')
        FindSection (pszSection, fpInsertFile, fpOutputFile);

      HandleDuplicates ((WORD) HD_REPLACE_ALL, pszSearchString, pszCommand,
                        fpInsertFile, fpOutputFile, NULL);
    }
  else  /* Replace single line */
    {
      if (pszSection[0] != '\0')
        FindSection (pszSection, fpInsertFile, fpOutputFile);

      HandleDuplicates (wReplaceLine, pszSearchString,
                        pszCommand, fpInsertFile,
                        fpOutputFile, NULL);
    }

  /* Close both files */

  CloseFile (fpInsertFile);
  CloseFile (fpOutputFile);

  /* Delete .BAK file */

  strcpy (szBakPathname, pszFilename);

  /* Clear out the original file's extention, if it exists */

  for (i = strlen (szBakPathname) - 1; i >= 0 &&
                                       szBakPathname[i] != '.'; --i)
    ;

  if (i != -1)
    szBakPathname[i] = '\0';

  strcat (szBakPathname, ".BAK");

  DeleteFile (szBakPathname);

  /* Rename main file to .BAK */
  RenameFile (pszFilename, szBakPathname);

  /* Rename temp file to main filename */
  RenameFile (szTempPathname, pszFilename);

  return (FALSE);
}


/*********************************************************************
 * ReadCommands - Reads the "Insert Commands" commands from the
 *                MSD.INI file and adds them to the listbox pointed
 *                to by pwnd.
 *
 * pwnd - Listbox for storing the commands.
 ********************************************************************/

VOID ReadCommands (PWND pwnd)
{
  FILE *fp;   /* File handle to the MSD.INI file */
  CHAR szIniLine[MAX_INI_LINE_LENGTH + 1];  /* Input line from MSD.INI */
  WORD i;     /* Looping variable */


  /* Clear out the listbox */
  SendMessage (pwnd, LB_RESETCONTENT, NULL, NULL);

  /* Open the file */
  fp = OpenIniFile();

  if (fp != NULL && FindSection ("[Commands]", fp, NULL) == FALSE)
    {
      /* Read the lines from the MSD.INI file */
      while (ReadLine (szIniLine, MAX_INI_LINE_LENGTH, fp, FALSE) != EOF)
        {
          /* Break out if this is the end of the section */
          if (szIniLine[0] == '[')
            break;

          /* Ignore comment lines and blank lines */
          if (szIniLine[0] == ';' || szIniLine[0] == '\0')
            continue;

          /* Add the line to the list box */
          AddIniLine (pwnd, szIniLine);
        }

      CloseFile (fp);
    }
  else
    {
      /* Default MSD.INI file settings */
      for (i = 0; paszDefaultMsdIni[i] != NULL; ++i)
        AddIniLine (pwnd, paszDefaultMsdIni[i]);
    }

  return;
}


/*********************************************************************
 * FindSection - Finds the specified section entry in fpInsertFile.
 *               All information with fpInsertFile up to and including
 *               the section line are written to fpOutputFile.
 *
 * pszSection   - Section to search for.
 * fpInsertFile - File to read.
 * fpOutputFile - File to write.  NULL if no write action is
 *                necessary.
 *
 * Returns:  TRUE if an error occured, or the section was not found.
 *           FALSE if the section was found.
 *********************************************************************/

BOOL FindSection (PSZ  pszSection, FILE *fpInsertFile, FILE *fpOutputFile)
{
  CHAR chBuffer [MAX_INI_LINE_LENGTH + 1];  /* Character buffer - no line */
                                            /*   is assumed to be more    */
                                            /*   than MAX_LINE_LENGTH     */
                                            /*   bytes in length.         */
  WORD i1, i2;                              /* Looping variables          */

  /* If the file does not have a section entry, return FALSE */

  if (pszSection[0] == '\0')
    return (TRUE);

  /*************************/
  /* Find the section here */
  /*************************/

  /* Read the line */

  while (ReadLine (chBuffer, MAX_INI_LINE_LENGTH, fpInsertFile, FALSE) != EOF)
    {
      /* Write it out to the output line */

      if (fpOutputFile != NULL)
        if (OutputLine (chBuffer, fpOutputFile))
          return (TRUE);

      /* Skip whitespace at beginning of chBuffer */

      for (i1 = 0; i1 < MAX_INI_LINE_LENGTH && chBuffer[i1] != '\0' &&

⌨️ 快捷键说明

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