14.2 Cstring Interface

ORIGIN '~beta/basiclib/betaenv';
LIB_DEF 'sysutilscstring' '../lib';
BODY 'private/cstringbody';

(*
 * COPYRIGHT
 *       Copyright Mjolner Informatics, 1992-2000
 *       All rights reserved.
 *)

-- LIB: attributes --

CString:
  (* BETA interface to standard NULL-terminated C-strings, externally
   * allocated, e.g., using malloc.
   *)
  (# charptr: @integer
       (* The pointer to the external string *);
     init: 
       (* Allocate length bytes (default 256) using malloc, and assign the
        * result to charptr. You may want to explicitly free THIS(CString)
        * before a reinitialization.
        *)
       (# length: @integer;
       enter length 
       do ...
       #);
     set: 
       (* Initializes THIS(CString) to be large enough to hold the chars of 
        * T, and then copies the chars of T to THIS(CString). You may want to
        * explicitly free THIS(CString) before this operation, if this is a
        * reinitialization.
        * For convenience, it also exits the char pointer.
        *)
       (# T: ^text; 
       enter T[]
       do ...
       exit charptr
       #);
     get:
       (* Return the chars of THIS(CString) copied into the text T *)
       (# T: ^text;
       do ... 
       exit T[]
       #);
     getChars:
       (* Return a repetition, with the chars of THIS(CString) copied to it *)
       (# R: [0]@char;
       do ...
       exit R
       #);
     length: IntegerValue
       (* Return the string-length if THIS(CString), i.e., the number of chars
        * before the NULL-char in the externally allocated string.
        *)
       (# ... #);
     inxPut:
       (* Put ch at index inx in the externally allocated CString. The first char
        * is at index 0. NOTICE: There is no index check, it is  the programmer to
        * ensure, that it is allowed to put at the given index.
        *)
       (# inx: @integer;
          ch: @char;
       enter (inx, ch) 
          ...
       #);
     inxGet:
       (* Obtain the char at the given index in the externally allocated string.
        * The first char is at index 0. 
        *)
       (# inx: @integer;
          ch: @char;
       enter inx
          ...
       exit ch
       #);
     toLocal:
       (* Convert a BETA CString with newlines to a string containing system
        * line terminators.
        *)
       (# new: ^CString;
       ...
       exit new[]
       #);
     fromLocal:
       (* Convert a CString containing system line terminators to a BETA 
        * string with the proper newline character
        *)
       (# new: ^CString
       ...
       exit new[]
       #);
     free: 
       (* Free the externally allocated string. The chars of the externally
        * allocated string will be unaccessible after this operation.
        *)
       (# ... #);
     
     <<SLOT CStringLib: attributes>>;
     
  enter charptr
  do INNER
  exit charptr
  #);

(* Standard C-library operations to perform on CStrings *)

strlen: External
  (# charptr: @integer;
     result: @integer;
  enter charptr
  exit result
  #);

strcat: External
  (# charptr1, charptr2: @integer;
     result_ptr: @integer;
  enter (charptr1, charptr2)
  exit result_ptr
  #);

strncat: External
  (# charptr1, charptr2: @integer;
     n: @integer;
     result_ptr: @integer;
  enter (charptr1, charptr2, n)
  exit result_ptr
  #);

strdup: External
  (# charptr: @integer;
     result_ptr: @integer;
  enter (charptr)
  exit result_ptr
  #);

strcmp: External
  (# charptr1, charptr2: @integer;
     result: @integer;
  enter (charptr1, charptr2)
  exit result
  #);

strncmp: External
  (# charptr1, charptr2: @integer;
     n: @integer;
     result: @integer;
  enter (charptr1, charptr2, n)
  exit result
  #);

strcpy: External
  (# charptr1, charptr2: @integer;
     result_ptr: @integer;
  enter (charptr1, charptr2)
  exit result_ptr
  #);

strncpy: External
  (# charptr1, charptr2: @integer;
     n: @integer;
     result_ptr: @integer;
  enter (charptr1, charptr2, n)
  exit result_ptr
  #);

strchr: External
  (# charptr: @integer;
     c: @integer;
     result_ptr: @integer;
  enter (charptr, c)
  exit result_ptr
  #);

strrchr: External
  (# charptr: @integer;
     c: @integer;
     result_ptr: @integer;
  enter (charptr, c)
  exit result_ptr
  #);

strpbrk: External
  (# charptr1, charptr2: @integer;
     result_ptr: @integer;
  enter (charptr1, charptr2)
  exit result_ptr
  #);

strspn: External
  (# charptr1, charptr2: @integer;
     result: @integer;
  enter (charptr1, charptr2)
  exit result
  #);

strcspn: External
  (# charptr1, charptr2: @integer;
     result: @integer;
  enter (charptr1, charptr2)
  exit result
  #)


14.2 Cstring Interface
© 1994-2002 Mjølner Informatics
[Modified: Thursday May 4th 2000 at 16:19]