Combining Case functions....

i have a app that writes to an xml file using command line switches, what i would like to do is have several of the cases combine into one so i can use switch -v xxxxxx and each x (either 1 or 0) is a different line below is a better example of what i am trying to accomplish

1
2
3
4
5
6
fprintf(fp_outfile,"                    <Option Name=\"FileRead\">%s</Option>\n", s_fileread);
fprintf(fp_outfile,"                    <Option Name=\"FileWrite\">%s</Option>\n", s_filewrite);
fprintf(fp_outfile,"                    <Option Name=\"FileDelete\">%s</Option>\n", s_filedelete);
fprintf(fp_outfile,"                    <Option Name=\"FileAppend\">%s</Option>\n", s_fileappend);
fprintf(fp_outfile,"                    <Option Name=\"DirCreate\">%s</Option>\n", s_dircreate);
fprintf(fp_outfile,"                    <Option Name=\"DirDelete\">%s</Option>\n", s_dirdelete);



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
case 'r': case 'R': i= i + 1;
					strcpy(s_fileread, argv[i]);
					i= i + 1;
					break;
                case 'w': case 'W': i= i + 1;
					strcpy(s_filewrite, argv[i]);
					i= i + 1;
					break;
                case 'l': case 'L': i= i + 1;
					strcpy(s_filedelete, argv[i]);
					i= i + 1;
					break;
                case 'a': case 'A': i= i + 1;
					strcpy(s_fileappend, argv[i]);
					i= i + 1;
					break;
                case 'c': case 'C': i= i + 1;
					strcpy(s_dircreate, argv[i]);
					i= i + 1;
					break;
                case 'e': case 'E': i= i + 1;
					strcpy(s_dirdelete, argv[i]);
					i= i + 1;
					break;


curently this is working as i like it to .. but i would like to combine the above cases into a single case how would i go about doing that ?
Last edited on
i would like to combine the above cases into a single case how would i go about doing that ?

Something like this ?:

1
2
3
4
5
6
7
8
#include <cctype>


switch(tolower(ch)) {
    case 'r' : /**/
    case 'w' : /**/
    ...
}
i mean take all the cases and put them into 1 case (ie -v011010)
with that example it would write to the file

<Option Name=FileRead>0</Option>
<Option Name=FileWrite>1</Option>
<Option Name=FileDelete>1</Option>
<Option Name=FileAppend>0</Option>
<Option Name=DirCreate>1</Option>
<Option Name=DirDelete>0</Option>
I'm not sure exactly if this is what you mean, but this is the closest thing I can come up with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
char* target = 0;

switch(...)
{
case 'r': case 'R':  target = s_fileread;       break;
case 'w': case 'W':  target = s_filewrite;      break;
case 'l': case 'L':  target = s_filedelete;     break;
...
}

if(target)
{
    strcpy(target,argv[i+1]);
    i += 2;
}
else
{
    // one of the above options wasn't selected
}
i may be confusing both of us LOL, lets try this ..

i want to get rid of all those cases and only have 1 case
where when the exe is called with -v 000001

it will break apart the 000001 (six slots) to equal this

slot 1 = s_fileread
slot 2 = s_filewrite
... ect

and do the function as described...

does this make more sense ?

i will try your code, but i have to be honest im a c++ newb and not sure what to replace switch(...) with and in the else statment not sure if i need that as if the -v is not used it just continues. there are other cases that are required, would it help if i posted the code in its entirety?
here is the full code... theres too much to post if you have an email addy i can send you the code and you can see better what i am trying to do.
Don't send me code by email. If it's too big to post here then it's probably too big for me to read. I don't really want to sift through pages and pages of code.

I learn by example. So why don't you give me an example problem and what you want the desired result to be, and then it'll be able to help more.

Your previous example was kind of unclear. From what it looks like... if the user inputs this: myprogram -v 000001 you want:

s_fileread == 0
s_filewrite == 0
...
s_dirdelete == 1



But that doesn't really make any sense because these are strings.. so I'm still a little confused.

ok what the program does is creates user accounts for filezilla servers,

the use is {now}...
 
fzadd -u test -p mypass -d c:\mydir\  -r 1 -w 0 -l 1 -a 0 -c 1 -e 0


what i want to do instead is ...

 
fzadd -u test -p mypass -d c:\mydir\  -v 101010


hope this clears it up.


Last edited on
So these are 1 digit arguments? Why are you using strings, then... wouldn't it make more sense to use an int or char or something?

anyway... you can just take individual characters out of the "101010" argument and assign them to the appropriate variable:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// note this code assumes fileread, filewrite, etc are chars, not strings
//  as in they hold a single character

// argv[i] is the commandline argument that contains the characters you want to break up:
//   ie:  "101010"

if(strlen(argv[i]) >= 6)  // make sure it has enough digits
{
  fileread = argv[i][0];  // take the first digit
  filewrite = argv[i][1]; // then the next
  //...                       // and so on
  dirdelete = argv[i][5];  // until the last
}
else
{
  // user didn't input at least 6 digits, give them an error or something
}

Topic archived. No new replies allowed.