Jabber WindowGram Client (JWGC)

Introduction Screenshots Installation Downloads
Documentation Browse Source Resources Project Site

Stable Version
-none-

Latest Version
beta5



Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

new_string.c

Go to the documentation of this file.
00001 /*
00002  *      Copyright (c) 1989 by the Massachusetts Institute of Technology.
00003  *      For copying and distribution information, see the file
00004  *      "mit-copyright.h".
00005  *
00006  *      Modified for jwgc by Daniel Henninger.
00007  */
00008 
00009 #include "mit-copyright.h"
00010 
00011 #include "new_string.h"
00012 #include "main.h"
00013 
00014 /*
00015  * string - a module providing operations on C strings.  (i.e., char *'s)
00016  *
00017  * Overview:
00018  *
00019  *        A string is a standard C string.  I.e., a char pointer to a
00020  *    null-terminated sequence of characters.  0 is NOT considered a valid
00021  *    string!  Various operations are available.  See the string_spec file
00022  *    for details.
00023  *
00024  *    Note: This module assumes that malloc NEVER returns 0 for reasonable
00025  *          requests.  It is the users responsibility to either ensure that
00026  *          this happens or supply a version of malloc with error
00027  *          handling.
00028  *
00029  *    Some strings are mutable.
00030  */
00031 
00032 #define  assert(x)
00033 
00034 #define string_Length(s) strlen(s)
00035 /* typedef char *string; */
00036 
00037 /*
00038  *    string string_CreateFromData(char *data, int length):
00039  *        Requires: data[0], data[1], ..., data[length-1] != 0
00040  *        Effects: Takes the first length characters at data and
00041  *                 creates a string containing them.  The returned string
00042  *                 is on the heap & must be freed eventually.
00043  *                 I.e., if passed "foobar" and 3, it would return
00044  *                 string_Copy("foo").
00045  */
00046 
00047 string 
00048 string__CreateFromData(data, length)
00049         char *data;
00050         int length;
00051 {
00052         string result;
00053 
00054         assert(length >= 0);
00055 
00056         result = (string) malloc(length + 1);
00057         assert(result);
00058 
00059         (void) memcpy(result, data, length);
00060         result[length] = 0;
00061 
00062         return (result);
00063 }
00064 
00065 /*
00066  *    string string_Copy(string s):
00067  *        Effects: Returns a copy of s on the heap.  The copy must be
00068  *                 freed eventually.
00069  */
00070 
00071 string 
00072 string__Copy(s)
00073         string s;
00074 {
00075         int length;
00076         string result;
00077 
00078         assert(s);
00079 
00080         length = string_Length(s) + 1;
00081         result = (string) malloc(length);
00082         assert(result);
00083 
00084         (void) memcpy(result, s, length);
00085         return (result);
00086 }
00087 
00088 /*
00089  *    string string_Concat(string a, b):
00090  *        Effects: Returns a string equal to a concatenated to b.
00091  *                 The returned string is on the heap and must be
00092  *                 freed eventually.  I.e., given "abc" and "def",
00093  *                 returns string_Copy("abcdef").
00094  */
00095 
00096 string 
00097 string__Concat(a, b)
00098         string a, b;
00099 {
00100         string result;
00101         int a_length, b_size, result_size;
00102 
00103         a_length = string_Length(a);
00104         b_size = string_Length(b) + 1;
00105         result_size = a_length + b_size;
00106         result = (string) malloc(result_size);
00107         assert(result);
00108 
00109         (void) memcpy(result, a, a_length);
00110         (void) memcpy(result + a_length, b, b_size);
00111 
00112         return (result);
00113 }
00114 
00115 /*
00116  *    string string_Concat2(string a, b):
00117  *        Modifies: a
00118  *        Requires: a is on the heap, b does not point into a.
00119  *        Effects: Equivalent to:
00120  *                     string temp;
00121  *                     temp = string_Concat(a,b);
00122  *                     free(a);
00123  *                     return(temp);
00124  *                 only faster.  I.e., uses realloc instead of malloc+memcpy.
00125  */
00126 
00127 string 
00128 string__Concat2(a, b)
00129         string a, b;
00130 {
00131         int a_length = string_Length(a);
00132         int b_size = string_Length(b) + 1;
00133 
00134         a = (string) realloc(a, a_length + b_size);
00135         assert(a);
00136         (void) memcpy(a + a_length, b, b_size);
00137 
00138         return (a);
00139 }
00140 
00141 /*
00142  *    string string_Downcase(string s):
00143  *        Modifies: s
00144  *        Effects: Modifies s by changing every uppercase character in s
00145  *                 to the corresponding lowercase character.  Nothing else
00146  *                 is changed.  I.e., "FoObAr19." is changed to "foobar19.".
00147  *                 S is returned as a convenience.
00148  */
00149 
00150 string 
00151 string_Downcase(s)
00152         string s;
00153 {
00154         char *ptr;
00155 
00156         for (ptr = s; *ptr; ptr++) {
00157                 if (isupper(*ptr))
00158                         *ptr = tolower(*ptr);
00159         }
00160 
00161         return (s);
00162 }
00163 
00164 /*
00165  *    string string_Upcase(string s):
00166  *        Modifies: s
00167  *        Effects: Modifies s by changing every lowercase character in s
00168  *                 to the corresponding uppercase character.  Nothing else
00169  *                 is changed.  I.e., "FoObAr19." is changed to "FOOBAR19.".
00170  *                 S is returned as a convenience.
00171  */
00172 
00173 string 
00174 string_Upcase(s)
00175         string s;
00176 {
00177         char *ptr;
00178 
00179         for (ptr = s; *ptr; ptr++) {
00180                 if (islower(*ptr))
00181                         *ptr = toupper(*ptr);
00182         }
00183 
00184         return (s);
00185 }


Last updated at Tue Dec 18 21:07:42 PST 2007. This site and project hosted by...SourceForge.net Logo
Source Perspective by Fisheye