<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- http://noteslog.com/chili/ - Copyright 2010 / Andrea Ercolino -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

<title>TESTING CHILI 3.0 - Other Languages</title>

<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript" src="../jquery.chili.js"></script>


<script id="setup" type="text/javascript">
$.chili.options.dynamic.origin = "../recipes/";
$.chili.options.decoration.lineNumbers = true;

</script>

<script type="text/javascript" src="examples.js"></script>
<link type="text/css" rel="stylesheet" href="examples.css" />

</head>
<body>

<h2 id="title"></h2>

<h3 id="highlight" style="color:red; display:none;">
<a href="#">highlight</a> now
</h3>

<p>
This page has the following setup.
</p>
<div id="show_setup"></div>


<hr/>


<div class="section">
<h3>C++</h3>
<pre><code class="chili-lang-cplusplus">
/*
extracted from "Templates" 
(http://www.cplusplus.com/doc/tutorial/templates.html)
*/

// sequence template
#include &lt;iostream&gt;
using namespace std;

template &lt;class T, int N&gt;
class mysequence {
    T memblock [N];
  public:
    void setmember (int x, T value);
    T getmember (int x);
};

template &lt;class T, int N&gt;
void mysequence&lt;T,N&gt;::setmember (int x, T value) {
  memblock[x]=value;
}

template &lt;class T, int N&gt;
T mysequence&lt;T,N&gt;::getmember (int x) {
  return memblock[x];
}

int main () {
  mysequence &lt;int,5&gt; myints;
  mysequence &lt;double,5&gt; myfloats;
  myints.setmember (0,100);
  myfloats.setmember (3,3.1416);
  cout &lt;&lt; myints.getmember(0) &lt;&lt; '\n';
  cout &lt;&lt; myfloats.getmember(3) &lt;&lt; '\n';
  return 0;
}
</code></pre>
</div>




<div class="section">
<h3>C#</h3>
<pre><code class="chili-lang-csharp">
/*
extracted from "Introducing Generics in the CLR" 
(http://msdn.microsoft.com/msdnmag/issues/03/09/NET/)
*/

using System;

// Definition of a node type for creating a linked list
class Node {
   Object  m_data;
   Node    m_next;

   public Node(Object data, Node next) {
      m_data = data;
      m_next = next;
   }

   // Access the data for the node
   public Object Data {
      get { return m_data; } 
   }

   // Access the next node
   public Node Next {
      get { return m_next; } 
   }

   // Get a string representation of the node
   public override String ToString() {
      return m_data.ToString();
   }            
}

// Code that uses the node type
class App {
   public static void Main() {

      // Create a linked list of integers
      Node head = new Node(5, null);
      head = new Node(10, head);
      head = new Node(15, head);

      // Sum-up integers by traversing linked list
      Int32 sum = 0;
      for (Node current = head; current != null;
         current = current.Next) {
         sum += (Int32) current.Data;
      }      

      // Output sum
      Console.WriteLine("Sum of nodes = {0}", sum);      
   }
}
</code></pre>
</div>




<div class="section">
<h3>Delphi</h3>
<pre><code class="chili-lang-delphi">
(*
extracted from "Avoiding memory leaks" 
(http://www.delphibasics.co.uk/Article.asp?Name=Memory)
*)

 unit MyClass;
 
 interface
 
 uses
   Classes;
 
 type
   TMyClass = class
   private
     fileData : TStringList;
   published
     Constructor Create(const fileName : string);
     Destructor  Destroy; override;
     procedure   PrintFile;
   end;
 
 implementation
 
 uses
   Printers, Dialogs, SysUtils;
 
 // Constructor - builds the class object
 constructor TMyClass.Create(const fileName: string);
 begin
   // Create the string list object used to hold the file contents
   fileData := TStringList.Create;
 
   // And load the file data into it
   try
     fileData.LoadFromFile(fileName);
   except
     ShowMessage('Error : '+fileName+' file not found.');
   end;
 end;
 
 // Destructor - frees up memory used by the class object
 destructor TMyClass.Destroy;
 begin
   // Free up the memory used by the string list object
   FreeAndNil(fileData);
 
   // Call the parent class destructor
   inherited;
 end;
 
 // Print the file passed to the constructor
 procedure TMyClass.PrintFile;
 var
   myFile   : TextFile;
   i        : Integer;
 
 begin
   // Open a printer file
   AssignPrn(myFile);
 
   // Now prepare to write to the printer
   ReWrite(myFile);
 
   // Write the lines of the file to the printer
   for i := 0 to fileData.Count-1 do
     WriteLn(myFile, fileData[i]);
 
   // Close the print file
   CloseFile(myFile);
 end;
 
 end.
</code></pre>
</div>




<div class="section">
<h3>Java</h3>
<pre><code class="chili-lang-java">
/*
extracted from "Reference Objects and Garbage Collection" 
(http://java.sun.com/developer/technicalArticles/ALT/RefObj/index.html)
*/

import java.awt.Graphics;
import java.awt.Image;
import java.applet.Applet;
import java.lang.ref.SoftReference;

public class DisplayImage extends Applet {

        SoftReference sr = null;

        public void init() {
            System.out.println("Initializing");
        }

        public void paint(Graphics g) {
            Image im = (
              sr == null) ? null : (Image)(
              sr.get());
            if (im == null) {
                System.out.println(
                "Fetching image");
                im = getImage(getCodeBase(),
                   "truck1.gif");
                sr = new SoftReference(im);
           }
           System.out.println("Painting");
           g.drawImage(im, 25, 25, this);
           im = null;  
        /* Clear the strong reference to the image */
        }

        public void start() {
            System.out.println("Starting");
        }

        public void stop() {
            System.out.println("Stopping");
        }

}
</code></pre>
</div>




<div class="section">
<h3>LotusScript</h3>
<pre><code class="chili-lang-lotusscript">
%REM
extracted from "OLE constants" 
(http://noteslog.com/post/ole-constants/)
%END REM

'Install tlbinf32.dll: 
 
Option Public 
Option Declare 
 
Use "RegistryAccess" 
 
Sub Initialize 
%INCLUDE "error_handling" 
 
    Dim tli As Variant 
    On Error Resume Next 
    Set tli = CreateObject( "TLI.TLIApplication" ) 
    On Error Goto HandleError 
    If Err = 0 Then 
        Set tli = Nothing 
        Exit Sub 
    End If 
 
    Dim instalar As String 
    instalar = "tlbinf32.dll" 
 
    Dim s As New NotesSession 
    Dim db As NotesDatabase 
    Set db = s.CurrentDatabase 
 
    Dim d As notesdocument 
    Set d = GetHelpAboutDocument( db, instalar ) 
    If d Is Nothing Then 
        Msgbox "The library " &amp; instalar &amp; " has not been installed" &amp; Chr( 10 ) _ 
        &amp; "The library could not be found in the database" &amp; Chr( 10 ) _ 
        &amp; "Please notify your admin" 
        Exit Sub 
    End If 
 
    Dim systemRoot As String 
    systemRoot = RegQueryValue( "HKEY_LOCAL_MACHINE", "SOFTWAREMicrosoftWindows NTCurrentVersion", "SystemRoot" ) 
 
    Dim path As String 
    path = systemRoot &amp; "system32" &amp; instalar 
 
    Call ExtractAttachment( d, instalar, path ) 
    If Dir( path ) = "" Then 
        Msgbox "The library " &amp; instalar &amp; " has not been installed" &amp; Chr( 10 ) _ 
        &amp; "The library could not be put in the folder " &amp; path &amp; Chr( 10 ) _ 
        &amp; "Please notify your admin" 
        Exit Sub 
    End If 
 
    If Shell( "regsvr32 /s " &amp; instalar ) &lt;&gt; 33 Then 
        Msgbox "The library " &amp; instalar &amp; " has not been installed" &amp; Chr( 10 ) _ 
        &amp; "The library could not be registered" &amp; Chr( 10 ) _ 
        &amp; "Please notify your admin" 
        Exit Sub 
    End If 
 
    Msgbox "The library " &amp; instalar &amp; " has been installed" 
 
    ' HKEY_CLASSES_ROOTCLSID{8B217746-717D-11CE-AB5B-D41203C10000}InprocServer32 
 
End Sub 
 
Function GetHelpAboutDocument( db As NotesDatabase, filename As String ) As NotesDocument 
%INCLUDE "error_handling" 
 
    Dim nc As NotesNoteCollection 
    Set nc = db.CreateNoteCollection( False ) 
    nc.SelectHelpAbout = True 
    Call nc.BuildCollection 
    Dim nid As String 
    nid = nc.GetFirstNoteId 
 
    If nid &lt;&gt; "" Then 
        Set GetHelpAboutDocument = db.GetDocumentByID( nid ) 
    Else 
        Set GetHelpAboutDocument = Nothing 
    End If 
End Function 
 
Sub ExtractAttachment( d As NotesDocument, filename As String, path As String ) 
%INCLUDE "error_handling" 
 
    If Not d.HasEmbedded Then Exit Sub 
 
    Dim embedded As NotesEmbeddedObject 
    Set embedded = d.GetAttachment( filename ) 
    If embedded Is Nothing Then Exit Sub 
 
    Call embedded.ExtractFile( path ) 
End Sub
</code></pre>
</div>




<div class="section">
<h3>MySQL</h3>
<pre><code class="chili-lang-mysql">
/* 
extracted from "Advanced MySQL user variable techniques" 
(http://www.xaprb.com/blog/2006/12/15/advanced-mysql-user-variable-techniques/)
*/

CREATE TABLE fruits (
  `type` varchar(10) NOT NULL,
  variety varchar(20) NOT NULL,
  price decimal(5,2) NOT NULL default 0,
  PRIMARY KEY  (`type`,variety)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

insert into fruits(`type`, variety, price) values
('apple',  'gala',       2.79),
('apple',  'fuji',       0.24),
('apple',  'limbertwig', 2.87),
('orange', 'valencia',   3.59),
('orange', 'navel',      9.36),
('pear',   'bradford',   6.05),
('pear',   'bartlett',   2.14),
('cherry', 'bing',       2.55),
('cherry', 'chelan',     6.33);


set @num := 0, @type := '';

select `type`, variety, price, @num
from fruits
where 2 >= greatest(
   @num := if(@type = `type`, @num + 1, 1),
   least(0, length(@type := `type`)));
</code></pre>
</div>

</body>
</html>
