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

📄 cfgfile.c

📁 dos下一个强大的文本编辑器,支持多文档、多窗口、二进制文件编辑。
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
 * This module contains all the routines needed to redefine key, colors, and
 *   modes via a configuration file.
 *
 * Program Name:  tdecfg
 * Author:        Frank Davis
 * Date:          June 5, 1992
 */


#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "tdecfg.h"
#include "cfgfile.h"


/*
 * reference the structures in config files.
 */
extern struct vcfg cfg;
extern FILE *tde_exe;                  /* FILE pointer to tde.exe */
extern KEY_FUNC key_func;
extern MACRO macros;
extern COLORS temp_colours;
extern MODE_INFO in_modes;
extern long keys_offset;
extern long two_key_offset;
extern long macro_offset;
extern long color_offset;
extern long mode_offset;
extern long sort_offset;



char line_in[2000];             /* line buffer */
int  stroke_count;              /* global variable for macro strokes */
unsigned int line_no;           /* global variable for line count */
int modes[NUM_MODES];


TWO_KEY two_key_list;

SORT_ORDER sort_order;


/*
 * Name:    tdecfgfile
 * Date:    June 5, 1992
 * Notes:   read in a configuration file
 */
void tdecfgfile( void )
{
FILE *config;
char fname[80];
int  rc;

   /*
    * prompt for the configuration file name.
    */
   cls( );
   xygoto( 0, 3 );
   puts( "Enter configuration file name, e.g. tde.cfg  :" );
   gets( fname );

   if (strlen( fname ) != 0) {
      rc = OK;
      if ((rc = access( fname, EXIST )) != 0) {
         puts( "\n\n Error: File not found." );
         getkey( );
         rc = ERROR;
      } else if ((config = fopen( fname, "r" )) == NULL ) {
         puts( "\n\nError: Cannot open configuration file." );
         getkey( );
         rc = ERROR;
      }

      /*
       * if everything is everthing so far, get the current editor settings.
       */
      if (rc == OK) {
         fseek( tde_exe, keys_offset, SEEK_SET );
         fread( (void *)&key_func, sizeof(KEY_FUNC), 1, tde_exe );
         fseek( tde_exe, two_key_offset, SEEK_SET );
         fread( (void *)&two_key_list, sizeof(TWO_KEY), 1, tde_exe );
         fseek( tde_exe, macro_offset, SEEK_SET );
         fread( (void *)&macros, sizeof(MACRO), 1, tde_exe );
         fseek( tde_exe, color_offset, SEEK_SET );
         fread( (void *)&temp_colours, sizeof(COLORS), 1, tde_exe );
         fseek( tde_exe, mode_offset, SEEK_SET );
         fread( (void *)&in_modes, sizeof( MODE_INFO ), 1, tde_exe );
         fseek( tde_exe, sort_offset, SEEK_SET );
         fread( (void *)&sort_order, sizeof( SORT_ORDER ), 1, tde_exe );

         stroke_count = get_stroke_count( );

         /*
          * put the current modes into an array.  it's easier to work
          *   with them in an array.  This is from cfgmode.c.
          */
         modes[Ins]         = in_modes.insert;
         modes[Ind]         = in_modes.indent;
         modes[PTAB]        = in_modes.ptab_size;
         modes[LTAB]        = in_modes.ltab_size;
         modes[Smart]       = in_modes.smart_tab;
         modes[Write_Z]     = in_modes.control_z;
         modes[Crlf]        = in_modes.crlf;
         modes[Trim]        = in_modes.trailing;
         modes[Eol]         = in_modes.show_eol;
         modes[WW]          = in_modes.word_wrap;
         modes[Left]        = in_modes.left_margin;
         modes[Para]        = in_modes.parg_margin;
         modes[Right]       = in_modes.right_margin;
         modes[Size]        = in_modes.cursor_size;
         modes[Backup]      = in_modes.do_backups;
         modes[Ruler]       = in_modes.ruler;
         modes[Date]        = in_modes.date_style;
         modes[Time]        = in_modes.time_style;
         modes[InflateTabs] = in_modes.inflate_tabs;
         modes[Initcase]    = in_modes.search_case;
         modes[JustRM]      = in_modes.right_justify;

         line_no = 1;
         while (!feof( config )) {
            if (fgets( line_in, 1500, config ) == NULL)
               break;
            parse_line( line_in );
            ++line_no;
         }

         /*
          *  if we changed any modes, reset them b4 we write them to tde.exe.
          */
         in_modes.insert        = modes[Ins];
         in_modes.indent        = modes[Ind];
         in_modes.ptab_size     = modes[PTAB];
         in_modes.ltab_size     = modes[LTAB];
         in_modes.smart_tab     = modes[Smart];
         in_modes.control_z     = modes[Write_Z];
         in_modes.crlf          = modes[Crlf];
         in_modes.trailing      = modes[Trim];
         in_modes.show_eol      = modes[Eol];
         in_modes.word_wrap     = modes[WW];
         in_modes.left_margin   = modes[Left];
         in_modes.parg_margin   = modes[Para];
         in_modes.right_margin  = modes[Right];
         in_modes.cursor_size   = modes[Size];
         in_modes.do_backups    = modes[Backup];
         in_modes.ruler         = modes[Ruler];
         in_modes.date_style    = modes[Date];
         in_modes.time_style    = modes[Time];
         in_modes.inflate_tabs  = modes[InflateTabs];
         in_modes.search_case   = modes[Initcase];
         in_modes.right_justify = modes[JustRM];

         fseek( tde_exe, keys_offset, SEEK_SET );
         fwrite( (void *)&key_func, sizeof(KEY_FUNC), 1, tde_exe );
         fseek( tde_exe, two_key_offset, SEEK_SET );
         fwrite( (void *)&two_key_list, sizeof(TWO_KEY), 1, tde_exe );
         fseek( tde_exe, macro_offset, SEEK_SET );
         fwrite( (void *)&macros, sizeof(MACRO), 1, tde_exe );
         fseek( tde_exe, color_offset, SEEK_SET );
         fwrite( (void *)&temp_colours, sizeof(COLORS), 1, tde_exe );
         fseek( tde_exe, mode_offset, SEEK_SET );
         fwrite( (void *)&in_modes, sizeof( MODE_INFO ), 1, tde_exe );
         fseek( tde_exe, sort_offset, SEEK_SET );
         fwrite( (void *)&sort_order, sizeof( SORT_ORDER ), 1, tde_exe );
         fclose( config );
         printf( "\n\n    Configuration file read.  Press a key to continue :" );
         getkey( );
      }
   }
   cls( );
}


