delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/06/16/15:44:42

Message-ID: <394A8433.FDE909DD@programmingparadise.com>
Date: Sat, 17 Jun 2000 01:17:01 +0530
From: Vikas Yadav <vikas AT programmingparadise DOT com>
Organization: programmingParadise.com
X-Mailer: Mozilla 4.7 [en] (Win98; I)
X-Accept-Language: en
MIME-Version: 1.0
To: DJGPP <djgpp AT delorie DOT com>
Subject: class String has problem
Reply-To: djgpp AT delorie DOT com

This is a multi-part message in MIME format.
--------------F07E21CD207FAF2F71D2F43E
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

#define MAX_STRING         0x4000
#ifndef min
#define min(x,y) ( x > y ? y : x )
#endif
class String
{
///////////////////////////////////
//---------------------------------
// Hello evrybody.
// I had written this class about 6 months before;
// Everything is running perfect, but this does not look
// to be a healthy code;
//
// What is a *better alternative* that this big stack;
//
//            16K
char ptr1[MAX_STRING];
// This ptr1 is used in function like right()/substring() etc.
// The caller need not supply the extra parameter like
//   ....,char * dest, int dest_size){
// Free from these *extra parameters* make the coding really very easy
// at least for me. :-)
// But this seems to be the price I have to pay.
// I have also defined a heap buffer below
// *which actually holds the string*. This heapbuffer gets resized
// depending on the new string it gets. etc.. etc..
// Due to this HUGE stack "ptr1" I SHOULD not do this:
// String s ="hello world\n"; //insted
// String *s  = new String("hellow world\n"); // as this is far more
safer.
// --Sometimes this huge stack DID actually crash a program;
// --This stack also PUTS A LIMIT TO MAXIMUM POSSIBLE string size;
// --A complete waste of memory
// When you use TC/BCC this will not even compile.
// DJGPP or MSVC do not mind that BIG thing at all.
// My coding style is not at at all good but
//           *it works perfect*.
//
// Any suggestions to improve this class and to remove this
// BOMB from here?
//
// Thank you very much!
// Vikas Yadav
//---------------------------------
///////////////////////////////////
public:
 char *str1;
//////////////////////////////////
 String()
 {
  str1 = NULL;
 }
//////////////////////////////////
    String(char*x1)
    {
  str1 = NULL;
  if(!x1)
   return;
        str1=new char[strlen(x1)+1];
        strcpy(str1,x1);
    }
//////////////////////////////////
 ~String()
 {
  if(str1 && *str1)
        {
   delete str1;
   str1=NULL;
  }
 }
//////////////////////////////////
    char* operator=(void*x1)
    {
  if(str1)
        {
   delete str1;
   str1=NULL;
  }
  if(!x1)
   return (char*)x1;
  //alloc=strlen((const char*)x1)+2;
  //DPrint("=: Allocated %lu for \"%s\"(%lu)",alloc,x1,strlen((const
char*)x1));
  str1=strdup((const char*)x1);
        return (char*)x1;
    }
//////////////////////////////////
    char* operator+=(void* x1)
    {
        int l1;
  if(!x1)
   return (char*)str1;
  unsigned long alloc = l1 = strlen(str1) + strlen((char*)x1) + 2;
  char *str2 = new char[alloc];
        ::sprintf(str2,"%s%s",str1,x1);
        if(str1)
        {
   delete str1;
   str1=NULL;
  }
  str1=str2;
        return str1;
    }
//////////////////////////////////
    char* operator+=(String &x1)
    {
        int l1;
        unsigned long alloc= l1 = strlen(str1) + strlen(x1.str1) + 1 ;
  char *str2 = new char[alloc];
        ::sprintf(str2,"%s%s",str1,x1.str1);
        if(str1)
        {
   delete str1;
   str1=NULL;
  }
  str1=str2;
        return str1;
    }
//////////////////////////////////
    char* right(int r1)
    {
        if(!str1)
   return NULL;
  strcpy(ptr1,str1);
        return (ptr1 + ( strlen(ptr1) - r1));
    }
//////////////////////////////////
    char* left(int r1)
    {
        if(!str1)
   return NULL;
        strcpy(ptr1,str1);
        ptr1[r1]=0;
        return ptr1;
    }
//////////////////////////////////
    short operator==(const char*x1)
    {
  if(!str1 && !strlen(x1))
   return 1;
  return strcmp(str1,x1)==0;
    }
//////////////////////////////////
    short operator!=(const char*x1)
    {
  if(!str1 && !strlen(x1))
   return 0;
        return strcmp(str1,x1)!=0;
    }
//////////////////////////////////
    int length()
    {
  if(!str1)
   return 0;
        return strlen(str1);
    }
