AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Get only 8th line from array (https://forums.alliedmods.net/showthread.php?t=75541)

SwataZ 08-07-2008 15:04

Get only 8th line from array
 
Hi,

I use sockets module ant i get data from web server. I'm new in scripting. Sockets returns that for me:

HTTP/1.1 200 OK
Date: Thu, 07 Aug 2008 18:57:16 GMT
Server: Apache
Transfer-Encoding: chunked
Content-Type: text/html

5
lukas
0

That returns my buf[512] array. How can i get only "lukas" (8th line) from this?

Lee 08-07-2008 18:28

Re: Get only 8th line from array
 
Loop through the array and count the \n (new line) characters.

zwfgdlc 08-08-2008 01:55

Re: Get only 8th line from array
 
try this.
Code:
    new text[32];     new line=1;     new pos=-1;     new start=-1;     new end=-1;     while((pos=strfind(buf,"\n"))!=-1)     {         line++;         if(line==8)         start=pos;         if(line==9)         {             end=pos;             copy(text,end-start,buf[start]);         }     }

SwataZ 08-08-2008 05:28

Re: Get only 8th line from array
 
zwfgdlc, text variable returns nothing :/ Is in pawn explode function, like PHP ?

Arkshine 08-08-2008 05:31

Re: Get only 8th line from array
 
Here what I've done.

Method without any extra checks. ( So, no safe depending how you use it. But, if the buffer is always the same, it's perfectly fine :) )

Code:
ExtractName ( const s_Buffer[], s_Name[] ) {     new i, i_Cnt; while ( s_Buffer[ i++ ] != '^n' || ++i_Cnt != 7 ) {}     new j = i;    while ( s_Buffer[ i++ ] != '^n' ) {}             i = 0; do { s_Name[ i++ ] = s_Buffer[ j++ ]; } while ( s_Buffer[ j ] != '^n' ); }

So, to retrieve name :

Code:
new s_Name[ 32 ]; ExtractName ( s_Buffer, s_Name );

Tested and it works.

Is it enough for you ? I mean you want just to retrieve the name, that's all ?

Maybe do you want a more safe method and more general ? ( eg, like retrieving the line of your choice, etc. )

SwataZ 08-08-2008 05:49

Re: Get only 8th line from array
 
Ant i think, while() cycle doesn't work here, because server_print() does not print anything:

Code:

/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <amxmisc>
#include <sockets>

#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "SwataZ"
new g_sckweb //socket "id"
#define SCRIPT_NAME "/indexas.php"
#define REMOTE_HOST "c-s.lt" //port d.80



public plugin_init() {
    register_plugin(PLUGIN, VERSION, AUTHOR)
    set_task(1.0, "dispTime", 0, "", 0, "b")
    // Add your code here...
}

public dispTime()


{     
   
    new error = 0
    new constring[512]
    new timeout

    g_sckweb = socket_open(REMOTE_HOST, 80, SOCKET_TCP, error)
    if (g_sckweb > 0) {
    format(constring,511,"GET %s HTTP/1.1^nHost: %s^n^n",SCRIPT_NAME,REMOTE_HOST)

    socket_send(g_sckweb,constring, 511)
    new buf[512]
   
    timeout=0
    socket_recv(g_sckweb, buf, 511)
    new text[32];
    new line=1;
    new pos=-1;
    new start=-1;
    new end=-1;
   
    while(pos=strfind(buf,"\n") != -1)
    {
        line++;
        if(line==8)
        start=pos;
        if(line==9)
        {
        end=pos;
       
            copy(text,end-start,buf[start]);
        server_print("Startas: %d Endas: %d bufas: %s",start,end,buf);
        }
    }
   

    //set_hudmessage(255, 255, 255, 0.8, 0.1, 0)

   
}
}


SwataZ 08-08-2008 05:52

Re: Get only 8th line from array
 
arkshine, man , it works! Thank you so mutch dude :) Karma+


All times are GMT -4. The time now is 05:32.

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