Raised This Month: $ Target: $400
 0% 

Get only 8th line from array


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
SwataZ
BANNED
Join Date: Jan 2008
Old 08-07-2008 , 15:04   Get only 8th line from array
Reply With Quote #1

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?
SwataZ is offline
Lee
AlliedModders Donor
Join Date: Feb 2006
Old 08-07-2008 , 18:28   Re: Get only 8th line from array
Reply With Quote #2

Loop through the array and count the \n (new line) characters.
__________________
No support via PM.
Lee is offline
zwfgdlc
Senior Member
Join Date: May 2006
Old 08-08-2008 , 01:55   Re: Get only 8th line from array
Reply With Quote #3

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]);         }     }
zwfgdlc is offline
SwataZ
BANNED
Join Date: Jan 2008
Old 08-08-2008 , 05:28   Re: Get only 8th line from array
Reply With Quote #4

zwfgdlc, text variable returns nothing :/ Is in pawn explode function, like PHP ?

Last edited by SwataZ; 08-08-2008 at 05:31.
SwataZ is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 08-08-2008 , 05:31   Re: Get only 8th line from array
Reply With Quote #5

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. )
__________________

Last edited by Arkshine; 08-08-2008 at 06:32.
Arkshine is offline
SwataZ
BANNED
Join Date: Jan 2008
Old 08-08-2008 , 05:49   Re: Get only 8th line from array
Reply With Quote #6

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 is offline
SwataZ
BANNED
Join Date: Jan 2008
Old 08-08-2008 , 05:52   Re: Get only 8th line from array
Reply With Quote #7

arkshine, man , it works! Thank you so mutch dude Karma+
SwataZ is offline
Reply


Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump


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


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