Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #15359 > unrolled thread

Best Way to Handle Unknown Data Sizes?

Started bymrc2323@cox.net (Mike Copeland)
First post2012-04-13 15:03 -0700
Last post2012-04-14 12:20 +1200
Articles 9 — 7 participants

Back to article view | Back to comp.lang.c++


Contents

  Best  Way to Handle Unknown Data Sizes? mrc2323@cox.net (Mike Copeland) - 2012-04-13 15:03 -0700
    Re: Best  Way to Handle Unknown Data Sizes? Ian Collins <ian-news@hotmail.com> - 2012-04-14 10:21 +1200
    Re: Best  Way to Handle Unknown Data Sizes? copx <copx@gazeta.pl> - 2012-04-14 00:38 +0200
    Re: Best  Way to Handle Unknown Data Sizes? Paavo Helde <myfirstname@osa.pri.ee> - 2012-04-13 17:39 -0500
    Re: Best  Way to Handle Unknown Data Sizes? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-04-13 22:42 +0000
      Re: Best  Way to Handle Unknown Data Sizes? red floyd <no.spam.here@its.invalid> - 2012-04-13 16:44 -0700
    Best Way to Handle Unknown Data Sizes?  @Jeff-Relf.Me - 2012-04-13 16:04 -0700
      Best Way to Handle Unknown Data Sizes?  @Jeff-Relf.Me - 2012-04-13 17:16 -0700
        Re: Best Way to Handle Unknown Data Sizes?  Ian Collins <ian-news@hotmail.com> - 2012-04-14 12:20 +1200

#15359 — Best Way to Handle Unknown Data Sizes?

Frommrc2323@cox.net (Mike Copeland)
Date2012-04-13 15:03 -0700
SubjectBest Way to Handle Unknown Data Sizes?
Message-ID<MPG.29f25082df0f84119896fd@news.eternal-september.org>
   I have a number of functions similar to the code below that are 
starting to cause problems when incoming data exceeds the limit (e.g. 
1024) imposed by the code.  Rather than select another arbitrary limit 
and have the problem occur later when larger data appears, I'd like to 
fix the issue the right way.  I'm unsure how to do this...
   Please advise.  TIA

char* TextFunctions::removeCR(char *s) // Remove C/R from data string
{
    static char w[1024];
    int         n;
    char        *p;

    strcpy (w, s);
    n = strlen(w);
    p = strrchr(w, char(10));
    if(p) w[p-w] = '\0';

    return w;
}  // remove CR 

[toc] | [next] | [standalone]


#15361

FromIan Collins <ian-news@hotmail.com>
Date2012-04-14 10:21 +1200
Message-ID<9urn6nFjbhU3@mid.individual.net>
In reply to#15359
On 04/14/12 10:03 AM, Mike Copeland wrote:
>     I have a number of functions similar to the code below that are
> starting to cause problems when incoming data exceeds the limit (e.g.
> 1024) imposed by the code.  Rather than select another arbitrary limit
> and have the problem occur later when larger data appears, I'd like to
> fix the issue the right way.  I'm unsure how to do this...
>     Please advise.  TIA
>
> char* TextFunctions::removeCR(char *s) // Remove C/R from data string
> {
>      static char w[1024];
>      int         n;
>      char        *p;
>
>      strcpy (w, s);
>      n = strlen(w);
>      p = strrchr(w, char(10));
>      if(p) w[p-w] = '\0';
>
>      return w;
> }  // remove CR

Use C++ strings.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#15363

Fromcopx <copx@gazeta.pl>
Date2012-04-14 00:38 +0200
Message-ID<jma9te$s2b$1@speranza.aioe.org>
In reply to#15359
On 14.04.2012 00:03, Mike Copeland wrote:
>     I have a number of functions similar to the code below that are
> starting to cause problems when incoming data exceeds the limit (e.g.
> 1024) imposed by the code.  Rather than select another arbitrary limit
> and have the problem occur later when larger data appears, I'd like to
> fix the issue the right way.  I'm unsure how to do this...
>     Please advise.  TIA
>
> char* TextFunctions::removeCR(char *s) // Remove C/R from data string
> {
>      static char w[1024];
>      int         n;
>      char        *p;
>
>      strcpy (w, s);
>      n = strlen(w);
>      p = strrchr(w, char(10));
>      if(p) w[p-w] = '\0';
>
>      return w;
> }  // remove CR

