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

📄 string.c

📁 CC386 is a general-purpose 32-bit C compiler. It is not an optimizing compiler but given that the co
💻 C
📖 第 1 页 / 共 2 页
字号:
/* 
   Copyright 1994-2003 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
   02111-1307, USA.  

   You may contact the author at:

   mailto::camille@bluegrass.net

   or by snail mail at:

   David Lindauer
   850 Washburn Ave Apt 99
   Louisville, KY 40222
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <dos.h>
#include <time.h>

#include "langext.h"
#include "defines.h"
#include "types.h"
#include "subs.h"
#include "globals.h"
/*                                 STRING.C                                */

/*+-------------------------------------------------------------------------+
  |                                                                         |
  |                        allocate_string                                  |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
string_ptr allocate_string(bit_16 len)
BeginDeclarations
string_ptr                             temp;
#define Temp                           (*temp)
EndDeclarations
BeginCode
 temp = StringPtr(allocate_memory(Bit_32(sizeof(string_type))+ \
                                        Bit_32(len)));
 Temp.max_length = len;
 Temp.length     = 0;
 Temp.text[0]    = '\000';
 return(temp);
EndCode
#undef Temp

/*+-------------------------------------------------------------------------+
  |                                                                         |
  |                         compare_string                                  |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
int_16 compare_string(string_ptr left, string_ptr right)
BeginDeclarations
bit_16                                 left_len;
bit_16                                 right_len;
EndDeclarations
BeginCode
 left_len  = Length(left);
 right_len = Length(right);
 If (left_len IsZero) AndIf (right_len IsZero)
  Then
   return(0);
  EndIf;
 If left_len LessThan right_len
  Then
   return(-1);
  EndIf;
 If left_len GreaterThan right_len
  Then
   return(1);
  EndIf;
 return(far_compare(String(left), String(right), left_len));
EndCode

/*+-------------------------------------------------------------------------+
  |                                                                         |
  |                         compare_short_string                            |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
int_16 compare_short_string(string_ptr left, string_ptr right)
BeginDeclarations
bit_16                                 left_len;
EndDeclarations
BeginCode
 left_len = Length(left);
 If left_len IsZero
  Then
   return(0);
  EndIf;
 return(far_compare(String(left), String(right), left_len));
EndCode

/*+-------------------------------------------------------------------------+
  |                                                                         |
  |                         concat_string                                   |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
string_ptr concat_string(string_ptr dest, string_ptr source)
BeginDeclarations
bit_16                                 temp;
EndDeclarations
BeginCode
 If (Length(source) + Length(dest)) Exceeds MaxLength(dest)
  Then
   linker_error(8,"Destination string to small (%u bytes) to hold "
                  "concatination of:\n"
                  "\t\"%s\" (%u bytes) and\n"
                  "\t\"%s\" (%u bytes)\n",
                  MaxLength(dest),
                  String(dest), Length(dest),
                  String(source), Length(dest));
  EndIf;
 temp = Length(source);
 far_move(Addr(String(dest)[Length(dest)]), String(source), temp + 1);
 Length(dest) += temp;
 return(dest);
EndCode

/*+-------------------------------------------------------------------------+
  |                                                                         |
  |                         concat_char_to_string                           |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
string_ptr concat_char_to_string(string_ptr dest, byte c)
BeginDeclarations
EndDeclarations
BeginCode
 If (Length(dest) + 1) Exceeds MaxLength(dest)
  Then
   linker_error(8,"Destination string to small (%u bytes) to add \"%c\" "
                  "to string:\n"
                  "\t\"%s\" (%u bytes)\n",
                  MaxLength(dest), c,
                  String(dest), Length(dest));
  EndIf;
 String(dest)[Length(dest)++] = c;
 String(dest)[Length(dest)]   = '\000';
 return(dest);
EndCode

/*+-------------------------------------------------------------------------+
  |                                                                         |
  |                         copy_string                                     |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
string_ptr copy_string (string_ptr dest, string_ptr source)
BeginDeclarations
EndDeclarations
BeginCode
 If Length(source) Exceeds MaxLength(dest)
  Then
   linker_error(8,"Destination string to small (%u bytes) to hold:\n"
                  "\t\"%s\" (%u bytes)\n",
                  MaxLength(dest),
                  String(source), Length(dest));
  EndIf;
 Length(dest) = Length(source);
 far_move(String(dest), String(source), Length(source)+1);
 return(dest);
EndCode

/*+-------------------------------------------------------------------------+
  |                                                                         |
  |                            cut_string                                   |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
string_ptr cut_string(string_ptr s, bit_16 at, bit_16 len)
BeginDeclarations
bit_16                                 length_to_right;
bit_16                                 string_length;
EndDeclarations
BeginCode
 string_length = Length(s);
 If string_length NotGreaterThan at
  Then
   return(s);
  EndIf;
 If string_length LessThan (at + len)
  Then
   len = string_length - at;
  EndIf;
 Length(s)       -= len;
 length_to_right  = string_length - (at+len);
 far_move(Addr(String(s)[at]), Addr(String(s)[at+len]), length_to_right+1);
 return(s);
EndCode

/*+-------------------------------------------------------------------------+
  |                                                                         |
  |                            duplicate_string                             |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
string_ptr duplicate_string(string_ptr s)
BeginDeclarations
string_ptr                             temp;
#define Temp                           (*temp)
EndDeclarations
BeginCode
 temp = StringPtr(allocate_memory( \
                                  Bit_32(sizeof(string_type))+ \
                                  Bit_32(Length(s))));
 Temp.max_length = Length(s);
 copy_string(temp, s);
 return(temp);
EndCode
#undef Temp

/*+-------------------------------------------------------------------------+
  |                                                                         |
  |                           edit_number_string                            |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
string_ptr edit_number_string(string_ptr s, char_ptr format, ...)
BeginDeclarations
va_list                                argptr;
bit_16                                 i;
EndDeclarations
BeginCode
 va_start(argptr,format);
 vsprintf((char_ptr) temp_near_string, format, argptr);
 copy_string(s, string(temp_near_string));
 i = Length(s);
 While i Exceeds 3
  BeginWhile
   i -= 3;
   paste_string(s, i, comma_string);
  EndWhile;
 return(s);
EndCode

/*+-------------------------------------------------------------------------+
  |                                                                         |
  |                           index_string                                  |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
bit_16 index_string(string_ptr s, bit_16 from, string_ptr pattern)
BeginDeclarations
bit_16                                 i;
bit_16                                 iteration_count;
byte_ptr                               left;
bit_16                                 len;
byte_ptr                               pat;
bit_16                                 pattern_length;
EndDeclarations
BeginCode
 pattern_length = Length(pattern);

⌨️ 快捷键说明

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