Raised This Month: $ Target: $400
 0% 

[SOLVED]Extractstr stock


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Alka
AMX Mod X Plugin Approver
Join Date: Dec 2006
Location: malloc(null)
Old 11-27-2007 , 14:09   [SOLVED]Extractstr stock
Reply With Quote #1

Hi. I've maded a stock, but is not working as i expected...is not like "strtok" or something else... E.g i have a string...
Code:
static String[] = "wow | my stock | is working. :D";
i want to extract "my stock" from that string. Here is the stock...

Code:
stock extractstr(Source[], Char1, Char2, RetString[128])
{
 new iMaxLen;
 iMaxLen = strlen(Source);
 
 new bool:iStarted;
 new Len, TempString[256];
 
 Len = formatex(TempString, sizeof TempString - 1, "");
 
 for(new i = 0 ; i < iMaxLen ; i++)
 {
  if((Source[i] == Char2) && iStarted)
   iStarted = false;
 
  if((Source[i] == Char1) && !iStarted)
   iStarted = true;
 
  if(iStarted)
  {
   Len += formatex(TempString[Len], (sizeof TempString - 1) - Len, "%s", Source[i]);
   //i think here is the problem, because i must only add one by one char. :/
  }
 }
 trim(TempString);
 formatex(RetString, sizeof RetString - 1, "%s", TempString);
}
when i use this...

Code:
public Test(id)
{
 static String[] = "wow | my stock | is working. :D";
 static TempString[128];
 
 extractstr(String, '|', '|', TempString);
 
 client_print(id, print_chat, "Testing my stock : %s", TempString);
}
currently is printing, "| my stock | wow my is working ....and so on, mixing words :S" ,i want to show only "my stock". If anyone maybe know... :-p

Thanks.
__________________
Still...lovin' . Connor noob! Hello

Last edited by Alka; 11-29-2007 at 08:21.
Alka is offline
purple_pixie
Veteran Member
Join Date: Jun 2007
Location: Winchester, England
Old 11-28-2007 , 08:24   Re: Extractstr stock
Reply With Quote #2

Don't quite have time to read all that, but is it anything like this stock?

Code:
stock str_piece(const input[],output[][],outputsize,piecelen,&token='|') {     // takes the string input and puts it into output[]     // each level of output is a "piece" of input, delimeted by token.     // return value is the number of pieces.     // outputsize is the max number of levels to fill ( sizeof output )     // piecelen is the max length of a piece ( sizeof output[] - 1 )     // e.g.       // new array[3][4];     // str_piece("abcdef|ghijkl|mnopqr|stuvw|xyz",array,sizeof array,sizeof array[] - 1,'|')     // returns 3.     // and sets the array as:     // array[0]="abc"     // array[1]="ghi"     // array[2]="mno"     // ( only 3 pieces can be returned, with a max length of 3 )         new i=-1,pieces,len=-1 ;     while ( input[++i] != 0 )     {         if ( input[i] != token )         {             if ( ++len < piecelen )                 output[pieces][len] = input[i] ;         }         else         {             output[pieces++][++len] = 0 ;             len = -1 ;             if ( pieces == outputsize )                 return pieces ;         }     }     return pieces + 1; }

(From pixie_stocks, naturally)

EDIT: OK, similar but not quite the same.
Can't see the problem with yours ... does this work?
Code:
stock extractstr(source[],start,end,output[128]) {     new maxlength = strlen(source)     new len     new bool:started     for ( new i ; i < maxlength ; i ++ )     {         if ( started )         {             if ( source[i] == end )             {                 output[len] = 0 ;                 break ;             }             output[len++]=source[i] ;             if ( len == sizeof output )             {                 output[len] = 0 ;                 break ;             }             continue ;         }         if ( source[i] == start )             started = true ;     }     trim(output) ;     return ; }

Last edited by purple_pixie; 11-28-2007 at 08:33.
purple_pixie is offline
alien
Senior Member
Join Date: Aug 2005
Location: London || Slovakia
Old 11-28-2007 , 09:42   Re: Extractstr stock
Reply With Quote #3

Did a fx() for you
Should be even much more faster.

Code:
stock extractstr(source[], char1, char2, result[128])     {         new             length = strlen(source),             i, // position of char1 in source             j; // position of char2 in source                     // let's find char1 first ...                 for (i = 0; i < length; i++)             if (source[i] == char1) break;                     if (i < length) // is char1 part of source?             {                 // let's find char2 afterwards ...                                 for (j = i + 1; j < length; j++)                     if (source[j] == char2) break;                                     if (j < length) // is char2 part of source?                     {                         new result_length = j - i + 1;                                                 if (result_length < 128) // is result long enough to hold the string?                             {                                 // return the string and result length                                                                 copy(result, result_length, source[i]);                                 return result_length;                             }                     }             }                     // return empty string and 0                 result[0] = 0;         return 0;     }

Oh btw .. it's also returning a length of the result. Just for a case.
__________________

Last edited by alien; 11-28-2007 at 09:45.
alien is offline
Send a message via ICQ to alien
purple_pixie
Veteran Member
Join Date: Jun 2007
Location: Winchester, England
Old 11-28-2007 , 09:48   Re: Extractstr stock
Reply With Quote #4

Looks like it should work.
Still haven't tested mine, or tried to fix Alka's

EDIT: Although neither of ours will work where source=output, which Alka's does.
purple_pixie is offline
[ --<-@ ] Black Rose
ANNIHILATED
Join Date: Sep 2005
Location: Stockholm, Sweden.
Old 11-28-2007 , 09:49   Re: Extractstr stock
Reply With Quote #5

new TempString[128], temp[2], String[128] = "wow | my stock | is working. "

strtok(String, temp, 1, String, 127, '|', 1)
strtok(String, TempString, 127, temp, 127, '|', 1)
[ --<-@ ] Black Rose is offline
Old 11-28-2007, 09:51
purple_pixie
This message has been deleted by purple_pixie. Reason: I'm a tool. Who can't read
Old 11-28-2007, 10:14
[ --<-@ ] Black Rose
This message has been deleted by [ --<-@ ] Black Rose. Reason: Unecessary
Old 11-28-2007, 10:16
purple_pixie
This message has been deleted by purple_pixie. Reason: Still a tool.
Alka
AMX Mod X Plugin Approver
Join Date: Dec 2006
Location: malloc(null)
Old 11-29-2007 , 07:59   Re: Extractstr stock
Reply With Quote #6

@purple_pixie - i think your stock "str_piece" stock will work too...thanks for other one. ;P
@alien - thanks.
@Black Rose - thanks.

I will try those things :p

EDIT:purple_pixie's works fine. :d thanks.
__________________
Still...lovin' . Connor noob! Hello

Last edited by Alka; 11-29-2007 at 08:20.
Alka is offline
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 On
HTML code is Off

Forum Jump


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


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