That's C-style code, not really idiomatic C++. Plus, it sounds and looks 
like homework.

But well, I will give you a few pointers (pun intended):

In C-style code you would dynamically allocate, reallocate, and 
eventually free memory as needed. The related functions are: malloc(), 
realloc(), and free() -- do a web search

In "modern C++" you are supposed to use the container types in the STL. 
Web search for: std::string and std::vector



[toc] | [prev] | [next] | [standalone]


#15364

FromPaavo Helde <myfirstname@osa.pri.ee>
Date2012-04-13 17:39 -0500
Message-ID<XnsA03510EFF6C10myfirstnameosapriee@216.196.109.131>
In reply to#15359
mrc2323@cox.net (Mike Copeland) wrote in 
news:MPG.29f25082df0f84119896fd@news.eternal-september.org:

>    I have a number of functions similar to the code below that are 
> starting to cause problems when incoming data exceeds the limit (e.g. 
> 1024) imposed by the code.  Rather than select another arbitrary limit 
> and have the problem occur later when larger data appears, I'd like to 
> fix the issue the right way.  I'm unsure how to do this...
>    Please advise.  TIA
> 
> char* TextFunctions::removeCR(char *s) // Remove C/R from data string
> {
>     static char w[1024];
>     int         n;
>     char        *p;
> 
>     strcpy (w, s);
>     n = strlen(w);
>     p = strrchr(w, char(10));
>     if(p) w[p-w] = '\0';
> 
>     return w;
> }  // remove CR 

Please note that the above function is named and commented as removing CR 
(carriage return, ASCII code 13) from the string, whereas actually it 
truncates the string at the last LF byte (line feed, ASCII code 10), and 
only the last one. So it removes a lot more and a lot less than it 
promises.

Also note that using a static buffer could create havoc in multithreaded 
programs.

OK, the same in C++ now, without the static buffer:

std::string TruncateAtLastLF(const std::string& s) {
    return s.substr(0, s.rfind('\n'));
}

hth
Paavo

[toc] | [prev] | [next] | [standalone]


