Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C/C++

The Curl Programming Environment


Sep01: The Curl Programming Environment

Friedger is a Ph.D. student in computer science at the University of Bristol, England. He can be contacted at [email protected].


C url is a programming language designed for developing interactive Internet applications. As such, it combines the ease of markup languages with the functionality of object-oriented ones. More specifically, the Curl language aims to replicate capabilities of HTML, JavaScript, and Macromedia's Flash — all within a single language.

The Curl Content Language was first defined and developed by Tim Berners-Lee, Michael Dertouzos, and others at the Massachusetts Institute of Technology Lab of Computer Science. Berners-Lee and Dertouzos went on to establish Curl Corp. to launch a commercial implementation of the language. Although the language specification is currently proprietary, the company has promised to open source parts of Curl technology in the near future. In the meantime, the Curl development environment is freely available for personal use at http://www.curl.com/ (commercial applications require a license).

Curl-based development is done using a Surge Lab, an IDE that provides run-time libraries, dynamic native-code compiler, and renderer. Curl applications are delivered to clients in source-code form, then compiled on the client by Surge 1.0, a run-time environment distributed as a browser plug-in (which won't be open sourced) that lets users view Curl-based content. The idea here is that Internet downloads are dramatically improved since transferring source code takes less time than compiled executables. Interestingly, Surge 1.0 is licensed to commercial sites on a metered basis — individual users aren't charged for downloads, but the site is billed for the amount of downloaded data. Curl Surge 1.0 is currently available only for Windows, with MacOS X and Linux support supposedly forthcoming.

Curl technology is both a content- delivery format (like HTML) and programming language (like C++ or Java). With it, you can specify text formatting, layout, and object-oriented interactive behavior in a single, descriptive format to be interpreted and displayed by the Curl engine. As a language, the output of a Curl program defaults to a text document, making it easy to design a simple web page within seconds. In this sense, the language is comparable to text-formatting languages such as Tex or HTML. It even contains a standard document style that provides a table of contents constructed out of the section hierarchy of the page. In general, text attributes and all other functions are added within curly brackets in the manner of Lisp. The name of the function forms the first element followed by the parameters, everything put in curly brackets; for example, {bold Text} is shown as Text.

To extend the design of a flat page with, say, dynamic content, you don't need to learn a new language. Just about everything you've been doing with different languages can be done with Curl technology. This is because Curl has been designed from the ground up to let you gradually add more Curl-specific functionality. The language provides strongly typed, object-oriented programming constructs. Since applications can be programmed in the usual way, it is therefore easy, say, to build user interfaces that provide menus, buttons, text areas, and the like, and that can request data from web servers and adapt only parts of the page without rendering the entire page again. This results in decreased download times. Unlike HTML, a request for data can be raised by more than just a form. All objects on the page — from buttons to a single characters — are live and can initiate events or changes to other objects. This is possible because Curl is based on a compiler, not an interpreter. Again, the Curl code (which can be partly precompiled) is compiled at the client-side into executable programs.

The Surge Lab IDE appears to be a nice environment to develop Curl content pages. As Figure 1 shows, it comes with an editor, debugger, applet viewer, inspector for graphical objects, and release tool. Additionally, it includes a visual layout tool. The error messages clearly explain the cause of errors so that they can be easily corrected. Once written, the first applet can be integrated in an HTML page as an embedded object using a standard web design tool or it can be used on its own after the web server is taught to recognize the file extension ".curl" (see Listing One).

The Curl Programming Contest

Coinciding with the introduction of the language and IDE, Curl Corp. sponsored a programming contest (http://www.curl.com/html/developers/contestwelcome.jsp) that I, along with many others, entered. All in all, we had four weeks to learn a new language, come to grips with a new development environment, and implement our ideas. Entries were judged on programming style, originality — and (of course) implementation of Curl's unique features. Admittedly, most entries weren't highly sophisticated because of the limited time frame. Still they ranged from a "Hello World" document to a complete shopping-cart application. My entry, called "Moving Letter Box," was a graphic display showing characters of a string moving when the mouse is moved; see Figure 2. Users get an immediate response when they enter text or move the mouse. In total, this application (available electronically; see "Resource Center," page 5) packed a lot of Curl functionality into only 5 KB of code.

The goal of Moving Letter Box was to show how an applet is built in Curl and how object-oriented programming can lead to more efficient web sites. The box displays a colored string that follows the movements of the mouse pointer. Every letter of the string moves along its own path at its own pace. The component is built upon the Canvas class — a subclass of Box, the basic class of the Curl language and container for all graphical objects. It forms the output class of every document. The Canvas lets you specify its exact size and the exact placement of its children. To use these properties, all but the string parameter specified at the instantiation of LetterBox are passed through to the constructor of the Canvas class by the rest parameter, three dots (...) at the end of the parameter list. This lets you handle an unspecified number of parameters.

The string parameter instantiates a collection of letters. MovingLetter is a class derived from BasicTextFlowBox, where the text is limited to one single character. Additionally, it has a timer field and one for the coordinates of the position the letter should move to. The parent of the letter can enable the timer and set its interval via the public functions moving and set-timer. On every timer event, the letter places itself nearer to its destination. It has the full control of its movement. However, its parent (the letter box) determines speed, color, and destination. Speed and color are set as soon as the letter is created by the add-letters function. There, the variable mletter is of type #MovingLetter, where the # indicates that the variable might be empty (Null). Without the #, it has to be initialized at the same time as the declaration. The distinction between initialized and not initialized variables is part of the safe types system of the Curl language.

LetterBox is encapsulated in its own package that provides two public interface functions, one to the displayed string and the other an encapsulated call of the private function move-string-to that starts the movement of the string. The call parameters are the coordinates of the center of the moving letter box. The event handler for mouse movements in the letter box uses the same function calls. It ensures that the string follows the mouse pointer and that, in case the pointer moves out of the box, the string appears in the center of the box.

A rudimentary application of the letter box might be to provide a default string at the start, which then can be changed by the user. Since the change can be performed without interaction with the server, the response is immediate. This is possible because the editable TextField has its own event handler. As soon as the field is left or Enter is pressed, the string is set to its new value. Without further intervention, all old letters are freed and new ones are created under the control of the LetterBox. This underscores how easy it is for you to add new features. If properly encapsulated, you can also define new design languages as an extension of the Curl language, that can be applied without any programming knowledge. For example, this might be a language to design web sites in a specific layout or for electronic circuit simulations.

Extending the Example

To this point, the example application doesn't differ much from a conventional application. However, a slight variant of the program (see Listing Two) shows how the connection to the server enhances the possibilities. For instance, when a new string is entered in TextField, one out of six strings stored on a web server should be added to the user's text. The string is requested by the function get-web-string. Therefore, a random number between 1 and 6 is created (the language supports linear and additive congruent random-number generators), then a stream is opened for reading. The stream can be any resource the applet is allowed to access. For security reasons, ordinary applets can only access ftp-, http-, or https-servers. With Curl Surge, however, users can give additional privileges to applets from selected locations, such that those applets can read from the local filesystem. In this extended example, the applet attempts to access a web location that can handle requests. A PHP script (Listing Three) on the specified location returns just the requested element of an array of strings. On the client-side, just the first line of the stream is read, returned as the result, and the new dynamically created string is displayed. Only one string is transmitted from the server to the client to change the contents of the page. There is no need to send the entire rendered page again. In the meantime, users have the impression that they are using a program that behaves in the same way as a locally installed application, but works on data that come directly from the source (an online-shop with updated prices or an intranet application with company-specific information, for instance).

Of course, it is still necessary to write server-side scripts such as the small PHP script. But they don't have to handle the page rendering process. They can be restricted to data processing and providing a collection of XML data. The Curl engine comes with a package for parsing XML so working with these types of data is straightforward.

Conclusion

The example I present here illustrates how little time it takes to implement an interactive applet. The formatting features are integrated in the Curl language, and the language is easily extendible within the same framework (in contrast to Tcl/Tk where C has to be used). Plus, Curl is faster to learn and apply than, say, Shockwave Director.

However, Curl does have obstacles to overcome. First, issues like start-up time for the plug-in or missing functionality for databases will hopefully disappear in the next release. Secondly, the commercial meter-based license has been criticized because organizations using Curl technology get charged for the amount of data downloaded from their site. It is unclear whether customers of Curl Corp. will accept this, or if the company will have to modify or change its approach. Thirdly, as long as there is no concept of how web pages using Curl's technology can be found on the Internet, the Curl applets will always be just an embedded object in ordinary HTML pages. Still, this works fine and there is no reason why Curl Surge shouldn't be in general web use in the near future, in much the same way that Macromedia Flash is today.

DDJ

Listing One

<html>
<head>
<title> Moving Letter Box</title>
<meta name="author" content="Friedger Müffke">
</head>
<body>
<div align="center">
<embed src="start2.curl" width=500 height=400 pluginurl="http://www.curl.com">
</embed>
</div>
</body>
</html>

Back to Article

Listing Two

|| File: start2.curl
|| Contents:
||     This applet allows to enter a value to a TextField
||     The prompt and result will be shown with funny, jumping letters
|| Author: Friedger Mueffke
|| Version: 1.2

{curl 1.5 applet}

{import * from MUEFFKE.COMPONENTS.LETTERBOX,
    location="letterbox.curl"}

|| Create a document without margins and scrollbars
|| that it can neatly integrated in a HTML document
{document-style PlainDocument}


|| Define function that fetches a string from a web site
{define-proc {get-web-string}:String
    {let nr: int = {value
                         {let rand:LinearRandom={LinearRandom}}
                         {rand.next-in-range 0, 6}
                     },
        result: String ="Welcome"
    }

    {try
        let intext: TextInputStream =
            {read-open{url "txt.php3?nr=" & nr}}
        {set result={{intext.read-one-line}.to-String}}
         {intext.close}
     catch err: MissingFileException do
        {error "Cannot find that file. Sorry!"}
    }
    {return result}

}

|| Declare a global variable without instanziation
{let c:#LetterBox}

|| The output of this environement is its last line
{value
    {set
        || Define Canvas containing the letters
         c={LetterBox
                        "Enter your Name",
                        width=10cm,
                        height=5cm,
                        background="black"
                     }
    }
    {let
        || Define TextField to enter a value
        tf:TextField={TextField
                         {on ValueFinished do
                             {if (tf.value != null ) then
                                 {do
                                     {c.set-string ({get-web-string} & " " & tf.value)}
                                     {c.move-string-to-center}
                                 }
                             }
                         }
                     }
    }

    {c.add tf,
        x=1cm,
        y=1cm
    }

    c
}

|| This environment doesn't produce any output!
|| Resize the Letter Box to a size of 500 x 400 pixel
{do
    {set c.height=c.points-per-pixel*400}
    {set c.width=c.points-per-pixel*500}

}

Back to Article

Listing Three

<?
$strings = array("Welcome", "Willkommen", "Bien Venue", "Servus", 
                              "Bien Venidos", "Benvenuto","Bem Vindo");
print($strings[$nr]);
exit;
?>



Back to Article


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.