buffers, streams, confusion

3

31337one

Hello all,

Let me introduce myself. I would say that I am an intermediate
programmer in C++. I am studying comptuer science in college. However,
I have never had a formal introduction to buffers. Here is my problem:

I have a program that can have a config file and also command line
arguments.

When it executes I want to grab the command line arguements and stuff
them into a buffer (i think). Then i want to read everything from the
config file which is basically command line flags that are set and
stuff them into the buffer also.

The problem I am seeing is a "buffer" is just a C-style string. I dont
see how to malloc() the right amount of memory. It is easy if its just
the command line arguments because you can do an array and sizeof()
each argument.

Ultimately I would like to treat the "buffer" as a list and ask for
certain parameters.

Maybe a std::map is what I should use.

Here is an example:

the config file has:
---------------------
| -user joe
| -pwd football
|____________


run the program with additional cmd line args

C:\ prog -server 127.0.0.1 -port 999

the server, port, user, and pwd need to be in the "buffer". This is
why a map might work but could be overkill.

Thanks for any help,

31337one
 
M

Marcus Kwok

I have a program that can have a config file and also command line
arguments.

When it executes I want to grab the command line arguements and stuff
them into a buffer (i think). Then i want to read everything from the
config file which is basically command line flags that are set and
stuff them into the buffer also.

The problem I am seeing is a "buffer" is just a C-style string. I dont
see how to malloc() the right amount of memory. It is easy if its just
the command line arguments because you can do an array and sizeof()
each argument.

Ultimately I would like to treat the "buffer" as a list and ask for
certain parameters.

A "buffer" doesn't necessarily have to be a C-style string. I take
"buffer" to mean basically just a temporary storage area where you can
hold things.

One idea would be to use a std::vector<std::string>. If you went this
route, then you do not have to worry about how much memory to allocate
since these containers (std::string can be thought of as a container of
characters) handle that for you.

Also note that in C++, you usually allocate memory with new or new[]
instead of malloc(), though in certain situations you may really need to
use malloc().
 
T

Thomas J. Gritzan

Hello all,

Let me introduce myself. I would say that I am an intermediate
programmer in C++. I am studying comptuer science in college. However,
I have never had a formal introduction to buffers. Here is my problem:

I have a program that can have a config file and also command line
arguments.

When it executes I want to grab the command line arguements and stuff
them into a buffer (i think). Then i want to read everything from the
config file which is basically command line flags that are set and
stuff them into the buffer also.

When command line arguments are allowed to override config file entries,
you would have to read the config file first.
The problem I am seeing is a "buffer" is just a C-style string. I dont
see how to malloc() the right amount of memory. It is easy if its just
the command line arguments because you can do an array and sizeof()
each argument.

sizeof() does not work on c-style strings. strlen() is for this.
Since you are writing C++, you should use new[] instead of malloc().

Well, better don't use c-style strings and plain pointers directly. Use
std::string for your "buffer", then you don't have to think about its
length. The string class handles the dirty details.
Ultimately I would like to treat the "buffer" as a list and ask for
certain parameters.

Maybe a std::map is what I should use.

std::map is exactly what you want. Learn more about the C++ standard
library, it can do a lot of things for you.

Thomas
 
3

31337one

I do like the map idea and will probably try to use that. I have used
maps, vectors, and such. I am just brainstorming on how to encapsulate
my command line arguments and .conf file and have them in one place.

Another problem I am running into now with the map idea is some flags
for my program take more than one argument.

Example:

prog -server 127.0.0.1 -port 999 -search xyz wtf bbq

Initially I thought, well just use a map with strings as keys and
strings as values.

However, now there are multiple values for the search and they need to
be treated differently. Would it be smart to just pack the "xyz" "wtf"
"bbq" into one string "xyz wtf bbq" and go with my original idea? I can
use a blank space as a separator.

If i do just concatenate all of the strings like this into one, is
there any easy way to split them up later?

Thank you,

31337one
 
J

Jim Langston

I do like the map idea and will probably try to use that. I have used
maps, vectors, and such. I am just brainstorming on how to encapsulate
my command line arguments and .conf file and have them in one place.

Another problem I am running into now with the map idea is some flags
for my program take more than one argument.

Example:

prog -server 127.0.0.1 -port 999 -search xyz wtf bbq

Initially I thought, well just use a map with strings as keys and
strings as values.

However, now there are multiple values for the search and they need to
be treated differently. Would it be smart to just pack the "xyz" "wtf"
"bbq" into one string "xyz wtf bbq" and go with my original idea? I can
use a blank space as a separator.

If i do just concatenate all of the strings like this into one, is
there any easy way to split them up later?

Thank you,

31337one

Well, if you used a vector you could build 3 entries
-search xyz
-search wtf
-search bbq

a map wouldn't allow this because the key needs to be unique.
 
J

Jerry Coffin

[ ... ]
prog -server 127.0.0.1 -port 999 -search xyz wtf bbq

Initially I thought, well just use a map with strings as keys and
strings as values.

However, now there are multiple values for the search and they need to
be treated differently. Would it be smart to just pack the "xyz" "wtf"
"bbq" into one string "xyz wtf bbq" and go with my original idea? I can
use a blank space as a separator.

You can. Alternatively, you can use a multimap instead of
a map. In this case, you'll probably want to use
equal_range to find your values.
If i do just concatenate all of the strings like this into one, is
there any easy way to split them up later?

One easy way is to create a stringstream, and then read
the individual values from it -- but a multimap will
usually be cleaner.
 
3

31337one

Well thank you all for your support and ideas.

What I was intending to do in the begining was have the map act as a
holder for the different flags on the command line and in a config
file. Then when I actually create the class that would act on the
flags, it would just ask the map for the values (username,
password....).


Instead, I nixed the whole map idea and just updated the class at parse
time.


I didnt want to do this because I thought it would be messier than the
map but the map just turned up to be twice as much code.

Thanks you all again,

31337one
 

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

Similar Threads

Output confusion 2
fstream Buffers 26
Return confusion 4
Buffers 5
flushing streams 3
buffers and memoryviews 0
Command Line Arguments 0
Need to use streams/buffers, but NOT related to cout, cin or to files 6

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top