AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [SOLVED]Extractstr stock (https://forums.alliedmods.net/showthread.php?t=63702)

Alka 11-27-2007 14:09

[SOLVED]Extractstr stock
 
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 :D ....and so on, mixing words :S" ,i want to show only "my stock". If anyone maybe know... :-p

Thanks.

purple_pixie 11-28-2007 08:24

Re: Extractstr stock
 
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 ; }

alien 11-28-2007 09:42

Re: Extractstr stock
 
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.

purple_pixie 11-28-2007 09:48

Re: Extractstr stock
 
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.

[ --<-@ ] Black Rose 11-28-2007 09:49

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

strtok(String, temp, 1, String, 127, '|', 1)
strtok(String, TempString, 127, temp, 127, '|', 1)

Alka 11-29-2007 07:59

Re: Extractstr stock
 
@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.


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

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