Server Help

Bot Questions - Buffer Issues :/

Dark_Nexus - Wed Apr 07, 2004 12:41 am
Post subject: Buffer Issues :/
I am using a windows function to access reg keys and .ini files, here is the function with its syntax:

GetPrivateProfileSection(section name,buffer name,size of buffer,path to file)

so lets say i have an .ini file with one section and use that function to retrieve the data from it:

[Operators]
Dark_Nexus=3
blehbleh=4
bleep=2

the way it returns the section is, each data key gets put into the buffer as a string, it is followed by a NULL terminating character, and the last string of the buffer has two NULL terminating characters. So it looks like this in the buffer:

Dark_Nexus=3 \n blehbleh=4 \n bleep=2 \n\n

So now lets say i want to assign a variable to the buffer, as in:

String msg = buffer;

now String msg will only equal the first string, as in Dark_Nexus=3, because it encounters the first NULL terminating character and thinks it is a string.

So i need a way to sort through the buffer, and place the data i need into a String variable.

here is what i have tried (and does not work):



Code: Show/Hide
msg = "Operators: ";
GetPrivateProfileSection("Operators",buffer,250,path);
String opstr = buffer;
GetPrivateProfileString("Operators","ASOCount",0,question_num,3,path);
for (int i = 1;i <= atoi(question_num);i++)
{
    msg += opstr.split('\n');
    msg += ", ";
}
sendPrivate(p,msg);


so lets say i have 3 moderators, it will look like this in game:

Bot> Operators: Dark_Nexus=3, Dark_Nexus=3, Dark_Nexus=3

icon_sad.gif helppppp
Mr Ekted - Wed Apr 07, 2004 1:22 am
Post subject:
Off the top of my head...

Code: Show/Hide
char *p;

p = buffer;  // loaded with results of function call

while (*p)
   {
   // set your string = p and do what you like
   p += strlen(p) + 1;
   }


This shows C's strength: the ability to handle memory in an arbitrary format.
Mine GO BOOM - Wed Apr 07, 2004 10:28 am
Post subject:
Mr Ekted wrote:
This shows C's strength: the ability to handle memory in an arbitrary format.

Would you come up with a source to show in anyway how C++ is a subset of C?
Mr Ekted - Wed Apr 07, 2004 11:41 am
Post subject:
C++ is a superset of C. I'm not sure what you are asking.
D1st0rt - Wed Apr 07, 2004 11:50 am
Post subject:
Heres a solution, thanks to Dustpuppy:

It's golden icon_cool.gif
50% Packetloss - Wed Apr 07, 2004 12:36 pm
Post subject:
char *INIAccess::GetString(char *section, char *key, char *strdefault)

if you run this function, make sure you delete [] what it returns, so that you dont get a memory leak
Mr Ekted - Wed Apr 07, 2004 1:22 pm
Post subject:
This is one of my main dislikes of C++ (if you go overboard). It makes the coding at the top level look all nice and simple, when in fact, there's 10x more going on under the hood. More code executing (slower + larger), and unnecessary memory allocations/frees. IMO, C++ is best used to encapsulate large functional blocks. Of course, you can do this in C just as well.
50% Packetloss - Wed Apr 07, 2004 1:50 pm
Post subject: Re: Buffer Issues :/
Dark_Nexus wrote:

the way it returns the section is, each data key gets put into the buffer as a string, it is followed by a NULL terminating character, and the last string of the buffer has two NULL terminating characters. So it looks like this in the buffer:

Dark_Nexus=3 \n blehbleh=4 \n bleep=2 \n\n


rofl, a null looks like \0
the \n is a newline character.

I dont think that function will work anyway.
Dustpuppy - Wed Apr 07, 2004 3:47 pm
Post subject:
That class doesn't provide for reading an entire section, which is what Dark Nexus wanted.
To be honest, INIs aren't meant for this sort of data and you would be better off reading from a standard text file, one operator to a line.

If you really wanted to keep it in an ini, perhaps you could use:

[Operators]
Sysop=Foo:Bar
Smod=Blah:bleh

etc

If you wanted to keep your current format, I think Ekted's code would be best. But if you wanted to use a MERV String, you could try using a strncpy:
I believe GetPrivateProfileSection returns the length of the string it copied into buffer, so you could store the length it returned, then strncpy into your String.msg, and use split on the newlines.

I can't remember if the String object lets you access the msg property though.
Cyan~Fire - Wed Apr 07, 2004 5:59 pm
Post subject:
Just get lines from the file and process them one at a time.

Some crap code using iostreams (please don't yell Ekted):
Code: Show/Hide
ifstream op_file;
char buffer[100], ops[20][12], levels[20];
for (int i = 0; i < 20; i++)
{
   if (op_file.eof())
      break;
   op_file.getline(buffer, 100);
   int loc = find(bufffer, '=');   //some kind of character finding func
   strncpy(ops[i], buffer, loc);
   levels[i] = atoi(buffer + loc + 1);
}

Hope this helps... and that I didn't make too many mistakes!
Dark_Nexus - Wed Apr 07, 2004 8:37 pm
Post subject:
GetPrivateProfileSection is a standard windows function...i would hope it was microsoft tested and approved tongue.gif
Mr Ekted - Wed Apr 07, 2004 10:39 pm
Post subject:
Dark_Nexus wrote:
GetPrivateProfileSection is a standard windows function...i would hope it was microsoft tested and approved tongue.gif


Agreed. Don't roll your own with INI files.
Cyan~Fire - Wed Apr 07, 2004 10:52 pm
Post subject:
Except for the fact that file file you're writing is not really an INI.
Dustpuppy - Thu Apr 08, 2004 7:56 pm
Post subject:
It is an INI but it shouldn't be. I already went over this...
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group