|
|
C Cod - Examples
Compile C on Demand
The purpose of this page is to demonstrate different uses
of C Cod and CSP through examples. Please
browse the various snippets below. Anything you can do with
in ANSI/C99 C you can do with C Cod. As of version 1.0 Objective-C is fully supported.
C Cod Examples
Hello World
#!/usr/bin/ccod
<?
printf("Hello World\n");
?>
Loops
#!/usr/bin/ccod
<?
int i;
for (i=0;i<25;i++)
{
printf("Count %i\n", i);
}
?>
#!/usr/bin/ccod
<?
for ( int i=0;i<25;i++)
{
printf("Count %i\n", i);
}
?>
#!/usr/bin/ccod
<?
int i = 0;
do
{
printf("Count %i\n", i);
i++;
} while (i<25)
?>
Inline Text
#!/usr/bin/ccod
<?
?>
Hello World
Non-Script Mode
#!/usr/bin/ccod
#pragma CCOD:script no
int main()
{
printf("Non-Script Example.\n");
return 0;
}
Header Section
Inline Functions
#!/usr/bin/ccod
<?
printf( "Inline Function Example\n");
int whatisawren()
{
printf("A Bird.\n");
}
printf( "What is a Wren?\n");
whatisawren();
?>
C++ and Classes
#!/usr/bin/ccod
<?#
#include <iostream>
#include <stdio.h>
using namespace std;
class MyLilClass
{
public:
char name[50];
int age;
};
#?>
<?
MyLilClass *iam = new MyLilClass();
sprintf(iam->name, "John Denton");
iam->age = 46;
cout << "Hi my name is " << iam->name << "." << endl;
cout << "I'm " << iam->age << " years old." << endl;
delete iam;
return 0;
?>
Objective-C
#!/usr/bin/ccod
<?#
@interface HelloWorld : NSObject
{
sadfasd
}
@end
@implementation HelloWorld
- (void)hello
{
printf("Hello World\n");
}
@end
#?>
<?
HelloWorld *hw = [[HelloWorld alloc] init];
[hw hello];
?>
Gotos (Wheeeee!)
#!/usr/bin/ccod
<?
goto l__movefar;
l__moveclose:
printf("Now I am here.\n");
return 0;
?>
This is some inline text that will never be reached!
<?
l__movefar:
printf("Here I am.\n");
goto l__moveclose;
?>
CSP Examples
Page Redirection
#!/usr/bin/ccod
<?
Response.Redirect("http://www.google.com");
?>
Print Server Variables
#!/usr/bin/ccod
<table border=1>
<?
for ( int i=0;i<Request.ServerVariables.length;i++)
{
?><tr><?
Response.Write("<td>%s</td>", Request.ServerVariables.Key(i));
Response.Write("<td>%s</td>", Request.ServerVariables.Value(i));
?></tr><?
}
?>
</table>
Output Buffering
#!/usr/bin/ccod
<?
Response.Write("This is text that will never appear ");
Response.Write("because the buffer that holds it is cleared.<br>");
Response.Clear();
Response.Write("Now this text is real!.<br>");
?>
SSI Includes
#!/usr/bin/ccod
<!--#include file="myfile.inc"-->
Forms
File uploads
#!/usr/bin/ccod
<form action=result.c method=POST ENCTYPE="multipart/form-data">
Field 1: <input type="text" name="field_1"><br>
Field 2: <input type="text" name="field_2"><br>
File 1: <input type="file" name="file_1"><br>
File 2: <input type="file" name="file_2"><br>
<input type=submit value="Submit Form"><br>
</form>
#!/usr/bin/ccod
<?
Response.Write("Field 1: %s<br><br>", Request.Form.Get("field_1"));
Response.Write("<b>All Fields Dumped</b><br>");
for (int i=0;i<Request.Form.Count;i++)
{
Response.Write("%s: %s<br>", Request.Form.Key(i), Request.Form.Value(i));
}
?>
Session Variables
#!/usr/bin/ccod
<?
Response.Write("Var1 : [%s]<br>", Session.Get("Var1"));
Session.Set("Var1", "MyVal");
Response.Write("Var1 : [%s]<br>", Session.Get("Var1"));
Session.Abandon();
Response.Write("Var1 : [%s]<br>", Session.Get("Var1"));
?>
Cookies
#!/usr/bin/ccod
<?
Response.Write("Var1 : [%s]<br>", Request.Cookies.Get("Var1"));
Response.Cookies.Set("Var1", "MyVal");
Response.Write("Var1 : [%s]<br>", Request.Cookies.Get("Var1"));
Response.Cookies.Set("Var1", "");
Response.Write("Var1 : [%s]<br>", Request.Cookies.Get("Var1"));
?>
Display Binary Data
#!/usr/bin/ccod
<?
unsigned char csppng[207] = {
0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A,0x00,0x00,0x00,0x0D,0x49,
0x48,0x44,0x52,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x13,0x08,0x03,
0x00,0x00,0x00,0xC6,0x4A,0xFF,0xC5,0x00,0x00,0x00,0x04,0x67,0x41,
0x4D,0x41,0x00,0x00,0xAF,0xC8,0x37,0x05,0x8A,0xE9,0x00,0x00,0x00,
0x19,0x74,0x45,0x58,0x74,0x53,0x6F,0x66,0x74,0x77,0x61,0x72,0x65,
0x00,0x41,0x64,0x6F,0x62,0x65,0x20,0x49,0x6D,0x61,0x67,0x65,0x52,
0x65,0x61,0x64,0x79,0x71,0xC9,0x65,0x3C,0x00,0x00,0x00,0x06,0x50,
0x4C,0x54,0x45,0x15,0x3C,0x35,0xFF,0xFF,0xE0,0xD8,0xEE,0x5C,0x14,
0x00,0x00,0x00,0x4F,0x49,0x44,0x41,0x54,0x78,0xDA,0xAC,0x91,0x41,
0x0A,0x00,0x20,0x08,0x04,0x77,0xFF,0xFF,0xE9,0xA8,0x48,0xD3,0x22,
0xA9,0xF4,0x10,0x63,0xCC,0x61,0x55,0x30,0x28,0xA4,0x08,0xA8,0xD5,
0x3B,0x4B,0xE8,0x02,0xB4,0x11,0x52,0x6C,0x02,0x97,0x47,0x71,0x08,
0x94,0xDF,0x09,0x5F,0x04,0x17,0xD2,0x0B,0xF4,0x21,0xE9,0x05,0xDA,
0x90,0xBC,0x11,0xB6,0x63,0xF2,0xB8,0xA8,0x70,0xD5,0xA9,0xD7,0xFC,
0x13,0x8A,0x00,0x03,0x00,0x29,0x98,0x01,0xD1,0xDA,0x7F,0x3C,0x6C,
0x00,0x00,0x00,0x00,0x49,0x45,0x4E,0x44,0xAE,0x42,0x60,0x82,
} ;
Response.SetContentType( "image/png");
Response.BinaryWrite(csppng, sizeof(csppng));
?>
Copyright @ 2004-2008 Joshua J Baker
|
|
|