From f39e53b85960e35aa7985702b106bea2b7ba5397 Mon Sep 17 00:00:00 2001 From: Brian Koropoff Date: Tue, 11 Jun 2002 05:41:59 +0000 Subject: [PATCH] Imported the beginnings of a standard "library" for GIB. --- doc/config/gib/list.gib | 123 ++++++++++++++++++++++++++++++++++++++++ doc/config/gib/std.gib | 22 +++++++ doc/config/gib/str.gib | 109 +++++++++++++++++++++++++++++++++++ 3 files changed, 254 insertions(+) create mode 100644 doc/config/gib/list.gib create mode 100644 doc/config/gib/std.gib create mode 100644 doc/config/gib/str.gib diff --git a/doc/config/gib/list.gib b/doc/config/gib/list.gib new file mode 100644 index 000000000..12d84849d --- /dev/null +++ b/doc/config/gib/list.gib @@ -0,0 +1,123 @@ +// list.gib +// +// "list" routines for GIB in QuakeForge 0.5.1 +// +// Copyright (C) 2002 Brian Koropoff +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +// Hash-like functions + +// Note: at first I thought using a bunch of cvars to +// hold lists was stupid, but then I realized that +// hashes are used for it internally anyway. The only +// problem is that everything is in one namespace, increasing +// the number of collisions. The hash size for cvars should +// probably be increased for this purpose + +// *************************************************************** +// list_set +// Sets the value of an element of a list with a certain key. +// Usage: list_set list_name key value +// Returns: Nothing. +// *************************************************************** + +alias list_set { + set qlist_${1}_${2} $3 +} + +// ********************************************************** +// list_get +// Fetches the value of an element of a list by its key. +// Usage: list_get list_name key +// Returns: the value of the element +// ********************************************************** + +alias list_get { + return ${qlist_${1}_${2}} +} + +// Array-like functions + +// ********************************************************** +// list_reset +// Resets a list to zero-size (does not delete elements) +// ********************************************************** + +alias list_reset { + set qlist_${1}_size 0; +} + +// ***************************************************** +// list_add +// Adds an element to the end of an array-like list +// ***************************************************** + +alias list_add { // Drop something into the end of a list + ifnot ${qlist_${1}_size} { // Make sure _size exists and has a numeric value + set qlist_${1}_size 0 + } + set qlist_${1}_size #{${qlist_${1}_size}+1}; // Increment size + set qlist_${1}_${qlist_${1}_size} $2; // Set value +} + +// ********************************************************* +// list_remove +// Removes an element from an array-like list and moves +// all elements after it down one. +// FIXME: Add list_insert and figure out some way +// to delete old elements (cvar_delete?) +// ********************************************************* + +alias list_remove { // Remove something from a numbered list + if #{~{list_get $1 size}==0} return; + for {i = $2; #{$i<${qlist_${1}_size}}; i = #{$i+1}} { + list_set $1 $i ~{list_get $1 #{$i+1}}; + }; + list_set $1 size #{~{list_get $1 size}-1}; +} + +// **************************************************************** +// list_find +// Locates either the first element of a list that matches +// a string or the first element starting at a specific index. +// **************************************************************** + +alias list_find { // Returns the index of a string in a list + ifnot ${qlist_${1}_size} { + return 0; + } + // This is an evil for statement. You have been warned. + for {if #{$argn > 2} {i = $2} else {i = 1}; #{$i<=~{list_get $1 size}}; i = #{$i+1}} { + if ~{streq $2 ~{list_get $1 $i}} { + return $i; + }; + }; + return 0; +} + +// ************************************************************ +// list_grab +// Chooses and returns a random element from an array-like +// list. Fun, but not that useful. +// ************************************************************ + +alias list_grab { // Get a random element + ifnot ${qlist_${1}_size} { + return 0; + } + number = ~{randint 1 ~{list_get $1 size}}; + return ~{list_get $1 $number}; +} diff --git a/doc/config/gib/std.gib b/doc/config/gib/std.gib new file mode 100644 index 000000000..986e07e89 --- /dev/null +++ b/doc/config/gib/std.gib @@ -0,0 +1,22 @@ +// std.gib +// +// standard routines for GIB in QuakeForge 0.5 +// +// Copyright (C) 2002 Brian Koropoff +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +exec gib/list.gib +exec gib/str.gib \ No newline at end of file diff --git a/doc/config/gib/str.gib b/doc/config/gib/str.gib new file mode 100644 index 000000000..7564c48a4 --- /dev/null +++ b/doc/config/gib/str.gib @@ -0,0 +1,109 @@ +// str.gib +// +// string routines for GIB in QuakeForge 0.5 +// +// Copyright (C) 2002 Brian Koropoff +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + +// Many of these string-manipulating expressions are pure evil. +// Eat your heart out, Larry. + +// ***************************************************************************** +// str_replace +// Replaces text between two positions in a string with a different string. +// Usage: str_replace base_string new_string start_position end_position +// Returns: the new string +// ***************************************************************************** + +alias str_replace { + basestr = $1 + newstr = $2 + pos1 = $3 + pos2 = $4 + return $basestr[0:#{$pos1-1}]$newstr$basestr[#{$pos2+1}:~{strlen $basestr}] + // Ugh. I think my punishment for creating such an ugly language + // is having to write the standard "library" for it +} + +// ******************************************************************************** +// str_findsubstr +// Finds the location of a substring within a string starting at any position. +// Usage: str_findsubstr haystack needle [start_position] +// Returns: The position of the substring or -1 if it wasn't found. +// ******************************************************************************** + +alias str_findsubstr { + basestr = $1 + substr = $2 + if #{$argn < 4} { + 3 = 0; // Fill in the last argument if we didn't get it + } + for {i = $3; #{$i < ~{strlen $basestr}}; i = #{$i + 1}} { + if ~{streq $substr $basestr[$i:#{$i + ~{strlen $substr} - 1}]} { + return $i + } + } + return -1 +} + +alias str_snip { + basestr = $1 + pos1 = $2 + pos2 = $3 + return $basestr[0:#{$pos1-1}]$basestr[#{$pos2+1}:~{strlen $basestr}] +} + +alias str_insert { + basestr = $1 + newstr = $2 + pos = $3 + return $basestr[0:#{$pos}]$newstr$basestr[#{$pos+1}:~{strlen $basestr}] +} + +alias str_escape { + basestr = $1; + toescape = $2; + for {i = 0; #{$i < ~{strlen $basestr}}; i = #{$i+1}} { + if #{~{str_findsubstr $toescape $basestr[$i]} >= 0} { + basestr = ~{str_insert $basestr "\\" #{$i-1}} + i = #{$i+1} + } + } + return $basestr +} + +alias str_gettoken { + str = $1 + toknum = $2 + delim = $3 + pos = 0 + toknum = #{$toknum - 1} + while $toknum { + pos = ~{str_findsubstr $str $delim $pos} + if #{$pos < 0} { + return "" + } + pos = #{$pos + ~{strlen $delim}} + toknum = #{$toknum - 1} + } + pos2 = ~{str_findsubstr $str $delim $pos} + if #{$pos2 < 0} { + return $str[$pos:~{strlen $str}] + } else { + return $str[$pos:#{$pos2-1}] + } +} \ No newline at end of file