/*
 * Name:    parse_line
 * Purpose: real work horse of the configuration utility, figure out what
 *          we need to do with each line of the config file.
 * Date:    June 5, 1992
 * Passed:  line:  line that contains the text to parse
 */
void parse_line( char *line )
{
char key[1042];         /* buffer to hold any token that we parse */
char *residue;          /* pointer to next item in line, if it exists */
int key_no;             /* index into key array */
int parent_key;         /* 1st of two-combination keys */
int color;              /* color field */
int mode_index;         /* index in mode array */
int func_no;            /* function number we want to assign to a key */
int color_no;           /* attribute we want to assign to a color field */
int mode_no;            /* mode number we want to assign to a mode */
int found;              /* boolean, did we find a valid key, color, or mode? */
int i;

   /*
    * find the first token and put it in key.  residue points to next token.
    */
   residue = parse_token( line, key );
   if (*key != '\0' && *key != ';') {
      if (strlen( key ) > 1) {
         /*
          * try to find a valid key
          */
         found = FALSE;
         key_no = search( key, valid_keys, AVAIL_KEYS-1 );
         if (key_no != ERROR) {
            /*
             * find the function assignment
             */
            found = TRUE;
            if (residue != NULL) {
               residue = parse_token( residue, key );

               /*
                * if this is not a comment, find the function to assign
                *   to key.  clear any previous macro or key assignment.
                */
               if (*key != '\0' && *key != ';') {
                  func_no = search( key, valid_func, NUM_FUNC );
                  if (func_no != ERROR) {
                     clear_previous_twokey( key_no );
                     clear_previous_macro( key_no );
                     key_func.key[key_no] = func_no;
                     if (func_no == PlayBack)
                        parse_macro( key_no, residue );
                  } else {
                     parent_key = key_no;

                     /*
                      * was the second key one letter?
                      */
                     if (strlen( key ) == 1) {
                        key_no = *key;
                        residue = parse_token( residue, key );
                        if (*key != '\0' && *key != ';') {
                           func_no = search( key, valid_func, NUM_FUNC );
                           if (func_no != ERROR && func_no != PlayBack) {
                              if (insert_twokey( parent_key+256, key_no,
                                                          func_no ) == ERROR) {
                                 printf( "==> %s", line_in );
                                 printf( "Out of room for two-key: line %u  : function %s\n",
                                      line_no, key );
                              }
                           } else {
                              printf( "==> %s", line_in );
                              if ( func_no == ERROR)
                                 printf( "Unrecognized function: line %u  : function %s\n",
                                         line_no, key );
                              else
                                 printf( "Cannot assign a macro to two-keys: line %u  : function %s\n",
                                         line_no, key );
                           }
                        }
                     } else {
                        residue = parse_token( residue, key );
                        key_no = search( key, valid_keys, AVAIL_KEYS-1 );
                        if (key_no != ERROR && *key != '\0' && *key != ';') {
                           func_no = search( key, valid_func, NUM_FUNC );
                           if (func_no != ERROR && func_no != PlayBack) {
                              if (insert_twokey( parent_key+256, key_no+256,
                                                        func_no )  == ERROR) {
                                 printf( "==> %s", line_in );
                                 printf( "Out of room for two-key: line %u  : function %s\n",
                                      line_no, key );
                              }
                           } else {
                              printf( "==> %s", line_in );
                              if ( func_no == ERROR)
                                 printf( "Unrecognized function: line %u  : function %s\n",
                                         line_no, key );
                              else
                                 printf( "Cannot assign a macro to two-keys: line %u  : function %s\n",
                                         line_no, key );
                           }
                        } else {
                           printf( "==> %s", line_in );
                           printf( "Unrecognized function: line %u  : function %s\n",
                                   line_no, key );
                        }
                     }
                  }
               }
            }
         }

         /*
          * valid key not found, now try a valid color
          */
         if (!found) {
            color = search( key, valid_colors, (NUM_COLORS * 2) - 1 );
            if (color != ERROR) {
               if (*key == 'm')
                  i = 0;
               else
                  i = 1;
               found = TRUE;
               if (residue != NULL) {
                  residue = parse_token( residue, key );

                  /*
                   * we found a color field and attribute.  now, make sure
                   *   everything is everything before we assign the attribute
                   *   to the color field.
                   */
                  if (*key != '\0' && *key != ';') {
                     color_no = atoi( key );
                     if (color_no >= 0 && color_no <= 127)
                        temp_colours.clr[i][color] = color_no;

⌨️ 快捷键说明

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