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

📄 !best001.c

📁 supplementary source file 1 for The BESTLibrary
💻 C
📖 第 1 页 / 共 2 页
字号:
 * RETURNS:
 * = pointer to contents of file read in (file pointer is set to byte just
 *   beyond match
 * = NULL if no match was found (file pointer is set to EOF)
 */
char *fil_read_to_strip(char *str, word *size, FILE *f)
{
  char ch,                             // temporary character holder
      *text;                           // text input buffer
  int  index_str,                      // index into string-to-read-to
       quotes = FALSE;                 // [inside a quote?] flag
  word index = 0,                      // generic buffer character index
       size_org,                       // original initial size of buffer
       spaces = 1;                     // count of ' ' (space) characters

  text = (char *) malloc(size_org = *size);
 while (TRUE)
 {
  do {
    ch = getc(f);                      // get next character
    while (TRUE)
    {
      if (ch != ' ') spaces = 0;       // reset space counter

      if (ch == '[') {
        if (!quotes)          quotes = 2;
        break;
      }
      if (ch == ']') {
        if (quotes == 2)      quotes = FALSE;
        break;
      }
      if (ch == '"') {
        if (!quotes)          quotes = 1;
        else if (quotes == 1) quotes = FALSE;
        break;
      }

      if (ch == CR) {
        // clear out all following CR/LF pairs
        for (ch = getc(f), ch = getc(f); ch == CR || ch == LF; ch = getc(f));
        quotes = FALSE, spaces = 1;
        if (index) {                   // if not first character
          text[index++] = LF;          //  store one EOL character
          if (index+1 >= *size)        //  allocate more memory if required
            text = (char *) realloc(text, *size += size_org);
        }
        continue;                      // check out new character
      }

      if (!quotes) {
        if (ch == ';') {               // remove all comments
          if (text[index-1] == ' ')
            index--;
          while ((ch = getc(f)) != CR && ch != EOF);
          continue;                    // check out new character
        }
        if (ch == '=') {               // ignore empty spaces surrounding '='
          if (text[index-1] == ' ')
            index--;
          spaces = 1;                  // if it is a space, will remove it
          break;                       // go store '='
        }
        if (ch == ' ' && ++spaces > 1) { // ignore 2+ blank spaces
          while ((ch = getc(f)) == ' ');
          continue;                    // check out new character
        }
      }

      if (ch == EOF) {                 // if we hit end of file
        free(text);                    //  no match was found (free up memory)
        return( NULL );                //  return NULL
      }
      break;                           // character is valid, so go store it
    }
    text[index++] = ch;                // store valid character
    if (index+1 >= *size)              // allocate more memory if required
      text = (char *) realloc(text, *size += size_org);
  } while (ch != str[0]);

  if (str_len(str) == 1) {
    text[1] = NULL;                    // end string with NULL-terminator
    return( text );                    // return pointer to text input holder
  }
  else {
    for (index_str = 0; (ch = getc(f)) == str[++index_str]; );
    if (index_str == str_len(str)) {   // if we found the string
      fseek(f, -1, SEEK_CUR);          //  rewind to byte just after match
      if (index+index_str > *size)     //  allocate more memory if required
        text = (char *) realloc(text, *size = index_str+index);
      str_copy(text + index, str+1);
      return( text );                  //  return pointer to text input holder
    }
    else if (ch == EOF) {              // if we hit end of file
      free(text);                      //  no match was found (free up memory)
      return( NULL );                  //  return NULL
    }
  }

  fseek(f, -index_str, SEEK_CUR);      // false alarm -- rewind
 }
}

/*----------------------------------------------------------------------------
 * Search a file for a string and set the file pointer just past it.
 *
 * "str" - string to skip past
 * "f"   - file handle of file to read from
 *
 * RETURNS:
 * The file pointer is set to one byte beyond the match or
 *   to the end-of-file if no match was found.
 * = number of bytes scanned upto (and including) last byte of match
 * = FALSE if no match was found
 */
word fil_skip_past(char *str, FILE *f)
{
  int  index;                          // input string character index
  char ch;                             // temporary character holder
  word offset = 0;                     // file character offset

 while (TRUE)
 {
  for (offset++; (ch = getc(f)) != str[0]; offset++)
    if (ch == EOF) return( FALSE );

  if (str_len(str) == 1)
    return( offset );
  else {
    for (index = 0, offset++; (ch = getc(f)) == str[++index]; offset++);
    if (index == str_len(str)) {       // if we found the string
      fseek(f, -1, SEEK_CUR);          //   rewind to byte just after match
      return( offset-1 );              //   return ending offset of find
    }
    else if (ch == EOF)                // if we hit end of file
      return( FALSE );                 //   return FALSE
  }

  fseek(f, -index, SEEK_CUR);          // false alarm -- rewind
  offset -= index;                     // false alarm -- rewind
 }
}

/*----------------------------------------------------------------------------
 * Search a file for a string and set the file pointer to it.
 *
 * "str" - string to set file pointer to
 * "f"   - file handle of file to read from
 *
 * RETURNS:
 * The file pointer is set to the first byte of the first match
 *   the end-of-file if no match was found.
 * = number of bytes scanned upto (and including) first byte of match
 *   file pointer set to the first byte of match
 * = FALSE if no match was found
 *   file pointer set to end-of-file
 */
word fil_skip_to(char *str, FILE *f)
{
  int  index;                          // input string character index
  char ch;                             // temporary character holder
  word offset = 0;                     // file character offset

 while (TRUE)
 {
  for (offset++; (ch = getc(f)) != str[0]; offset++)
    if (ch == EOF) return( FALSE );

  if (str_len(str) == 1) {
    fseek(f, -1, SEEK_CUR);            // rewind to beginning of match
    return( offset );
  }
  else {
    for (index = 0, offset++; (ch = getc(f)) == str[++index]; offset++);
    if (index == str_len(str)) {       // if we found the string
      fseek(f, -(index+1), SEEK_CUR);  //   rewind to first byte of match
      return( offset-index );          //   return beginning offset of find
    }
    else if (ch == EOF)                // if we hit end of file
      return( FALSE );                 //   return FALSE
  }

  fseek(f, -index, SEEK_CUR);          // false alarm -- rewind
  offset -= index;                     // false alarm -- rewind
 }
}

/*----------------------------------------------------------------------------
 * Return the next string from a string in memory.
 *
 * "text" - string to get the next string from
 *
 * RETURNS:
 * = pointer to next string
 */
char *str_next_str(char *text)
{
  char *str;                           // string to return
  word begin,                          // offset to beginning of next word
       index;                          // index into string to read from

  /* ignore initial whitespaces */
  for (index = 0; !is_letternum(text[index]) && text[index] != QUOTE; index++)
    if (!text[index]) return( NULL );  // if we hit end of string, return NULL

  /* get length of next word */
  begin = index;                       // save offset to beginning of nextword
  if (text[index] == QUOTE) {
    while (TRUE) {
      if (!text[index])                // if we hit end of string
        return( NULL );                //  return NULL
      if (text[++index] == QUOTE) {    // if next character is a '"'
        index++;                       //  make sure the end quote is stored
        break;                         //  break out of loop
      }
      if (text[index] == CR ||
          text[index] == LF)           // if we hit end of line
        break;                         //  break out of loop
    }
  }
  else
    while (is_letternum(text[++index]));

  /* allocate memory and make copy of the word */
  str = (char *) malloc(index-begin + 1);
  mem_copy(str, text+begin, index-begin);
  str[index-begin] = NULL;             // end string with NULL-terminator

  return( str );                       // return next string
}

/*----------------------------------------------------------------------------
 * Return the next word from a string in memory.
 *
 * "text" - string to get the next word from
 *
 * RETURNS:
 * = pointer to next word
 */
char *str_next_word(char *text)
{
  char *str;                           // str input buffer
  word  begin,                         // offset to beginning of next word
        index;                         // index into string to read from

  /* ignore initial whitespaces */
  for (index = 0; !is_letter(text[index]); index++);

  /* get length of next word */
  for (begin = index; is_letter(text[++index]); );

  /* allocate memory and make a copy of the word */
  str = (char *) malloc(index-begin + 1);
  mem_copy(str, text+begin, index-begin);
  str[index-begin] = NULL;             // end word with NULL-terminator

  return( str );                       // return next word
}

/*----------------------------------------------------------------------------
 * Convert the boolean FALSE or TRUE into the string "FALSE" or "TRUE",
 * respectively.
 *
 * "bool" - boolean to convert
 *
 * RETURNS:
 * = "TRUE"  if "bool" = TRUE
 * = "FALSE" if "bool" = FALSE
 */
char *con_bool_to_str(boolean bool)
{
  if (!bool)                           // is boolean = FALSE
    return( "FALSE" );                 //  yes, so return "FALSE"
  return( "TRUE" );                    // else return "TRUE"
}

/*----------------------------------------------------------------------------
 * Convert the numeric representation of a color to its string equivalent.
 *
 * "color" - color to convert
 *
 * RETURNS:
 * = string representation of "color"
 * = NULL if "color" is not a valid color
 */
char *con_color_to_str(byte color)
{
  if (color == BLACK       ) return( "BLACK"        );
  if (color == BLUE        ) return( "BLUE"         );
  if (color == GREEN       ) return( "GREEN"        );
  if (color == CYAN        ) return( "CYAN"         );
  if (color == RED         ) return( "RED"          );
  if (color == MAGENTA     ) return( "MAGENTA"      );
  if (color == BROWN       ) return( "BROWN"        );
  if (color == LIGHTGREY   ) return( "LIGHTGREY"    );
  if (color == DARKGREY    ) return( "DARKGREY"     );
  if (color == LIGHTBLUE   ) return( "LIGHTBLUE"    );
  if (color == LIGHTGREEN  ) return( "LIGHTGREEN"   );
  if (color == LIGHTCYAN   ) return( "LIGHTCYAN"    );
  if (color == LIGHTRED    ) return( "LIGHTRED"     );
  if (color == LIGHTMAGENTA) return( "LIGHTMAGENTA" );
  if (color == YELLOW      ) return( "YELLOW"       );
  if (color == WHITE       ) return( "WHITE"        );
  return( NULL );                      // no color matched -- return NULL
}

/*----------------------------------------------------------------------------
 * Convert the string representation of a boolean value into boolean FALSE or TRUE,
 * respectively.
 *
 * "str" - word to convert
 *
 * RETURNS:
 * = TRUE  if "str" = "TRUE"
 * = FALSE if "str" = "FALSE"
 * = -1    if "str" is not a valid boolean word
 */
shortint con_str_to_bool(char *str)
{
  str_case_up(str);                    // make sure we are in uppercase
  if (!str_cmp(str, "FALSE"))          // is string = "FALSE"
    return( FALSE );                   //  yes, so return FALSE
  if (str_cmp(str, "TRUE"))            // is string = "TRUE"
    return( -1 );                      //  no, so return -1
  return( TRUE );                      // else return TRUE
}

/*----------------------------------------------------------------------------
 * Convert the name of a color into its numeric equivalent.
 *
 * "str" - color to convert
 *
 * RETURNS:
 * = color value
 * = -1 if "str" is not a valid color
 */
shortint con_str_to_color(char *str)
{
  str_case_up(str);                    // make sure we are in uppercase
  if (!str_cmp(str, "BLACK")       ) return( BLACK        );
  if (!str_cmp(str, "BLUE")        ) return( BLUE         );
  if (!str_cmp(str, "GREEN")       ) return( GREEN        );
  if (!str_cmp(str, "CYAN")        ) return( CYAN         );
  if (!str_cmp(str, "RED")         ) return( RED          );
  if (!str_cmp(str, "MAGENTA")     ) return( MAGENTA      );
  if (!str_cmp(str, "BROWN")       ) return( BROWN        );
  if (!str_cmp(str, "LIGHTGREY")   ) return( LIGHTGREY    );
  if (!str_cmp(str, "DARKGREY")    ) return( DARKGREY     );
  if (!str_cmp(str, "LIGHTBLUE")   ) return( LIGHTBLUE    );
  if (!str_cmp(str, "LIGHTGREEN")  ) return( LIGHTGREEN   );
  if (!str_cmp(str, "LIGHTCYAN")   ) return( LIGHTCYAN    );
  if (!str_cmp(str, "LIGHTRED")    ) return( LIGHTRED     );
  if (!str_cmp(str, "LIGHTMAGENTA")) return( LIGHTMAGENTA );
  if (!str_cmp(str, "YELLOW")      ) return( YELLOW       );
  if (!str_cmp(str, "WHITE")       ) return( WHITE        );
  return( -1 );                        // no color matched -- return -1
}

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

⌨️ 快捷键说明

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