Login
User Name:

Password:



Register
Forgot your password?
Vote for Us!
auth_update crash
Dec 23, 2017, 10:15 pm
By Remcon
check_tumble
Dec 18, 2017, 7:21 pm
By Remcon
parse description bug
Dec 15, 2017, 10:08 pm
By Remcon
Couple bugs
Dec 12, 2017, 5:42 pm
By Remcon
Bug in disarm( )
Nov 12, 2017, 6:54 pm
By GatewaySysop
LoP 1.46
Author: Remcon
Submitted by: Remcon
LOP 1.45
Author: Remcon
Submitted by: Remcon
LOP Heroes Edition
Author: Vladaar
Submitted by: Vladaar
Heroes sound extras
Author: Vladaar
Submitted by: Vladaar
6Dragons 4.3
Author: Vladaar
Submitted by: Vladaar
Users Online
CommonCrawl, Yandex

Members: 0
Guests: 22
Stats
Files
Topics
Posts
Members
Newest Member
478
3,708
19,242
613
bastian
Today's Birthdays
There are no member birthdays today.
Related Links
» SmaugMuds.org » General » Coding » explode and implode
Forum Rules | Mark all | Recent Posts

explode and implode
< Newer Topic :: Older Topic > arrays of structures or strings.. b

Pages:<< prev 1 next >>
Post is unread #1 Jul 26, 2004, 9:54 pm   Last edited Nov 24, 2007, 2:39 pm by Samson
Go to the top of the page
Go to the bottom of the page

cynshard

GroupMembers
Posts95
JoinedNov 19, 2003

I've been writing a lot of PHP lately for work and I figured I'd start messing with making explode and implode functions for afkmud. I quickly found out that it's not simple to return a double array.

Here's what I've got so far ( That doesn't work )

#include 
#include "mud.h"

typedef struct string 
{
	char *content;
} STRING;

STRING[] explode( const char *delimiter, char *string )
{
	char *token;
	STRING ret[MIL];
	STRING temp;
	int i = 0;
	
	if( !string || string[0] == '\0' )
  return NULL;
	token = strtok( string, delimiter );
	while( token != NULL )
	{
  i = 0;
  while( ret[i] != NULL )
  	i++;
  CREATE( temp, STRING, 1 );
  ret[i] = temp;
  token = strtok( NULL, delimiter );
	}
	
	return ret;
}


So I'm getting a syntax error at the STRING[] section of the explode declaration.

How would I go about making a string structure and passing it around?

Thanks,
Cynshard
       
Post is unread #2 Jul 27, 2004, 5:46 pm   Last edited Nov 24, 2007, 2:41 pm by Samson
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,639
JoinedJan 1, 2002

For those of us not fluent or familiar with PHP, what do implode and explode do exactly? Perhaps there's already something in the codebase which does this?

[Edit: Couldn't resist a bit of irony here in that I now love PHP string handling to death]
       
Post is unread #3 Jul 27, 2004, 6:01 pm
Go to the top of the page
Go to the bottom of the page

cynshard

GroupMembers
Posts95
JoinedNov 19, 2003

Explode takes a string and creates an array of strings based upon a specified delimeter( or several delimeters, I was just going to make it one though..)

Example:

char *example = "This/Is/an/Example"
char [x][y] array;

array = explode( "/", example );

if you were to walk the array.

you would get something like:

0 => This
1 => Is
2 => an
3 => Example

Conversly, Implode "glues" an array together by a given delimeter.

take the above array

example = implode( ",", array );

would set the variable example to "This,Is,an,Example"

I'm sure it's been done for other codebases ( MudOs has it I think ) but I would like to hack through it as a firm lesson in strings and pointers.

I'm fairly certain the explode function will need to return char**, since I can't seem to return char*[] or char[][]

       
Post is unread #4 Jul 27, 2004, 6:17 pm
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,639
JoinedJan 1, 2002

lol, I should know better than to ask about string manipulation. The explanations almost never make sense to me, but perhaps Xorith will wander by and see this and have something to comment on. He's much better with PHP and would probably be the best one of us to comment on it.
       
Post is unread #5 Jul 27, 2004, 7:06 pm   Last edited Nov 24, 2007, 2:42 pm by Samson
Go to the top of the page
Go to the bottom of the page

cynshard

GroupMembers
Posts95
JoinedNov 19, 2003

I've been thinking ( run! ), maybe I should be putting these tokens into a linked list and returning the list instead of trying to return a char**. I could use the supplied doubly linked list macros... HMM...

-- Update --

I have completed a testable version of both explode and implode. Please give these a look/try and tell me what you think.
Here are my two files:

mudstring.h
// MudString.h

#include "mud.h"

typedef struct mud_string MUD_STRING;

struct mud_string
{
	MUD_STRING *next;
	MUD_STRING *prev;
	char *content;
};


mudstring.c
// MudString.c
#include 
#include "mudstring.h"

//
// This function returns a linked list of strings 
// Example:
//
// explode( "-", "Happy-go-lucky" )
// will create a linked list of MUD_STRINGS with the following content:
// Happy
// go
// lucky
//

MUD_STRING *explode( char *delimiter, char *string )
{
	char *token;
	MUD_STRING *first = NULL, *last = NULL, *tok;
	
	if( !string || string[0] == '\0' )
  return NULL;
  
	token = strtok( string, delimiter );
	
	do
	{
  CREATE( tok, MUD_STRING, 1 );
  tok->content = token;
  LINK( tok, first, last, next, prev );
  token = strtok( NULL, delimiter );
	} while( token != NULL );
	
	return first;
}

//
// This function returns a pointer to a string
// Example:
//
// implode( "-", string_list )
// will create a string "glued" together from the contents of the list:
// 
// list-list-list
// 


char *implode( char *glue, MUD_STRING *list )
{
	char *ret = "";
	MUD_STRING *temp;
	
	if( !glue || glue[0] == '\0' || !list )
  return NULL;
	
	CREATE( ret, char, 1 );
	
	for( temp = list; temp; temp = temp->next )
	{
  mudstrlcat( ret, temp->content, (strlen(ret)+strlen(temp->content)+1) );
  
  if( temp->next )
  	mudstrlcat( ret, glue, (strlen(ret)+strlen(glue)+1) );
	}
	
	return ret;
}

// These are some test functions

CMDF do_explode( CHAR_DATA *ch, char *argument )
{
	MUD_STRING *string, *show;
	char arg1[MIL];
	
	if( !argument || argument[0] == '\0' )
  return;
	
	argument = one_argument( argument, arg1 );
	
	string = explode( (char *)arg1, argument );
	
	for( show = string; show; show = show->next )
  log_printf( "%s", show->content );
	return;
}

CMDF do_implode( CHAR_DATA *ch, char *argument )
{
	MUD_STRING *string;
	char arg1[MIL];
	char *mploded;
	
	if( !argument || argument[0] == '\0' )
  return;
	
	argument = one_argument( argument, arg1 );
	
	string = explode( (char *)arg1, argument );
  
	mploded = implode( (char *)arg1, string );
	
	log_printf( "%s", mploded );
	
	return;
}


Now my concern is memory leaks. I'm not the greatest in knowing how to handle dynamic memory allocation/deallocation. I'm not sure WHEN I should DISPOSE the MUD_STRING variables made with the CREATE macro.
       
Pages:<< prev 1 next >>