//////////////////////////////////
    void toUpperCase()
    {
        if(!str1)
   return;
  unsigned long len = strlen( str1 );
  for( char *p = str1; p < (str1 + len); p++ )
        {
            if( islower( *p ) )
              *p = (char)toupper( *p );
        }
    }
//////////////////////////////////
    void toLowerCase()
    {
        if(!str1)
   return;
  unsigned long len = strlen( str1 );
  for( char *p = str1; p < (str1 + len); p++ )
        {
        if( isupper( *p ) )
           *p =(char)tolower( *p );
        }
    }
//////////////////////////////////
    char* operator+(char*x1)
    {
        if(!x1)
   return (char*)str1;
  ::sprintf(ptr1,"%s%s",str1,x1);
        return ptr1;
    }
//////////////////////////////////
// This operator is also a BUG.
// I want to use the String class something like this
//     String s;
//     fscanf(infile,1,255,s);
// This is not possible at current state.
// "char*" operator should be clever enough.
//
/*  operator char*()
 *  {
 *      strcpy(ptr1,str1);
 *      delete str1;
 *      str1=new char[MAX_STRING];
 *      strcpy(str1,ptr1);
 *      return str1;
 *  }
 */
//////////////////////////////////
    long lastIndexOf(char x1)
    {
        if(!str1)
   return -1;
        long c1=-1;
  for(c1=length(); c1>0; c1--)
            if( str1[c1] == x1 )
                return c1;
        return c1;
    }
//////////////////////////////////
    long indexOf(char x1)
    {
        if(!str1)
   return -1;
  long c1=-1,c2=length();
  for(c1=0; c1<c2; c1++)
  {
   if( str1[c1] == x1 )
                return c1;
  }
        return -1;
    }
//////////////////////////////////
    long indexOf(char* x1)
    {
        if(!str1)
   return -1;
  char*x2;
        if( (x2=strstr(str1, x1)) == NULL)
            return -1;
        return (x2-str1+1);
    }
//////////////////////////////////
    void trim()
    {
        if(!str1)
   return;
  char str2[MAX_STRING],*ptr2;
        strcpy(str2,str1);
        int c1,l;
        String s1;
        s1=str2;
        ptr2=str2;
  for(c1=0; isspace(s1.str1[c1]); c1++);
        ptr2 += c1;
        s1 = ptr2;
        for( c1=s1.length()-1; isspace(s1.str1[c1]); c1--);
        s1.left(c1);
  if(s1.str1[(l=length())-1]==10)
   s1.str1[l-1]=0;
        strcpy(str1,s1.str1);
    }
//////////////////////////////////
 char*substring(DWORD start1, DWORD len1=0)
 {
  if(!str1)
   return NULL;
  if(len1 == 0)
   len1 = length();
  String p1;
  DWORD l1 = length();
  p1 = right( l1 - start1 );
  p1 = p1.left( len1 );
  strcpy( ptr1 , p1.str1 );
  return ptr1;
 }
//////////////////////////////////
 char operator[](int x)
 {
  if(!str1)
   return NULL;
  return str1[x];
 }
//////////////////////////////////
 char* sprintf(char* format, ...)
 {
  char buf[MAX_STRING];
  if(!format)
   return NULL;
  va_list argptr;
  va_start(argptr,format);
  vsprintf(buf,format,argptr);
  va_end(argptr);
  if(str1)
        {
   delete str1;
   str1=NULL;
  }
  return str1=strdup(buf);
 }
};

//#define PutStaticString char String::ptr1[MAX_STRING]
//#if    defined( MAIN_DEFINED ) || defined( _WINMAINISHERE_ )
//PutStaticString;
//#endif /* MAIN_DEFINED */

#endif /* _STRING_ */

// Download the completle File from
// http://www.programmingparadise.com/downloads/ulib.zip

--------------F07E21CD207FAF2F71D2F43E
Content-Type: text/x-vcard; charset=us-ascii;
 name="vikas.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Vikas Yadav
Content-Disposition: attachment;
 filename="vikas.vcf"

begin:vcard 
n:Yadav;Vikas
tel;home:+91 044 4423440
tel;work:+91 044 4423440
x-mozilla-html:TRUE
url:http://www.programmingparadise.com/
org:programmingParadise.com;Webmaster
version:2.1
email;internet:vikas AT programmingparadise DOT com
title:Vikas
adr;quoted-printable:;;Flat B-1, Ashok Kirthi,=0D=0A152, L. B. Road;Chennai;Tamil Nadu;600041;India
fn:Vikas Yadav
end:vcard

--------------F07E21CD207FAF2F71D2F43E--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019