473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

C-style string parsing

I have a C-style string (null-terminated) that consists of items in one of the
following formats:
14 characters
5 characters space 8 characters
6 characters colon 8 characters
5 characters colon 8 characters

Items are delimited by semicolons or commas. I have to produce a string
delimited only by semicolons and containing items in the first two formats
only. For example,

"AAAAAAAAAAAAAA,AAAAAA:AAAAAAAA;AAAAA AAAAAAAA,AAAAA:AAAAAAAA" ->
"AAAAAAAAAAAAAA;AAAAAAAAAAAAAA;AAAAA AAAAAAAA;AAAAA AAAAAAAA"

Posting to comp.lang.c yielded the following:

int myfunc( const char *idlist )
{
int items=0;
char *newstr=(char *)malloc( strlen(idlist)+1 );
if( !newstr ) {
return( -2 );
}
int srcidx=0;
int destidx=0;
int chars=0;

for( ; idlist[srcidx] ; srcidx++ ) {
if( idlist[srcidx] == ':' ) {
if( chars == 5 ) {
newstr[destidx++]=' ';
chars++;
}
else if( chars != 6 )
return( -1 ); // Invalid format
}
}
else if( idlist[srcidx] == ';' || idlist[srcidx] == ',' ) {
if( chars != 14 ) { // Invalid format
return( -1 );
}
newstr[destidx++]=';';
chars=0;
items++;
}
else if( ++chars > 14 ) {
return( -1 );
}
else {
newstr[destidx++]=idlist[srcidx];
}
}
newstr[destidx]='\0';
if( chars == 14 ) {
items++;
}
else if( !items || chars ) { // items == 0 || chars != 0
return( -1 );
}
printf( "The string '%s' has %d items.\n", newstr, items );
/* Call a function using newstr here */
free( newstr );
return( 0 );
}

I'd like to know how to improve this function (specifically, the call to
malloc()) to make it more like typical C++. One thing: Don't tell me to use
std::string's, because it isn't an option (the C++ code at my company uses
C-style strings almost exclusively).

--
Christopher Benson-Manica | Upon the wheel thy fate doth turn,
ataru(at)cyberspace.org | upon the rack thy lesson learn.
Jul 19 '05 #1
6 8302
Hello.
return( -1 ); // Invalid format
You can do:

const int INVALID_FORMAT= -1;

And then

return INVALID_FOMAT;

Is auto-commented.
else if( !items || chars ) { // items == 0 || chars != 0
Why comment what you intend to do instead of doing it?

else if (items == 0 || chars != 0) {
I'd like to know how to improve this function (specifically, the call to
malloc()) to make it more like typical C++. One thing: Don't tell me to use
Use new / delete instead of malloc / free.
std::string's, because it isn't an option (the C++ code at my company uses
C-style strings almost exclusively).


You can be one of the exceptions ;)

Regards.
Jul 19 '05 #2
Julián Albo <JU********@terra.es> spoke thus:
const int INVALID_FORMAT= -1; And then return INVALID_FOMAT;
Well, the actual function uses an enumerated error code - I left it out for
clarity.
else if( !items || chars ) { // items == 0 || chars != 0

Why comment what you intend to do instead of doing it? else if (items == 0 || chars != 0) {
Because I want my code to be l337? ;)
You can be one of the exceptions ;)


I think they have error handling code for exceptions like me ;)

--
Christopher Benson-Manica | Upon the wheel thy fate doth turn,
ataru(at)cyberspace.org | upon the rack thy lesson learn.
Jul 19 '05 #3
Christopher Benson-Manica wrote:
I have a C-style string (null-terminated) that consists of items in one of
the following formats:
14 characters
5 characters space 8 characters
6 characters colon 8 characters
5 characters colon 8 characters

Items are delimited by semicolons or commas. I have to produce a string
delimited only by semicolons and containing items in the first two formats
only. For example,

"AAAAAAAAAAAAAA,AAAAAA:AAAAAAAA;AAAAA AAAAAAAA,AAAAA:AAAAAAAA" ->
"AAAAAAAAAAAAAA;AAAAAAAAAAAAAA;AAAAA AAAAAAAA;AAAAA AAAAAAAA"

Posting to comp.lang.c yielded the following:

int myfunc( const char *idlist )
{
int items=0;
char *newstr=(char *)malloc( strlen(idlist)+1 );
if( !newstr ) {
return( -2 );
}
int srcidx=0;
int destidx=0;
int chars=0;

for( ; idlist[srcidx] ; srcidx++ ) {
if( idlist[srcidx] == ':' ) {
if( chars == 5 ) {
newstr[destidx++]=' ';
chars++;
}
else if( chars != 6 )
return( -1 ); // Invalid format
}
}
else if( idlist[srcidx] == ';' || idlist[srcidx] == ',' ) {
if( chars != 14 ) { // Invalid format
return( -1 );
}
newstr[destidx++]=';';
chars=0;
items++;
}
else if( ++chars > 14 ) {
return( -1 );
}
else {
newstr[destidx++]=idlist[srcidx];
}
}
newstr[destidx]='\0';
if( chars == 14 ) {
items++;
}
else if( !items || chars ) { // items == 0 || chars != 0
return( -1 );
}
printf( "The string '%s' has %d items.\n", newstr, items );
/* Call a function using newstr here */
free( newstr );
return( 0 );
}

I'd like to know how to improve this function (specifically, the call to
malloc()) to make it more like typical C++. One thing: Don't tell me to
use std::string's, because it isn't an option (the C++ code at my company
uses C-style strings almost exclusively).


Don't be to set against std::string. If you need to write code that will be
used by other people in you company, and they insist on using c-style
strings, then simply make appropriate use of std::string::c_str(). Just
because other people you work with want to make things hard on themselves
doesn't mean that you have to.

Sean
Jul 19 '05 #4
Christopher Benson-Manica escribió:
else if( !items || chars ) { // items == 0 || chars != 0

Why comment what you intend to do instead of doing it?

else if (items == 0 || chars != 0) {


Because I want my code to be l337? ;)


Doing things that the compiler can do for you is being l337? }:)

Regards.
Jul 19 '05 #5
Sean Fraley <sf*****@rhiannonweb.com> spoke thus:
Don't be to set against std::string. If you need to write code that will be
used by other people in you company, and they insist on using c-style
strings, then simply make appropriate use of std::string::c_str(). Just
because other people you work with want to make things hard on themselves
doesn't mean that you have to.


Well, it doesn't seem to be too useful to create a std::string just for
parsing purposes and then convert back to a c_str... (un?)fortunately, the de
facto paradigm here is still C anyway. Not that *I'm* necessarily sad about
that (I *like* C!). The real problem comes from the fact that all the code
uses custom classes and template classes as substitutes for the STL...

--
Christopher Benson-Manica | Upon the wheel thy fate doth turn,
ataru(at)cyberspace.org | upon the rack thy lesson learn.
Jul 19 '05 #6
Christopher Benson-Manica wrote:
Well, it doesn't seem to be too useful to create a std::string just for
parsing purposes and then convert back to a c_str... (un?)fortunately, the de facto paradigm here is still C anyway. Not that *I'm* necessarily sad about that (I *like* C!). The real problem comes from the fact that all the code uses custom classes and template classes as substitutes for the STL...


Y'all are probably using C-style C++. Unless your C code actually so sloppy
that C++ can't compile it.

Follow this simple regimen:

- use std::string, and any other highest-level C++ thing, at whim

- have less bugs and tighter code than your colleagues

- count said bugs.

Here's Bjarne's "Don't use new[] like malloc()" interview:

http://www.artima.com/intv/goldilocksP.html

--
Phlip
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael Hogan | last post by:
I want to pars a playlist file for three different varibles, so I can save them as mp3 files. I am using: strTEMPURL = GetUrlSource(Text1.Text) to put the entire .pls file into a strTEMPURL...
8
by: Bob Davis | last post by:
In C# one can construct a string using the format method. for example string newstring = string.Format("CC%d-%s.%d-%s-% d",123,"aaaa",456,"bbb",789); The resulting newstring will be...
7
by: sbowman | last post by:
I have a completely lame string parsing question, but I need an answer fast and I know this is where to get it...I'm not completely familiar with the Len, Right, Left, and mid functions and I have...
4
by: igotyourdotnet | last post by:
I have a question. I'm reading a CSV file that is uploading to my SQL db, I'm parsing out the file line by line. I'm getting the values and putting them into an arrayList seperate by commas. The...
4
by: Gary Wessle | last post by:
Hi how can I do this in C++ string myword; string get_word_from_this_url( url ){ bool flag = true; while flag; download this url and search for this word; if found; set flag to false;
12
by: HMS Surprise | last post by:
The string below is a piece of a longer string of about 20000 characters returned from a web page. I need to isolate the number at the end of the line containing 'LastUpdated'. I can find...
7
by: cephal0n | last post by:
Hi there! I've been racking my brains out for 4 days and still not getting why is msaccess stubborn about string parsing, first I have a table named tblItems, this table has columns: ProductItem,...
28
by: pereges | last post by:
Hi I've a string input and I have to parse it in such a way that that there can be only white space till a digit is reached and once a digit is reached, there can be only digits or white space till...
9
balabaster
by: balabaster | last post by:
I'm looking for some ideas regarding string parsing and brackets. Say I have the following string: 56*(73+23/(28+(7/14)-(3/2)) What would be the best way to parse the string for each opening...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.