#include inherit "module"; inherit "caudiumlib"; // Module Parser, allows definition of tags, etc. constant module_type = MODULE_PARSER; constant module_name = "Harry Potter Container Module"; // Note the first \n is where the CIF decides where to wrap for // Less Documentation/More Documentation constant module_doc = #"This module was designed to show how a container module works.

\n Usage:

11.23 USD =
<usdtohp>11.23</usdtohp>
<p>
2 Galleons, 5 Sickle, and 14 Knuts =
<hptousd>
<galleons>2</galleons>
<sickles>5</sickles>
<knuts>14</knuts>
</hptousd> USD

\n"; constant module_unique = 1; constant thread_safe=1; // Initialize two counters just so we can keep track of how many // firstyears are reloading the pages int hptousdcount = 0; int usdtohpcount = 0; string container_usdtohp(string t, mapping m, string contents, object id) { usdtohpcount++; // How do we know this? // Price of book, $3.99 (14 sickles, 3 knuts) // 29 knuts in a sickle, 17 sickles in a galleon int knutindollar = 15 + 3*29; int knutsinsickle = 29; int knutsingalleon = 17 * knutsinsickle; float dollars = (float)contents; int totalknuts = (int)(dollars*knutindollar+0.5); int galleons = totalknuts / knutsingalleon; int sickles = (totalknuts - (galleons * knutsingalleon)) / knutsinsickle; int knuts = totalknuts - (galleons * knutsingalleon) - (sickles * knutsinsickle); return(sprintf("%d galleons, %d sickles and %d knuts",galleons,sickles,knuts)); } string container_hptousd(string t, mapping m, string contents, object id) { hptousdcount++; // How do we know this? // Price of book, $3.99 (14 sickles, 3 knuts) // 29 knuts in a sickle, 17 sickles in a galleon float knutindollar = (float)(15 + 3*29); int knutsinsickle = 29; int knutsingalleon = 17 * knutsinsickle; // Parse the contents of the container to set id->misc with the appropriate // values string parsed = parse_rxml(contents, id); int galleons = (int)id->misc->galleons; int sickles = (int)id->misc->sickles; int knuts = (int)id->misc->knuts; float dollars = 0.0; int totalknuts = (galleons * knutsingalleon) + (sickles * knutsinsickle) + knuts; return(sprintf("%.2f",totalknuts/knutindollar)); } // We define a container that is called by 3 different containers in the // html. Since we can see the tag name that was called, we can save time // and have the same routine called for each tag rather than writing // 3 different functions string container_hpmoney(string tag, mapping m, string contents, object id) { // we store the value of the contents in the container in id->misc id->misc[tag] = (int)contents; // Note: Evidently you must have a non-zero return or Caudium ignores // the callback return(""); } mapping query_container_callers() { return ([ "usdtohp":container_usdtohp,"hptousd":container_hptousd, "galleons":container_hpmoney,"sickles":container_hpmoney, "knuts":container_hpmoney ]); } string status() { // This is displayed in the Config Interface, Status return("The USDtoHP tag was called "+(string)usdtohpcount+" time(s)
" "The HPtoUSD tag was called "+(string)hptousdcount+" time(s)
"); }