Removing all Whitespaces with preprocessor

F

Franz Müller

Hi there,

I'm searching for a whitespace-free macro that does the following:

STRING_LIST(a, b,c ,d) => "a,b,c,d" (No blanks/whitespaces)

The following macro:

#define STRING_LIST(args...) #args

outputs: "a, b,c ,d" which causes some problems with the functions that
handle this string.

I already had a look on recusion, but this doesn't really work, because an
iterative macro-replacement is not executed by the preprocessor.

Any Ideas where or how to start?

Best regards,

Franz
 
E

Ed Prochak

Hi there,

I'm searching for a whitespace-free macro that does the following:

STRING_LIST(a, b,c ,d) => "a,b,c,d" (No blanks/whitespaces)

The following macro:

#define STRING_LIST(args...) #args

outputs: "a, b,c ,d" which causes some problems with the functions that
handle this string.

I already had a look on recusion, but this doesn't really work, because an
iterative macro-replacement is not executed by the preprocessor.

Any Ideas where or how to start?

Best regards,

Franz

Fix the function.
 
F

Franz Müller

Ed said:
Fix the function.

Well yes - I would if I could - it's an old algorithm library that is only
available in binary code - no sourcecode available - sorry.

Best regards,

Franz
 
N

Nick Keighley

Hi there,

I'm searching for a whitespace-free macro that does the following:

STRING_LIST(a, b,c ,d) => "a,b,c,d" (No blanks/whitespaces)

The following macro:

#define STRING_LIST(args...) #args

outputs: "a, b,c ,d" which causes some problems with the functions that
handle this string.

I already had a look on recusion, but this doesn't really work, because an
iterative macro-replacement is not executed by the preprocessor.

Any Ideas where or how to start?

write a function to strip spaces?

nasty_old_function (strip_whitespace (STRING_LIST(a, b,c d)));

you need to work out where strip_whitespace() is putting its
return value
 
S

Stefan Ram

Franz Müller said:
STRING_LIST(a, b,c ,d) => "a,b,c,d" (No blanks/whitespaces)

You wrote »it's an old algorithm library that is only
available in binary code - no sourcecode available«.

But the preprocessor only works with source code,
not with binary code. So, »STRING_LIST(a, b,c ,d)«
must be source code. Then, why can't you change this
to »STRING_LIST(a,b,c,d)«?

Or, you could try to use a more general preprocessor,
such as GPP.

http://en.nothingisreal.com/wiki/GPP
http://www-math.mit.edu/~auroux/software/gpp.html

Or write a custom preprocessor with M4, awk, sed,
perl or so.
 
L

ld

Hi there,

I'm searching for a whitespace-free macro that does the following:

STRING_LIST(a, b,c ,d) => "a,b,c,d" (No blanks/whitespaces)

The following macro:

#define STRING_LIST(args...) #args

outputs: "a, b,c ,d" which causes some problems with the functions that
handle this string.

I already had a look on recusion, but this doesn't really work, because an
iterative macro-replacement is not executed by the preprocessor.

Any Ideas where or how to start?

AFAIK, what you ask for is not trivial. Using the CPP library of the C
Object System, you can do

#include <cos/cpp/cpp.h>

#define STRING_LIST(...) \
COS_PP_STR(COS_PP_SEQ(COS_PP_MAP((__VA_ARGS__),COS_PP_ID)))

STRING_LIST(a, b, c , d)

// results: "a,b,c,d"

This code basically maps (COS_PP_MAP) the identity function
(COS_PP_ID) on each element of the tuple (__VA_ARGS__) and thus remove
the spaces around each argument, then converts it into a sequence,
that is drop the parenthesis (COS_PP_SEQ) and converts the result into
a string (COS_PP_STR). Obviously, if you know the number of arguments,
it is much simpler.

The COS-CPP lib is a standalone lib which can be found there

http://cos.cvs.sourceforge.net/viewvc/cos/CosBase/include/cos/cpp/

I have just added the cpp.h to simplify the header dependencies.

a+, ld.
 
F

Franz Müller

Hi ld,
AFAIK, what you ask for is not trivial. Using the CPP library of the C
Object System, you can do

#include <cos/cpp/cpp.h>

#define STRING_LIST(...) \
COS_PP_STR(COS_PP_SEQ(COS_PP_MAP((__VA_ARGS__),COS_PP_ID)))

STRING_LIST(a, b, c , d)

// results: "a,b,c,d"

This code basically maps (COS_PP_MAP) the identity function
(COS_PP_ID) on each element of the tuple (__VA_ARGS__) and thus remove
the spaces around each argument, then converts it into a sequence,
that is drop the parenthesis (COS_PP_SEQ) and converts the result into
a string (COS_PP_STR). Obviously, if you know the number of arguments,
it is much simpler.

The COS-CPP lib is a standalone lib which can be found there

http://cos.cvs.sourceforge.net/viewvc/cos/CosBase/include/cos/cpp/

I have just added the cpp.h to simplify the header dependencies.

a+, ld.

Sounds cool - I will test it!

Best regards,

Franz
 
L

luserXtrog

Hi there,

I'm searching for a whitespace-free macro that does the following:

STRING_LIST(a, b,c ,d) => "a,b,c,d" (No blanks/whitespaces)

The following macro:

#define STRING_LIST(args...) #args

outputs: "a, b,c ,d" which causes some problems with the functions that
handle this string.

I already had a look on recusion, but this doesn't really work, because an
iterative macro-replacement is not executed by the preprocessor.

Any Ideas where or how to start?


You can convert them individually and let the compiler concatenate.

#define STRING_LIST(a,b,c,d) #a "," #b "," #c "," #d
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top