madBE WARNED! Despite my following comments, this still has SERIOUS memory leaks. mad
mad Im working on it! mad


AT LAST! laugh
Here is the re-write I promised and Ive turned it into a REAL function, like was originally requested.

Ive spent the whole of today on it (at work hehehe wink ), and I was willing to spend the time as I'm sure
to need it myself later as I often use that format to store data in other apps/languages (VB#,C#). crazy

The first block of code is THE Function itself, after much work tired and research smile I believe is close to ready.
Now all I need if some of the experts here to fine tune it a bit.

The Second block is two examples of the code in use, which should be easily copied into a new or existing
project so you can see how it is used. I've supplied an easy version and a "clean" version.

The Function itself if very heavily documented and if I seem to be doing simple things in odd ways, its
just the way I code unfortunately, I seem to have a slightly twisted mind crazy, and Im only a Lite-C noob.

Enjoy and feel free to ask questions or make suggestions if you need.

PS Many thanks Joozey for clarifying my Garbage collection questions. VERY helpful!!
I now know the Garbage Collection Ive built into this function is necessary, if rather unwieldy.

Code:
function string_split(STRING* SourceString)
{	
   if(str_len(SourceString)==0)   return(str_create("0"));      //Invalid SourceString
   //
   char* Delim = ",";       // Delimiter to 'split' around
   STRING* tmpItem;         // Temp String Space used for element building 
   STRING* TempData = str_create(SourceString);   // Temp String used to step through SourceString
   ///Count Number Of Elements 
   var ItemCount = 1;       //Assume 1 already so no trailing comma needed after last
   while(str_len(TempData)>0)
   {	
      ItemCount += str_cmpni(TempData,Delim);      //if this byte is a Delimiter,  increment ItemCount
      str_clip(TempData,1);                        //remove checked character/byte 
   }
   /// Create Results array, with extra element[0] reserved for ItemCount return
   STRING* *ReturnArray = (STRING*)malloc(sizeof(STRING*)*(ItemCount+1));		
   /// Create Each Element in Results Array - Starting at element[1] because 0 is reserved
   TempData = str_create(SourceString);			
   for(cnt=1;cnt<=ItemCount+1;cnt++)
   {	
      tmpItem=str_create(TempData);        //put remaining SourceString proxy into Element
      if(str_stri(tmpItem, _str(Delim))>0)      //look for next Delimiter
      {	
         str_clip(TempData,str_stri(tmpItem, _str(Delim)));   //remove THIS element from SourceString proxy
         str_trunc(tmpItem, str_len(tmpItem) - str_stri(tmpItem, _str(Delim)) + 1);   //Remove Delimiter and beyond
      }
      else   str_cpy(TempData,"");   //Remove rest of SourceString proxy as this element was the last
      ReturnArray[cnt] = str_create(tmpItem);   //copy "temp" item into Results Array at correct element
      str_remove(tmpItem);         //flush used "temp" to avoid memory leak
   }
   //save ItemCount into element[0]. Can be retrieved from Target as follows "XXXXX = str_to_num(MyArray[0]);"
   ReturnArray[0] = str_for_num(TempData,ItemCount);	
   return(ReturnArray);	
}


And here is the sample/demo code.

Code:
//
   //
   //Suggested Clean and Safe Usage      (to my mind anyway)
   ...
   ...
// vvvvvvvv  'STRING**' type is VERY important.	Wont work otherwise.
   STRING**   DataArray =   0;         // LOCALLY defined Result Array Pointer
   var        ArrayLength = 0;         // LOCALLY defined Result Array Length storage
   ...
   ...
   //Valid string from any source
   STRING*   DataList = "Entry1,Entry2,Entry3,Entry4,Entry5,Entry6,Entry7,Entry8";
   ...
   ...
   if(DataArray != 0)   free(DataArray);      //Flush and Free resources from last use if any.
   DataArray = string_split("Entry1,Entry2,Entry3,Entry4,Entry5,Entry6,Entry7,Entry8");
   ArrayLength = str_to_num(DataArray[0]);
   if(ArrayLength == 0) break;   // Nothing in string - Do something about it.
   ...
   ...
   //Do something with Array of Strings.
   ...
   ...
   free(DataArray);   //Release and Free used memory.	
   //
   //
   //
   //
   /////////////////////////////////////////////////////////////////////////////////////////////
   //Simplified Usage
// vvvvvvvv  'STRING**' type is VERY important.	Wont work otherwise.
   STRING** DataArray = string_split("Entry1,Entry2,Entry3,Entry4,Entry5,Entry6,Entry7,Entry8");	
   var ArrayLength = str_to_num(DataArray[0]);
   //
   //
   ///////////// Debugging code - loog in acklog.txt in working folder after running
      diag("\n\nDebug Tag--> 'string_split' has decoded "); 
      diag(str_for_num(tmpStr,ArrayLength));	diag(" elements.\n");
      for(cnt=1;cnt<=ArrayLength;cnt++)
      {
         diag(DataArray[cnt]);	diag(" <-\n");
      }
   ///////////// End of Debugging code
   //
   //Once finished with it empty it as follows
   free(DataArray);
   //
   //
   //



Last edited by EvilSOB; 07/29/08 22:08. Reason: LEAKY MEMORY STILL

"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial