Skip to content

Instantly share code, notes, and snippets.

View ClassViewer.cfc
/**
ClassViewer.cfc
@hint An almost pure CFML implementation of ClassViewer.class (see http://adamcameroncoldfusion.blogspot.co.uk/2012/08/classviewerjava.html).
@description
*/
component {
// these are just for formatting the output
variables._NL = createObject("java", "java.lang.System").getProperty("line.separator");
variables._INDENT = " ";
@adamcameron
adamcameron / medalTable.cfm
Created Aug 13, 2012
Code for generating my medal tables (see: http://bit.ly/RG6FLD)
View medalTable.cfm
<cfset CRLF = chr(13) & chr(10)>
<cfsavecontent variable="rendered">
<cfsetting enablecfoutputonly="true">
<cfoutput>
<style>
<!--- these are inline because I actually need the styles, not a CSS link due to a vagary of the blog --->
<cfinclude template="./styles.css">
</style>
</cfoutput>
<cfoutput><table border="0" cellpadding="0" cellspacing="0" valign="top"></cfoutput>
@adamcameron
adamcameron / Application.cfc
Created Aug 15, 2012
An Application.cfc which logs when various variables become available
View Application.cfc
component {
param name="server.hitId" default=0;
server.hitId++;
testScopes("in pseudoconstructor before application name is set", "pseudoconstructor");
this.name = "scopeAvailability";
this.sessionManagement = true;
testScopes("in pseudoconstructor after application name is set", "pseudoconstructor");
function onApplicationStart(){
@adamcameron
adamcameron / structToKeys.cfm
Created Sep 18, 2012
Function to recursively get all structkeys and convert to dot notation
View structToKeys.cfm
<cfscript>
// data to test with
st = {
top1 = "top1 value",
top2 = {
middle = {
bottom1 = [1,2,3],
bottom2 = {},
bottom3 = "bottom3 value"
}
@adamcameron
adamcameron / Application.cfc
Created Sep 19, 2012
An Application.cfc to test what happens when this.datasource is set by a conditional using a CGI variable
View Application.cfc
component {
this.name = "testPseudoConstructor";
this.applicationTimeout = createTimeSpan(0,0,1,0);
this.sessionManagement = true;
this.sessionTimeout = createTimeSpan(0,0,0,30);
if (CGI.server_name == "localhost"){
this.datasource = "dsn1";
}else if (CGI.server_name == "testing.local"){
@adamcameron
adamcameron / paths.cfm
Created Sep 21, 2012
Demonstrating some (possible) weirdness with expandPath()
View paths.cfm
<cfoutput>
#expandPath(".")#<br /> <!--- for me outputs D:\websites\www.scribble.local\junk --->
#expandPath("/")#<br /> <!--- for me outputs d:\websites\www.scribble.local\. Note the difference in capitalisation of the drive letter --->
</cfoutput>
@adamcameron
adamcameron / baseMToBaseN.cfm
Created Sep 24, 2012
Converts a number from one arbitrary base to another arbitrary base. Is not restricted to bases that Java and CF natively support in their equivalent built-in functions. The internal maths are restricted to the bounds of java.math.BigInteger though.
View baseMToBaseN.cfm
/**
@hint Converts a number from one arbitrary base to another arbitrary base. Is not restricted to bases that Java and CF natively support in their equivalent built-in functions. The internal maths are restricted to the bounds of java.math.BigInteger though.
@number The number to convert.
@fromBase The base to convert from. Can either be one of BIN, DEC, HEX, BASE36, BASE62 or an 'alphabet' or characters that represent the digits. EG: OCTAL would be 01234567.
@toBase The base to convert to. Has same value rules as fromBase.
*/
string function baseMToBaseN(required string number, required string fromBase, required string toBase){
if (fromBase == toBase){ // ie: there's nothing to do
return number;
}
View testBaseMToBaseN.cfm
// testing
test.description = "A fairly easy to visually-test example";
test.number = 255;
test._baseFrom = "dec";
test._baseTo = "bin";
test._result = baseMToBaseN(test.number, test._baseFrom, test._baseTo);
writeDump(test);
test.description = "Another one";
@adamcameron
adamcameron / dateNow.cfm
Created Sep 24, 2012
How to create a java.sql.Date in CF
View dateNow.cfm
dateNow = createObject("java", "java.sql.Date").init(dateDiff("s", createDate(1970,1,1), now())*1000);
@adamcameron
adamcameron / renderWith.cfm
Last active Oct 11, 2015
This is a fairly contrived example of closure in CFML
View renderWith.cfm
<cfscript>
/**
@hint creates a function which will wrap passed-in text with the specified tag. Any addition arguments passed to this function will be treated as attribute/value pairs of the tag
@tag The tag to wrap the function's text in
*/
string function renderWith(required string tag){
var theTag = "";
var attributes = "";
var attribute = "";