LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-08-2002, 09:00 AM   #1
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Getting a variable name based on a string.


I am writting a communications module that takes in two types of commands, "put variablename value" and "get variablename". The commands come in as text in an eithernet packet. What I want to be able to do is get a pointer to variablename using the string from the comm module so I don't have to have a case statement with hundreds of cases. Anyone have any ideas?

John

OH ya... this is C code running on GCC 2.95.3 under Slackware...

Last edited by jtshaw; 10-08-2002 at 09:01 AM.
 
Old 10-08-2002, 10:19 AM   #2
bsdjunkie
Member
 
Registered: Sep 2002
Distribution: slack
Posts: 39

Rep: Reputation: 15
I would use libdnet or libpcap instead of trying to reinvent the wheel in packet capture
 
Old 10-08-2002, 10:24 AM   #3
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
No easy way to do this in C. You need to associate strings with pointers to the variables whose values they represent. The simplest way of doing this is to have an array:

typedef struct string_var_assoc
{
char *name;
void *value_pointer;
} string_var_assoc_t;

string_var_assoc_t var_array[] = {
{"foo", &foo},
{"bar", &bar},
{NULL, NULL}, /* Null terminator */
};

Now you can write a function to iterate through the array and set the appropriate value, if some of the variables' values have different types you might want to store the type of the value in string_var_assoc_t, so you can cast the (void *) pointer to the correct type.

The typing out of the array can be made a bit easier with a macro:

#define ASSOC(x) {#x, &x}

ASSOC(x) now expands to {"x", &x}

Hope this hgelps a little.

Alex
 
Old 10-08-2002, 10:52 AM   #4
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892

Original Poster
Blog Entries: 1

Rep: Reputation: 67
Ya, thanks. That is what I figured, I think so it is somewhat easy to figure out for the next poor sap that has to deal with this code I will just do it the long way with a case statement, at least the variables names will be descriptive.
 
Old 10-08-2002, 11:09 AM   #5
leed_25
Member
 
Registered: Jul 2002
Location: san francisco
Distribution: freebsd
Posts: 102

Rep: Reputation: 17
I can't help but thing that I am missing something here. This seems too simple: why not use getenv() and putenv() ?
 
Old 10-08-2002, 11:35 AM   #6
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
1) Programs shouldn't use the external environment for internal storage
2) You could only store ASCII strings, and you'd have to keep converting them

Still, it's a neat idea, might work OK for some things.

Alex
 
Old 10-08-2002, 11:47 AM   #7
leed_25
Member
 
Registered: Jul 2002
Location: san francisco
Distribution: freebsd
Posts: 102

Rep: Reputation: 17
Ok, well how about some kind of hashing mechanism, then? It would be fast. 'a switch statement with hundreds of cases..' that just sounds so... so ICKY.
 
Old 10-08-2002, 02:06 PM   #8
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
Yeah hashing is the fastest way - my example was supposed to show the easiest way. But like jtshaw said, the switch statement is the easiest to understand.

Alex
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Print a part of variable/string p0tw0r Linux - Newbie 1 04-07-2005 02:49 PM
Perl variable string search ugenn Programming 1 05-07-2004 08:19 PM
need to innitialize string variable with multiple spaces clsonnt Programming 3 08-11-2003 10:40 AM
variable to string x2000koh Programming 4 07-30-2003 02:23 AM
Assigning a string to a variable (not a pointer, not a array) JStew Programming 3 11-18-2002 08:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration