TinTin++ Mud Client The TinTin++ message board

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
TinTin++ Mud Client

Patch to add #list tokenize command

 
Post new topic   Reply to topic    The TinTin++ message board Forum Index -> Development
View previous topic :: View next topic  
Author Message
sj



Joined: 22 Jan 2012
Posts: 16
Location: Manchester, England

PostPosted: Sat May 18, 2013 10:57 am    Post subject: Patch to add #list tokenize command Reply with quote

This will create a list from a delimited string.

An example:
#list locations tokenize { } {TAKE HEAD LIGHT}
#showme {$locations}
{1}{TAKE}{2}{HEAD}{3}{LIGHT}

Maybe someone else will find it useful, so here you go.

Code:

From b18c3904e6ab9cdf3a176d840d23de53ca341067 Mon Sep 17 00:00:00 2001
From: Steven Jackson <sj@oscode.net>
Date: Sat, 18 May 2013 16:52:55 +0100
Subject: [PATCH] Added a tokenize list command.

---
 src/help.c   | 23 ++++++++++++-----------
 src/list.c   | 37 ++++++++++++++++++++++++++++++++++++-
 src/tables.c |  1 +
 src/tintin.h |  1 +
 4 files changed, 50 insertions(+), 12 deletions(-)

diff --git a/src/help.c b/src/help.c
index 5fdb7c9..139b91e 100755
--- a/src/help.c
+++ b/src/help.c
@@ -740,17 +740,18 @@ struct help_type help_table[] =
       "LIST",
       "<178>Command<078>: #list <178>{<078>variable<178>}<078> <178>{<078>option<178>}<078> <178>{<078>argument<178>}<078>\n"
       "\n"
-      "         #list {var} {add} {item}               Add {item} to the list\n"
-      "         #list {var} {clear}                    Empty the given list\n"
-      "         #list {var} {create} {item}            Create a list using {item}\n"
-      "         #list {var} {delete} {index} {number}  Delete the item at {index},\n"
-      "                                                the {number} is optional.\n"
-      "         #list {var} {insert} {index} {string}  Insert {string} at given index\n"
-      "         #list {var} {find} {string} {variable} Return the found index\n"
-      "         #list {var} {get} {index} {variable}   Copy an item to {variable}\n"
-      "         #list {var} {set} {index} {string}     Change the item at {index}\n"
-      "         #list {var} {size} {variable}          Copy list size to {variable}\n"
-      "         #list {var} {sort} {string}            Insert item in alphabetic order\n"
+      "         #list {var} {add} {item}                     Add {item} to the list\n"
+      "         #list {var} {clear}                          Empty the given list\n"
+      "         #list {var} {create} {item}                  Create a list using {item}\n"
+      "         #list {var} {delete} {index} {number}        Delete the item at {index},\n"
+      "                                                      the {number} is optional.\n"
+      "         #list {var} {insert} {index} {string}        Insert {string} at given index\n"
+      "         #list {var} {find} {string} {variable}       Return the found index\n"
+      "         #list {var} {get} {index} {variable}         Copy an item to {variable}\n"
+      "         #list {var} {set} {index} {string}           Change the item at {index}\n"
+      "         #list {var} {size} {variable}                Copy list size to {variable}\n"
+      "         #list {var} {sort} {string}                  Insert item in alphabetic order\n"
+      "         #list {var} {tokenize} {delimiters} {string} Tokenize the string in to the list\n"
       "\n"
       "         The index should be between 1 and the list's length. You can also give\n"
       "         a negative value, in which case -1 equals the last item in the list, -2\n"
diff --git a/src/list.c b/src/list.c
index b82d947..74fac4b 100755
--- a/src/list.c
+++ b/src/list.c
@@ -45,7 +45,7 @@ DO_COMMAND(do_list)
 
    if (*arg1 == 0 || *arg2 == 0)
    {
-      show_message(ses, LIST_VARIABLE, "#SYNTAX: #LIST {variable} {ADD|CLE|CRE|DEL|FIN|GET|INS|SET|SIZ|SOR} {argument}");
+      show_message(ses, LIST_VARIABLE, "#SYNTAX: #LIST {variable} {ADD|CLE|CRE|DEL|FIN|GET|INS|SET|SIZ|SOR|TOK} {argument}");
    }
    else
    {
@@ -421,3 +421,38 @@ DO_ARRAY(array_sort)
    }
    return ses;
 }
+
+DO_ARRAY(array_tokenize)
+{
+   char delimiters[BUFFER_SIZE], text[BUFFER_SIZE];
+
+   arg = sub_arg_in_braces(ses, arg, delimiters, GET_ONE, SUB_VAR|SUB_FUN);
+   arg = sub_arg_in_braces(ses, arg, text, GET_ALL, SUB_VAR|SUB_FUN);
+
+   if(*delimiters == 0 || *text == 0)
+   {
+      show_message(ses, LIST_VARIABLE, "#SYNTAX: #LIST {variable} TOKENIZE {delimiters} {string}");
+   }
+   else
+   {
+      char* tok;
+      int index;
+
+      if(!list->root)
+      {
+         list->root = init_list(ses, LIST_VARIABLE, LIST_SIZE);
+      }
+
+      index = list->root->used + 1;
+
+      tok = strtok(text, delimiters);
+      while(tok)
+      {
+         set_nest_node(list->root, ntos(index++), "%s", tok);
+         tok = strtok(NULL, delimiters);
+      }
+   }
+
+   return ses;
+}
+
diff --git a/src/tables.c b/src/tables.c
index 7a10b79..3a39381 100755
--- a/src/tables.c
+++ b/src/tables.c
@@ -453,6 +453,7 @@ struct array_type array_table[] =
    {     "SIZE",             array_size          },
    {     "SORT",             array_sort          },
    {     "SRT",              array_sort          },
+   {     "TOKENIZE",         array_tokenize      },
    {     "",                 NULL                }
 };
 
diff --git a/src/tintin.h b/src/tintin.h
index 7a627a9..5147a6b 100755
--- a/src/tintin.h
+++ b/src/tintin.h
@@ -1037,6 +1037,7 @@ extern DO_ARRAY(array_get);
 extern DO_ARRAY(array_size);
 extern DO_ARRAY(array_set);
 extern DO_ARRAY(array_sort);
+extern DO_ARRAY(array_tokenize);
 
 #endif
 
--
1.8.1.2
Back to top
View user's profile Send private message Visit poster's website
Scandum
Site Admin


Joined: 03 Dec 2004
Posts: 3770

PostPosted: Thu Aug 22, 2013 7:02 am    Post subject: Reply with quote

You can use:

Code:

#var locations {TAKE HEAD LIGHT};
#replace locations { } {;}


And if an index is required:

Code:

#list {locations} {create} {$locations}


I've also already added a tokenize option in the beta that splits up a string in characters, and I'll probably add untokenize to merge a list into a string.
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    The TinTin++ message board Forum Index -> Development All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Get TinTin++ Mud Client at SourceForge.net. Fast, secure and Free Open Source software downloads Get TinTin++ Mud Client at SourceForge.net. Fast, secure and Free Open Source software downloads
TinTin++ Homepage

Powered by phpBB © 2001, 2002 phpBB Group