#15365

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2012-04-13 22:42 +0000
Message-ID<slrnjohau5.1ls.grahn+nntp@frailea.sa.invalid>
In reply to#15359
On Fri, 2012-04-13, Mike Copeland wrote:
>    I have a number of functions similar to the code below that are 
> starting to cause problems when incoming data exceeds the limit (e.g. 
> 1024) imposed by the code.  Rather than select another arbitrary limit 
> and have the problem occur later when larger data appears, I'd like to 
> fix the issue the right way.  I'm unsure how to do this...
>    Please advise.  TIA
>
> char* TextFunctions::removeCR(char *s) // Remove C/R from data string
> {
>     static char w[1024];
>     int         n;
>     char        *p;
>
>     strcpy (w, s);
>     n = strlen(w);
>     p = strrchr(w, char(10));

Something (apart from the function's design) is wrong here -- ASCII CR
(not "C/R") is decimal 13. LF is 10.

And if you mean C++ '\n', say '\n' instead of hardcoding a numeric
value.

/Jorgen

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .     .
\X/     snipabacken.se>   O  o   .

[toc] | [prev] | [next] | [standalone]


#15368

Fromred floyd <no.spam.here@its.invalid>
Date2012-04-13 16:44 -0700
Message-ID<jmadpe$6uv$1@dont-email.me>
In reply to#15365
On 4/13/2012 3:42 PM, Jorgen Grahn wrote:
> On Fri, 2012-04-13, Mike Copeland wrote:
>>     I have a number of functions similar to the code below that are
>> starting to cause problems when incoming data exceeds the limit (e.g.
>> 1024) imposed by the code.  Rather than select another arbitrary limit
>> and have the problem occur later when larger data appears, I'd like to
>> fix the issue the right way.  I'm unsure how to do this...
>>     Please advise.  TIA
>>
>> char* TextFunctions::removeCR(char *s) // Remove C/R from data string
>> {
>>      static char w[1024];
>>      int         n;
>>      char        *p;
>>
>>      strcpy (w, s);
>>      n = strlen(w);
>>      p = strrchr(w, char(10));
>
> Something (apart from the function's design) is wrong here -- ASCII CR
> (not "C/R") is decimal 13. LF is 10.
>
> And if you mean C++ '\n', say '\n' instead of hardcoding a numeric
> value.
>
And if he really means CR, then he should use '\r'

[toc] | [prev] | [next] | [standalone]


#15366 — Best Way to Handle Unknown Data Sizes? 

From@Jeff-Relf.Me
Date2012-04-13 16:04 -0700
SubjectBest Way to Handle Unknown Data Sizes? 
Message-ID<Jeff-Relf.Me@2012_Apr.13^4.04^PM>
In reply to#15359
<PRE>
char* Up⋅To⋅LF( char *B⁰ ) {
//  From (B⁰), return a string up to,
//  but not including, any LineFeed ( 10 ).
//
//  If possible, rewrite this using a temporary heap;
//  so the heap can be destroyed,
//  instead of calling free() and/or realloc().

  static int Sz⋅Buff = 1024 ;  static char *B¹ ;  char *LF;  int Len;

  if ( LF = strchr( B⁰, 10 ), !LF )  //  Do nothing.
    return B⁰ ;

  if ( Len = LF - B⁰ + 1, Len > Sz⋅Buff )
    //  Grow the buffer.
    B¹ = (char*)realloc( B¹, Sz⋅Buff = Len );

  else if ( !B¹ )
    //  Create the buffer.
    B¹ = (char*)malloc( Sz⋅Buff );

  sprintf( B¹, "%.*s", --Len, B⁰ );  return B¹;  }

[toc] | [prev] | [next] | [standalone]


#15369 — Best Way to Handle Unknown Data Sizes? 

From@Jeff-Relf.Me
Date2012-04-13 17:16 -0700
SubjectBest Way to Handle Unknown Data Sizes? 
Message-ID<Jeff-Relf.Me@2012_Apr.13^5.16^PM>
In reply to#15366
<PRE>
[ Reposting; this one uses free(), not realloc() ]

char* Up⋅To⋅LF( char *B⁰ ) {
//  From (B⁰), return a string up to,
//  but not including, any LineFeed ( 10 ).
//
//  If possible, rewrite this using a temporary heap;
//  so the heap can be destroyed,
//  instead of calling free() and/or realloc().

  static int Sz⋅Buff = 1024 ;  static char *B¹ ;  char *LF;  int Len;

  if ( LF = strchr( B⁰, 10 ), !LF )  //  Do nothing.
    return B⁰ ;

  if ( Len = LF - B⁰ + 1, Len > Sz⋅Buff )
    //  Free the old buffer, alloc a larger one.
    Sz⋅Buff = Len, free( B¹ ), B¹ = 0 ;

  if ( !B¹ )  //  Create the buffer.
    B¹ = (char*)malloc( Sz⋅Buff );

  sprintf( B¹, "%.*s", --Len, B⁰ );  return B¹;  }

[toc] | [prev] | [next] | [standalone]


#15370 — Re: Best Way to Handle Unknown Data Sizes? 

FromIan Collins <ian-news@hotmail.com>
Date2012-04-14 12:20 +1200
SubjectRe: Best Way to Handle Unknown Data Sizes? 
Message-ID<9uru6qFjbhU6@mid.individual.net>
In reply to#15369
On 04/14/12 12:16 PM, @Jeff-Relf.Me wrote:
> [ Reposting; this one uses free(), not realloc() ]
>
> char* Up⋅To⋅LF( char *B⁰ ) {
> //  From (B⁰), return a string up to,
> //  but not including, any LineFeed ( 10 ).
> //
> //  If possible, rewrite this using a temporary heap;
> //  so the heap can be destroyed,
> //  instead of calling free() and/or realloc().
>
>    static int Sz⋅Buff = 1024 ;  static char *B¹ ;  char *LF;  int Len;
>
>    if ( LF = strchr( B⁰, 10 ), !LF )  //  Do nothing.
>      return B⁰ ;
>
>    if ( Len = LF - B⁰ + 1, Len > Sz⋅Buff )
>      //  Free the old buffer, alloc a larger one.
>      Sz⋅Buff = Len, free( B¹ ), B¹ = 0 ;
>
>    if ( !B¹ )  //  Create the buffer.
>      B¹ = (char*)malloc( Sz⋅Buff );
>
>    sprintf( B¹, "%.*s", --Len, B⁰ );  return B¹;  }

What is this nonsense?

-- 
Ian Collins

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web