AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   read_file again... (https://forums.alliedmods.net/showthread.php?t=48563)

Br41n 12-14-2006 15:51

read_file again...
 
Hi,
i'm working on a plugin like autobind, but the difference is, that the player can decide, if the player wants to bind the keys automatically or not..
For it i want to read a config.ini file with the key and the command for the key in it.. The file should look like this:

Code:

; Syntax: "<key>" "<command>"
"o" "say hello"

I tried to realize that part, but it doesn't work...
My Code:

PHP Code:

public autobind(idkeyparsedkeysparsedcommand)
{
    
//key will start at zero
    
if (key == 0//if player pressed Yes
    
{
        new 
readdata[128],parsedkeys[32],parsedcommand[32],txtlen
    
new line 0
    
while(!read_file(filename,line++,readdata,127,txtlen))
    {
    
parse(readdata,parsedkeys,32,parsedcommand,32)
    }   
        
client_cmd(id"say test")
    
client_print(idprint_chat"KEY: ^"%s^" COMMAND: ^"%s^" "parsedkeysparsedcommand)


    } else if (
key == 1) { // if player pressed No
         
client_print(idprint_chat"OK, no binding was changed. Type /help to see a list of available say commands.")
    }


I just tried a client_print to check, that read_file part works.. I want, when a player pressed 1 (Yes), the client_print tells this: "KEY: o COMMAND: say hello".. It's he first time, that I use read_file, so could anyone explain how to realize this?

Thanks
Br41n

[ --<-@ ] Black Rose 12-14-2006 15:55

Re: read_file again...
 
Use the new file natives.
EDIT: Something like this..?
Code:
public autobind(id, key) {         switch ( key ) { // switches are faster than if, else if.         case 0: {             new readdata[80], parsedkeys[32], parsedcommand[32]                         new fileh = fopen(filename, "r") // open file.                         if ( ! fileh ) // if no file, return.                 return                         while ( ! feof(fileh) ) { // while not in end of file, continue to read.                 fgets(fileh, readdata, 79) // gets line text.                 strbreak(readdata, parsedkeys, 31, parsedcommand, 31) // strbreak is better than parse, also, 31, not 32.                 client_print(id, print_chat, "KEY: ^"%s^" COMMAND: ^"%s^" ", parsedkeys, parsedcommand) // maby you wanted to print this on EVERY command(?)             }             fclose(fileh) // never forget to close the file         }                 case 1: client_print(id, print_chat, "OK, no binding was changed. Type /help to see a list of available say commands.")     } }

Br41n 12-14-2006 17:04

Re: read_file again...
 
Yeah.. thank you, i will try it and will argue with it..

EDIT: One Question: What does the 31 in strbreak() mean?

[ --<-@ ] Black Rose 12-14-2006 20:22

Re: read_file again...
 
Quote:

Originally Posted by schnitzelmaker (Post 415481)
strbreak is the same as parse,but only for 2 strings and you must know the lenght of the strings.

Incorrect.
Quote:

Originally Posted by schnitzelmaker (Post 415481)
Spaces are NOT break it

Wrong again.
Quote:

Originally Posted by schnitzelmaker (Post 415481)
strbreak always break at the position who is given.

Guess what, wrong.
Quote:

Originally Posted by schnitzelmaker (Post 415481)
"feof" have a little problem inside,while using it you read the last line double times

Nope.
Quote:

Originally Posted by schnitzelmaker (Post 415481)
better use
Code:
while (fgets(fileh, readdata, 79) ) {

Nawh.
Quote:

Originally Posted by schnitzelmaker (Post 415481)
fgets return 0 if it cant read the line.

No, it returns strlen. If there was a line with no text your code would stop reading the file.

Wanna try again?

Summary: STFU! You have no idea what you're talking bout.


Quote:

Originally Posted by Br41n (Post 415413)
EDIT: One Question: What does the 31 in strbreak() mean?

Its the maxlen of the output string.

Br41n 12-17-2006 09:28

Re: read_file again...
 
OK.. thank you.. now, i got the problem with reading a file working.. but there is one more smaall problem... The script reads the commented lines also... All lines with comments I started with ;..

Code:

; Here is an Example:
; Syntax: <key> <command>
; Now, go on!
; ~~~~~~~~~~~~~~~~~
"m" "amx_menu"

So what to do? The script should not read the ; started lines, only the lines without ;...

Thanks
Br41n

[ --<-@ ] Black Rose 12-17-2006 11:18

Re: read_file again...
 
That's easy
Code:
public autobind(id, key) {         switch ( key ) {         case 0: {             new readdata[80], parsedkeys[32], parsedcommand[32]                         new fileh = fopen(filename, "r")                         if ( ! fileh )                 return                         while ( ! feof(fileh) ) {                 fgets(fileh, readdata, 79)                 if ( readdata[0] == ';' ) // add this line                     continue // add this line                 strbreak(readdata, parsedkeys, 31, parsedcommand, 31)                 client_print(id, print_chat, "KEY: ^"%s^" COMMAND: ^"%s^" ", parsedkeys, parsedcommand)             }             fclose(fileh)         }                 case 1: client_print(id, print_chat, "OK, no binding was changed. Type /help to see a list of available say commands.")     } }

Br41n 12-17-2006 12:10

Re: read_file again...
 
ok.. thanks.. i tried the same if request, but a little bit wrong... ^^

PHP Code:

if( readdata[0] != ';') {  // ignore comments while reading file
                      
strbreak(readdataparsedkeys31parsedcommand31)
                      
client_print(idprint_chat"[SkBinds] KEY: ^"%s^" COMMAND: ^"%s^" | This is only a test, we are working on a new plugin..."parsedkeysparsedcommand// maby you wanted to print this on EVERY command(?)
           



[ --<-@ ] Black Rose 12-17-2006 17:52

Re: read_file again...
 
that should also work... if you're file doesn't look like this
Since the ';' must be at the first "place" in the row, no spaces in front of it...
Code:

;code
    ;code
  ;code


dutchmeat 12-18-2006 04:16

Re: read_file again...
 
You could search all tokens in the line, if the isn't blank(space, hard space) check if it's a ';' then return 1.
But ofcourse you can just get the file to normal.


All times are GMT -4. The time now is 06:48.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.