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

📄 !best004.c

📁 supplementary source file 1 for The BESTLibrary
💻 C
📖 第 1 页 / 共 2 页
字号:
  str = ini_verify_format(section, section_title, option, ptr);

  if ((num = atoi(str)) == 25 || num == 50)
    return( num-1 );

  str_case_up(str);
  if (str_cmp(str, "UNMODIFY"))
    ini_get_error(section, str, ptr, ROWS);

  return( txt_rows() );
}

/*----------------------------------------------------------------------------
 * Translate the keyboard state "ON", "OFF", or "UNMODIFY" into the numerical
 *   equivalent.
 *
 * "section"       - section text
 * "section_title" - section title to read from
 * "option"        - option to read from
 *
 * RETURNS:
 * = 0 - OFF
 * = 1 - ON
 * = 2 - UNMODIFY
 */
byte ini_get_state(char *section, char *section_title, char *option)
{
  char *str;                           // parameter
  word ptr[2];                         // two generic offset pointers

  str = ini_verify_format(section, section_title, option, ptr);

  str_case_up(str);
  if (!str_cmp(str, "OFF"))
    return( 0 );
  if (!str_cmp(str, "ON"))
    return( 1 );
  if (str_cmp(str, "UNMODIFY"))
    ini_get_error(section, str, ptr, STATE);
  return( 2 );
}

/*----------------------------------------------------------------------------
 * Translate a text string surrounded by quotes into a text string not
 *   surrounded by quotes.
 *
 * "section"       - section text
 * "section_title" - section title to read from
 * "option"        - option to read from
 *
 * RETURNS:
 * = pointer to quote-less string
 */
char *ini_get_text(char *section, char *section_title, char *option)
{
  char *str,                           // parameter
       *str2;                          // temporary string space
  word index,                          // generic character index
       ptr[2];                         // two generic offset pointers

  str = ini_verify_format(section, section_title, option, ptr);

  // verify the text is surrounded by quotes
  if (*str != '"' || str_right(str, 1) != '"')
    ini_get_error(section, str, ptr, STRING);

  // remove the quotes and keep just the text
  index = str_len(str)-2;              // remove two quotes
  str2 = malloc(index+1);              // allocate memory for text + NULL
  mem_copy(str2, str+1, index);        // make a copy of the text
  str2[index] = NULL;                  // terminate string
  return( str2 );
}

/*----------------------------------------------------------------------------
 * Translate the response "YES" or "NO" into the boolean TRUE or FALSE.
 *
 * "section"       - section text
 * "section_title" - section title to read from
 * "option"        - option to read from
 *
 * RETURNS:
 * = TRUE  - YES
 * = FALSE - NO
 */
boolean ini_get_yesno(char *section, char *section_title, char *option)
{
  char *str;                           // parameter
  word ptr[2];                         // two generic offset pointers

  str = ini_verify_format(section, section_title, option, ptr);

  str_case_up(str);
  if (!str_cmp(str, "YES"))
    return( TRUE );
  if (str_cmp(str, "NO"))
    ini_get_error(section, str, ptr, BOOLEAN);
  return( FALSE );
}

/*----------------------------------------------------------------------------
 * Read in the relevent information of a section.
 *
 * "str1" - string marking the beginning of the section
 * "str2" - string marking the ending of the section
 * "f"    - file handle of file to read from
 *
 * RETURNS:
 * = pointer to buffer containing relevant section information
 */
char *ini_read_section(char *str1, char *str2, FILE *f)
{
  char *section;     // relevent section information (can double as error msg)
  word  size = 1000;                   // default size of text input buffer

  if (!fil_skip_to(str1, f)) {
    section = (char *) malloc(1000);   // ample error message space
    sprintf(section, ERR06, str1, str1);
    fprintf(stderr, section);
    free(section);
    beep(); exit(4);
  }

  if ((section = fil_read_to_strip(str2, &size, f)) == NULL) {
    section = (char *) malloc(1000);   // ample error message space
    sprintf(section, ERR07, str2);
    fprintf(stderr, section);
    free(section);
    beep(); exit(5);
  }

  return( section );                   // return relevent section information
}

/*----------------------------------------------------------------------------
 * Verify format of the initialization file.
 * Checks if the - section is present
 *               - option is present
 *               - an equals '=' is present
 *
 * "section"       - section text
 * "section_title" - section title to read from
 * "option"        - option to read from
 * "ptr"           - two offset pointers
 *
 * RETURNS:
 * = pointer to next parameter
 */
char *ini_verify_format(char *section, char *section_title,
                        char *option, word *ptr)
{
  char *param;                         // parameter (can double as error msg)
  word  index1,                        // generic buffer index #1
        index2;                        // generic buffer index #2

  // verify the desired section is present
  if (!(ptr[0] = scan_str(section, section_title, NULL, FORWARD))) {
    section[scan_byte(section, LF, NULL, FORWARD)-1] = NULL;
    param = (char *) malloc(1000);     // ample error message space
    sprintf(param, ERR06, section, section_title);     // a section is missing
    fprintf(stderr, param);
    free(param);
    beep(); exit(4);
  }

  // get length of this section
  index1 = scan_byte(section+ptr[0], '[', NULL, FORWARD);

  // skip past section header
  index2 = scan_byte(section+ptr[0], LF, index1, FORWARD);
  ptr[1] = ptr[0]-1;                   // keep an index to section beginning
  ptr[0] += index2;

  // verify the desired option is present
  if (!(index2 = scan_str(section+ptr[0], option, index1-index2, FORWARD))) {
    param = (char *) malloc(1000);     // ample error message space
    sprintf(param, ERR08, section_title, option);      // an option is missing
    fprintf(stderr, param);
    free(param);
    beep(); exit(6);
  }

  // get to the offset of the second letter of that option
  ptr[0] += index2;

  // search for an equal sign
  index2 = scan_byte(section+ptr[0], '=', NULL, FORWARD);

  // make sure the equal sign follows the option in question
  if (index2 != str_len(option)) {
    section[ptr[1]+index1] = NULL;
    param = (char *) malloc(1000);     // ample error message space
    sprintf(param, ERR09, section+ptr[1], option);     // '=' (equals) missing
    fprintf(stderr, param);
    free(param);
    beep(); exit(7);
  }

  // get to the offset to the parameter (just beyond the equal sign)
  ptr[0] += index2;
  param = str_next_str(section+ptr[0]);// read in next parameter
  return( param );                     // return next parameter
}

/*==============================  END-OF-FILE  =============================*/

⌨️ 快捷键说明

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