Programming, using and understanding

Pike

by Fredrik Hübinette

Preface

This book was written with the intention of making anybody with a little programming experience able to use Pike. It should also be possible to gain a deep understanding of how Pike works and to some extent why it works the way it does from this book. It will teach you how to write your own extensions to Pike. I have been trying for years to get someone else to write this book, but since it seems impossible without paying a fortune for it I will have to do it myself. A big thanks goes to
Ian Carr-de Avelon and Henrik Wallin for helping me iron out some of the rough spots. The book assumes that you have programmed some other programming language before and that you have some experience of UNIX.

Table of contents

Preface
Table of contents
Introduction
Overview
The history of Pike
A comparison with other languages
What is Pike
How to read this manual
1 Getting started
1.1 Your first Pike program
1.2 Improving hello_world.pike
1.3 Further improvements
1.4 Control structures
1.5 Functions
1.6 True and false
1.7 Data Types
2 A more elaborate example
2.1 Taking care of input
2.1.1 add_record()
2.1.2 main()
2.2 Communicating with files
2.2.1 save()
2.2.2 load()
2.2.3 main() revisited
2.3 Completing the program
2.3.1 delete()
2.3.2 search()
2.3.3 main() again
2.4 Then what?
2.5 Simple exercises
3 Control Structures
3.1 Conditions
3.1.1 if
3.1.2 switch
3.2 Loops
3.2.1 while
3.2.2 for
3.2.3 do-while
3.2.4 foreach
3.3 Breaking out of loops
3.3.1 break
3.3.2 continue
3.3.3 return
3.4 Exercises
4 Data types
4.1 Basic types
4.1.1 int
4.1.2 float
4.1.3 string
4.2 Pointer types
4.2.1 array
4.2.2 mapping
4.2.3 multiset
4.2.4 program
4.2.5 object
4.2.6 function
4.3 Sharing data
4.4 Writing data types
5 Operators
5.1 Arithmetic operators
5.2 Comparison operators
5.3 Logical operators
5.4 Bitwise/set operators
5.5 Indexing
5.6 The assignment operators
5.7 The rest of the operators
5.8 Operator precedence
5.9 Operator functions
6 Object orientation
6.1 Terminology
6.2 The approach
6.3 How does this help?
6.4 Pike and object orientation
6.5 Inherit
6.6 Multiple inherit
6.7 Pike inherit compared to other languages
6.8 Modifiers
6.9 Operator overloading
6.10 Simple exercises
7 Miscellaneous functions
7.1 sscanf
7.2 catch & throw
7.3 gauge
7.4 typeof
8 Modules
8.1 How to use modules
8.2 Where do modules come from?
8.3 The . operator
8.4 How to write a module
8.5 Simple exercises
9 File I/O
9.1 File management - Stdio.File
9.2 Buffered file management - Stdio.FILE
9.3 Standard streams - Stdio.stdin, stdout and stderr
9.4 Listening to sockets - Stdio.Port
9.5 UDP socket and message management - Stdio.UDP
9.6 Terminal management - Stdio.Terminfo
9.6.1 Stdio.Terminfo.Termcap
9.6.2 Stdio.Terminfo.Terminfo
9.7 Simple input-by-prompt - Stdio.Readline
9.8 Other Stdio functions
9.9 A simple example
9.10 A more complex example - a simple WWW server
10 Threads
10.1 Starting a thread
10.2 Threads reference section
10.3 Threads example
11 Modules for specific data types
11.1 String
11.2 Array
12 Image
12.1 Image.Image
12.2 Image.Colortable
12.3 Image.Layer
12.4 Image.Font
12.5 Image.colortable
12.6 Image.Poly
12.7 Image.Color
12.7.1 Image.Color.Color
12.8 Image.X
12.9 Image.ANY
12.10 Image.AVS
12.11 Image.BMP
12.12 Image.GD
12.13 Image.GIF
12.14 Image.HRZ
12.15 Image.ILBM
12.16 Image.PCX
12.17 Image.PNG
12.18 Image.PNM
12.19 Image.PSD
12.20 Image.TGA
12.21 Image.XBM
12.22 Image.XCF
12.23 Image.XWD
12.24 Image.JPEG
12.25 Image.TIFF
12.26 Image.TTF
12.26.1 Image.TTF.Face
12.26.2 Image.TTF.FaceInstance
12.27 Image.XFace
13 Protocols
13.1 Protocols.HTTP
13.1.1 Protocols.HTTP.Query
13.2 Protocols.LysKOM
13.2.1 Protocols.LysKOM.Session
13.2.2 Protocols.LysKOM.Connection
13.2.3 Protocols.LysKOM.Request
13.2.3.1 Protocols.LysKOM.Request._Request
13.3 Protocols.DNS
13.3.1 Protocols.DNS.client
14 Other modules
14.1 System
14.2 Process
14.3 Regexp
14.4 Gmp
14.5 Gdbm
14.6 Getopt
14.7 Gz
14.8 Yp
14.9 ADT.Table
14.10 Yabu transaction database
14.10.1 The database
14.10.2 Tables
14.10.3 Transactions
14.11 MIME
14.11.1 Global functions
14.11.2 The MIME.Message class
14.11.2.1 Public fields
14.11.2.2 Public methods
14.12 Simulate
14.13 Mysql.mysql
14.14 The Pike Crypto Toolkit
14.14.1 Introduction
14.14.2 Block ciphers
14.14.3 Stream Ciphers
14.14.4 Hash Functions
14.14.5 Public key algorithms
14.14.6 Combining block cryptos
14.15 Locale.Gettext
14.16 Calendar
14.16.1 Calendar.time_unit
14.16.2 Calendar.Gregorian
14.16.2.1 Calendar.Gregorian.
14.16.2.2 Calendar.Gregorian.Year
14.16.2.3 Calendar.Gregorian.Stardate
14.16.2.3.1 Calendar.Gregorian.Stardate.TNGDate
14.17 Parser
14.18 Math
14.18.1 Math.Matrix
14.19 Calendar.time_unit
14.20 Calendar.Gregorian
14.20.1 Calendar.Gregorian.
14.20.2 Calendar.Gregorian.Year
14.20.3 Calendar.Gregorian.Stardate
14.20.3.1 Calendar.Gregorian.Stardate.TNGDate
14.21 Crypto.randomness
14.21.1 Crypto.randomness.pike_random
14.21.2 Crypto.randomness.arcfour_random
14.22 Geographical.Position
14.23 Geographical.Countries
14.24 Parser.SGML
14.25 Protocols.HTTP
14.25.1 Protocols.HTTP.Query
14.26 Protocols.LysKOM
14.26.1 Protocols.LysKOM.Session
14.26.2 Protocols.LysKOM.Connection
14.26.3 Protocols.LysKOM.Request
14.26.3.1 Protocols.LysKOM.Request._Request
14.27 Protocols.DNS
14.27.1 Protocols.DNS.client
15 The preprocessor
16 Builtin functions
17 Pike internals - how to extend Pike
17.1 The master object
17.2 Data types from the inside
17.2.1 Basic data types
17.2.2 struct svalue
17.2.3 struct pike_string
17.2.4 struct array
17.2.5 struct mapping
17.2.6 struct object
17.2.7 struct program
17.3 The interpreter
Appendix A Terms and jargon
Appendix B Register program
Appendix C Reserved words
Appendix D BNF for Pike
Appendix E How to install Pike
Appendix F How to convert from old versions of Pike
Appendix G Image.Layer modes
Appendix H Image.Color colors
Index

Introduction

This introduction will give you some background about Pike and this book and also compare Pike with other languages. If you want to start learning Pike immediately you can skip this chapter.

Overview

This book is designed for people who want to learn Pike fast. Since Pike is a simple language to learn, especially if you have some prior programming experience, this should benefit most people.

Chapter one is devoted to background information about Pike and this book. It is not really necessary to read this chapter to learn how to use and program Pike, but it might help explain why some things work the way they do. It might be more interesting to re-read the chapter after you have learned the basics of Pike programming. Chapter two is where the action starts. It is a crash course in Pike with examples and explanations of some of the basics. It explains the fundamentals of the Pike data types and control structures. The systematic documentation of all Pike capabilities starts in chapter three with a description of all control structures in Pike. It then continues with all the data types in chapter four and operators in chapter five. Chapter six deals with object orientation in Pike, which is slightly different than what you might be used to.

The history of Pike

In the beginning, there was Zork. Then a bunch of people decided to make multi-player adventure games. One of those people was Lars Pensjö at the Chalmers university in Gothenburg, Sweden. For his game he needed a simple, memory-efficient language, and thus LPC (Lars Pensjö C) was born. About a year later I started playing one of these games and found that the language was the most easy-to-use language I had ever encountered. I liked the language so much that I started improving it and before long I had made my own LPC dialect called LPC4. LPC4 was still geared towards writing adventure games, but was quite useful for writing other things with as well. A major problem with LPC4 was the copyright. Since it was based on Lars Pensjö's code, it came with a license that did not allow it to be used for commercial gain. So, in 1994 I started writing µLPC, which was a new but similar LPC interpreter. I got financial backing from Signum Support AB for writing µLPC. Signum is a company dedicated to supporting GNU and GPL software and they wanted to create more GPL software.

When µLPC became usable, InformationsVävarna AB started using it for their web-server. Before then, Roxen (then called Spinner) was non-commercial and written in LPC4. Then in 1996 I started working for InformationsVävarna developing µLPC for them. We also changed the name of µLPC to Pike to get a more commercially viable name.

A comparison with other languages

Python
Python is probably the language that is most like Pike. Pike is faster and has better object orientation. It also has a syntax similar to C++, which makes it more familiar for people who know C++. Python on the other hand, has a lot more libraries available.
C++
Pike's syntax is almost the same as for C++. A huge difference is that Pike is interpreted. This makes the code slower, but reduces compile times to almost nothing. For those few applications which require the speed of C or C++, it is often easier to write a Pike extension than to write the whole thing in C or C++.
Lisp and Scheme
Internally Pike has a lot in common with simple interpreted Lisp and Scheme implementations. They are all stack based, byte-compiled, interpreted languages. Pike is also a 'one-cell' language, just like Scheme.
Pascal
Pike has nothing in common with Pascal.
Tcl/Tk
Pike is similar to Tcl/Tk in intent and they both have good string handling. Pike has better data types and is much faster however. On the other hand Tcl/Tk has X windows system support.

What is Pike

Pike is: Pike has:

How to read this manual

This manual uses a couple of different typefaces to describe different things:
italics
Italics is used as a placeholder for other things. If it says a word in the text it means that you should put your own word there.
bold
Bold is just used to emphasize that this word is not merely what it sounds like. It is actually a term.
fixed size
Fixed size is used to for examples and text directly from the computer.
Also, please beware that the word program is also a builtin Pike data type.

Chapter 1, Getting started

First you need to have Pike installed on your computer. See appendix E "How to install Pike" if this is not already done. It is also vital for the first of the following examples that the Pike binary is in your UNIX search path. If you have problems with this, consult the manual for your shell or go buy a beginners book about UNIX.

1.1 Your first Pike program

int main()
{
    write("hello world\n");
    return 0;
}
Let's call this file hello_world.pike, and then we try to run it:

	$ pike hello_world.pike
	hello world
	$ 
Pretty simple, Let's see what everything means:
int main()
This begins the function main. Before the function name the type of value it returns is declared, in this case int which is the name of the integer number type in Pike. The empty space between the parenthesis indicates that this function takes no arguments. A Pike program has to contain at least one function, the main function. This function is where program execution starts and thus the function from which every other function is called, directly or indirectly. We can say that this function is called by the operating system. Pike is, as many other programming languages, built upon the concept of functions, i.e. what the program does is separated into small portions, or functions, each performing one (perhaps very complex) task. A function declaration consists of certain essential components; the type of the value it will return, the name of the function, the parameters, if any, it takes and the body of the function. A function is also a part of something greater; an object. You can program in Pike without caring about objects, but the programs you write will in fact be objects themselves anyway. Now let's examine the body of main;

{
    write("hello world\n");
    return 0;
}
Within the function body, programming instructions, statements, are grouped together in blocks. A block is a series of statements placed between curly brackets. Every statement has to end in a semicolon. This group of statements will be executed every time the function is called.

write("hello world\n");
The first statement is a call to the builtin function write. This will execute the code in the function write with the arguments as input data. In this case, the constant string hello world\n is sent. Well, not quite. The \n combination corresponds to the newline character. write then writes this string to stdout when executed. Stdout is the standard Unix output channel, usually the screen.

return 0;
This statement exits the function and returns the value zero. Any statements following the return statements will not be executed.

1.2 Improving hello_world.pike

Typing pike hello_world.pike to run our program may seem a bit unpractical. Fortunately, Unix provides us with a way of automating this somewhat. If we modify hello_world.pike to look like this:

#!/usr/local/bin/pike

int main()
{
    write("hello world\n");
    return 0;
}
And then we tell UNIX that hello_world.pike is executable so we can run hello_world.pike without having to bother with running Pike:

	$ chmod +x hello_world.pike
	$ ./hello_world.pike
	hello world
	$ 
N.B.: The hash bang (#!) must be first in the file, not even whitespace is allowed to precede it! The file name after the hash bang must also be the complete file name to the Pike binary, and it may not exceed 30 characters.

1.3 Further improvements

Now, wouldn't it be nice if it said Hello world! instead of hello world ? But of course we don't want to make our program "incompatible" with the old version. Someone might need the program to work like it used to. Therefore we'll add a command line option that will make it type the old hello world. We also have to give the program the ability to choose what it should output based on the command line option. This is what it could look like:

#!/usr/local/bin/pike

int main(int argc, array(string) argv)
{
    if(argc > 1 && argv[1]=="--traditional")
    {
        write("hello world\n"); // old style
    }else{
        write("Hello world!\n"); // new style
    }
    return 0;
}
Let's run it:

	$ chmod +x hello_world.pike
	$ ./hello_world.pike
	Hello world!
	$ ./hello_world.pike --traditional
	hello world
	$ 
What is new in this version, then?
int main(int argc, array(string) argv)
In this version the space between the parenthesis has been filled. What it means is that main now takes two arguments. One is called argc, and is of the type int. The other is called argv and is a an array of strings.

The arguments to main are taken from the command line when the Pike program is executed. The first argument, argc, is how many words were written on the command line (including the command itself) and argv is an array formed by these words.

if(argc > 1 && argv[1] == "--traditional")
{
    write("hello world\n"); // old style
}else{
    write("Hello world!\n"); // new style
}
This is an if-else statement, it will execute what's between the first set of brackets if the expression between the parenthesis evaluate to something other than zero. Otherwise what's between the second set of brackets will be executed. Let's look at that expression:

argc > 1 && argv[1] == "--traditional"
Loosely translated, this means: argc is greater than one, and the second element in the array argv is equal to the string --traditional. Since argc is the number of words on the command line the first part is true only if there was anything after the program invocation.

Also note the comments:

write("hello world\n"); // old style
The // begins a comment which continues to the end of the line. Comments will be ignored by the computer when it reads the code. This allows to inform whoever might read your code (like yourself) of what the program does to make it easier to understand. Comments are also allowed to look like C-style comments, i.e. /* ... */, which can extend over several lines. The // comment only extends to the end of the line.

1.4 Control structures

The first thing to understand about Pike is that just like any other programming language it executes one piece of code at a time. Most of the time it simply executes code line by line working its way downwards. Just executing a long list of instructions is not enough to make an interesting program however. Therefore we have control structures to make Pike execute pieces of code in more interesting orders than from top to bottom.

We have already seen an example of the if statement:

if( expression )
    statement1;
else
    statement2;
if simply evaluates the expression and if the result is true it executes statement1, otherwise it executes statement2. If you have no need for statement2 you can leave out the whole else part like this:
if( expression )
    statement1;
In this case statement1 is evaluated if expression is true, otherwise nothing is evaluated.

Note for beginners: go back to our first example and make sure you understand what if does.

Another very simple control structure is the while statement:

while( expression )
    statement;
This statement evaluates expression and if it is found to be true it evaluates statement. After that it starts over and evaluates expression again. This continues until expression is no longer true. This type of control structure is called a loop and is fundamental to all interesting programming.

1.5 Functions

Another control structure we have already seen is the function. A function is simply a block of Pike code that can be executed with different arguments from different places in the program. A function is declared like this:
modifiers type name(type varname1, type varname2, ...)
{
    statements
}
The modifiers are optional. See
section 6.8 "Modifiers" for more details about modifiers. The type specifies what kind of data the function returns. For example, the word int would signify that the function returns an integer number. The name is used to identify the function when calling it. The names between the parenthesis are the arguments to the function. They will be defined as local variables inside the function. Each variable will be declared to contain values of the preceding type. The three dots signifies that you can have anything from zero to 256 arguments to a function. The statements between the brackets are the function body. Those statements will be executed whenever the function is called.

Example:

int sqr(int x) { return x*x; }
This line defines a function called sqr to take one argument of the type int and also returns an int. The code itself returns the argument multiplied by itself. To call this function from somewhere in the code you could simply put: sqr(17) and that would return the integer value 289.

As the example above shows, return is used to specify the return value of a function. The value after return must be of the type specified before the function name. If the function is specified to return void, nothing at all should be written after return. Note that when a return statement is executed, the function will finish immediately. Any statements following the return will be ignored.

There are many more control structures, they will all be described in a later chapter devoted only to control structures.

1.6 True and false

Throughout this chapter the words true and false have been used without any explanation to what they mean. Pike has a fairly simple way of looking at this. The number 0 is false and everything else is true. (Except when using operator overloading as I will explain in a later chapter.)

1.7 Data Types

As you saw in our first examples we have to indicate the type of value returned by a function or contained in a variable. We used integers (int), strings (string), and arrays (with the * notation). The others are mapping, mixed, void, float, multiset, function, object and program. Neither mixed nor void are really types, void signifies that no value should be returned and mixed that the return value can be of any type, or that the variable can contain any type of value. Function, object and program are all types related to object orientation. We will not discuss the last three in any great detail here,
Int
The integer type stores a signed integer. It is 32 bit or 64 depending on architecture.
Float
This variable type stores a floating point number.
Array
Arrays are basically a place to store a number of other values. Arrays in Pike are allocated blocks of values. They are dynamically allocated and do not need to be declared as in C. The values in the array can be set when creating the array like this:
arr=({1,2,3});
Or, if you have already created an array, you can change the values in the array like this:
arr [ ind ] = data;
This sets entry number ind in the array arr to data. ind must be an integer. The first index of an array is 0 (zero). A negative index will count from the end of the array rather than from the beginning, -1 being the last element. To declare that a variable is an array we simply type array in front of the variable name we want:
array i;
We can also declare several array variables on the same line:
array i, j;
If we want to specify that the variable should hold an array of strings, we would write:
array (string) i;
String
A string contains a sequence of characters, a text, i.e. a word, a sentence or a book. Note that this is not simply the letters A to Z; special characters, null characters, newlines and so on can all be stored in a string. Any 8-bit character is allowed. String is a basic type in Pike, it is not an array of char like it is in C. This means that you cannot assign new values to individual characters in a string. Also, all strings are "shared", i.e. if the same string is used in several places, it will be stored in memory only once. When writing a string in a program, you enclose it in double quotes. To write special characters you need to use the following syntax:
\nnewline
\rcarriage return
\ttab
\bbackspace
\"" (quotation character)
\\\ (literal backslash)
Mapping
A mapping is basically an array that can be indexed on any type, not just integers. It can also be seen as a way of linking data (usually strings) together. It consists of a lot of index-data pairs which are linked together in such a way that map[index1] returns data1. A mapping can be created in a way similar to arrays:
mapping(string:string) map=(["five":"good", "ten":"excellent"]);
You can also set that data by writing map["five"]="good". If you try to set an index in a mapping that isn't already present in the mapping it will be added as well.
Multiset
A multiset is basically a mapping without data values. When referring to an index in the multiset a 1 (one) will be returned if the index is present and 0 (zero) otherwise.

Chapter 2, A more elaborate example

To illustrate several of the fundamental points of Pike we will now introduce an example program, that will be extended as we go. We will build a database program that keeps track of a record collection and the songs on the records. In the first version we hard-code our "database" into the program. The database is a mapping where the index is the record name and the data is an array of strings. The strings are of course the song names. The default register consists of one record.
#!/usr/local/bin/pike

mapping (string:array(string)) records =
([
    "Star Wars Trilogy" : ({
        "Fox Fanfare",
        "Main Title",
        "Princess Leia's Theme",
        "Here They Come",
        "The Asteroid Field",
        "Yoda's Theme",
        "The Imperial March",
        "Parade of the Ewoks",
        "Luke and Leia",
        "Fight with Tie Fighters",
        "Jabba the Hut",
        "Darth Vader's Death",
        "The Forest Battle",
        "Finale"
    })
]);
We want to be able to get a simple list of the records in our database. The function list_records just goes through the mapping records and puts the indices, i.e. the record names, in an array of strings, record_names. By using the builtin function sort we put the record names into the array in alphabetical order which might be a nice touch. For the printout we just print a header, "Records:", followed by a newline. Then we use the loop control structure for to traverse the array and print every item in it, including the number of the record, by counting up from zero to the last item of the array. The builtin function sizeof gives the number of items in an array. The printout is formatted through the use of sprintf which works more or less like the C function of the same name.
void list_records()
{
    int i;
    array (string) record_names=sort(indices(records));

    write("Records:\n");
    for(i=0;i<sizeof(record_names);i++)
        write(sprintf("%3d: %s\n", i+1, record_names[i]));
}
If the command line contained a number our program will find the record of that number and print its name along with the songs of this record. First we create the same array of record names as in the previous function, then we find the name of the record whose number (num) we gave as an argument to this function. Next we put the songs of this record in the array songs and print the record name followed by the songs, each song on a separate line.
void show_record(int num)
{
    int i;
    array (string) record_names = sort(indices (records));
    string name=record_names[num-1];
    array (string) songs=records[name];
    
    write(sprintf("Record %d, %s\n",num,name));
    for(i=0;i<sizeof(songs);i++)
        write(sprintf("%3d: %s\n", i+1, songs[i]));
}
The main function doesn't do much; it checks whether there was anything on the command line after the invocation. If this is not the case it calls the list_records function, otherwise it sends the given argument to the show_record function. When the called function is done the program just quits.
int main(int argc, array (string) argv)
{
    if(argc <= 1)
    {
        list_records();
    } else {
        show_record((int) argv[1]);
    }
}

2.1 Taking care of input

Now, it would be better and more general if we could enter more records into our database. Let's add such a function and modify the main() function to accept "commands".

2.1.1 add_record()

Using the method Stdio.Readline()->read() we wait for input which will be put into the variable record_name. The argument to ->read() is printed as a prompt in front of the user's input. Readline takes everything up to a newline character. Now we use the control structure while to check whether we should continue inputting songs. The while(1) means "loop forever", because 1 is always true. This program does not in fact loop forever, because it uses return to exit the function from within the loop when you type a period. When something has been read into the variable song it is checked. If it is a "." we return a null value that will be used in the while statement to indicate that it is not ok to continue asking for song names. If it is not a dot, the string will be added to the array of songs for this record, unless it's an empty string. Note the += operator. It is the same as saying records[record_name]=records[record_name]+({song}).
void add_record()
{
    string record_name=Stdio.Readline()->read("Record name: ");
    records[record_name]=({});
    write("Input song names, one per line. End with '.' on its own line.\n");
    while(1)
    {
        string song;
        song=Stdio.Readline()->read(sprintf("Song %2d: ",
                                                                sizeof(records[record_name])+1));
        if(song==".")
             return;
        if (strlen(song))
            records[record_name]+=({song});
    }
}

2.1.2 main()

The main function now does not care about any command line arguments. Instead we use Stdio.Readline()->read() to prompt the user for instructions and arguments. The available instructions are "add", "list" and "quit". What you enter into the variables cmd and args is checked in the switch() block. If you enter something that is not covered in any of the case statements the program just silently ignores it and asks for a new command. In a switch() the argument (in this case cmd) is checked in the case statements. The first case where the expression equals cmd then executes the statement after the colon. If no expression is equal, we just fall through without any action. The only command that takes an argument is "list" which works as in the first version of the program. If "list" receives an argument, that record is shown along with all the songs on it. If there is no argument it shows a list of the records in the database. When the program returns from either of the listing functions, the break instruction tells the program to jump out of the switch() block. "add" of course turns control over to the function described above. If the command given is "quit" the exit(0) statement stops the execution of the program and returns 0 (zero) to the operating system, telling it that everything was ok.
int main(int argc, array(string) argv)
{
    string cmd;
    while(cmd=Stdio.Readline()->read("Command: "))
    {
        string args;
        sscanf(cmd,"%s %s",cmd,args);

        switch(cmd)
        {
        case "list":
            if((int)args)
            {
                show_record((int)args);
            } else {
                list_records();
            }
            break;

        case "quit":
            exit(0);

        case "add":
            add_record();
            break;
        }
    }
}

2.2 Communicating with files

Now if we want to save the database and also be able to retrieve previously stored data we have to communicate with the environment, i.e. with files on disk. Now we will introduce you to programming with objects. To open a file for reading or writing we will use one of the programs which is builtin in Pike called Stdio.File. To Pike, a program is a data type which contains code, functions and variables. A program can be cloned which means that Pike creates a data area in memory for the program, places a reference to the program in the data area, and initializes it to act on the data in question. The methods (i.e. functions in the object) and variables in the object Stdio.File enable us to perform actions on the associated data file. The methods we need to use are open, read, write and close. See
chapter 9 "File I/O" for more details.

2.2.1 save()

First we clone a Stdio.File program to the object o. Then we use it to open the file whose name is given in the string file_name for writing. We use the fact that if there is an error during opening, open() will return a false value which we can detect and act upon by exiting. The arrow operator (->) is what you use to access methods and variables in an object. If there is no error we use yet another control structure, foreach, to go through the mapping records one record at a time. We precede record names with the string "Record: " and song names with "Song: ". We also put every entry, be it song or record, on its own line by adding a newline to everything we write to the file.
Finally, remember to close the file.
void save(string file_name)
{
    string name, song;
    Stdio.File o=Stdio.File();

    if(!o->open(file_name,"wct"))
    {
        write("Failed to open file.\n");
        return;
    }

    foreach(indices(records),name)
    {
        o->write("Record: "+name+"\n");
        foreach(records[name],song)
            o->write("Song: "+song+"\n");
    }

    o->close();
}

2.2.2 load()

The load function begins much the same, except we open the file named file for reading instead. When receiving data from the file we put it in the string file_contents. The absence of arguments to the method o->read means that the reading should not end until the end of the file. After having closed the file we initialize our database, i.e. the mapping records. Then we have to put file_contents into the mapping and we do this by splitting the string on newlines (cf. the split operator in Perl) using the division operator. Yes, that's right: by dividing one string with another we can obtain an array consisting of parts from the first. And by using a foreach statement we can take the string file_contents apart piece by piece, putting each piece back in its proper place in the mapping records.
void load(string file_name)
{
    string name="ERROR";
    string file_contents,line;

    Stdio.File o=Stdio.File();
    if(!o->open(file_name,"r"))
    {
        write("Failed to open file.\n");
        return;
    }

    file_contents=o->read();
    o->close();

    records=([]);

    foreach(file_contents/"\n",line)
    {
        string cmd, arg;
        if(sscanf(line,"%s: %s",cmd,arg))
        {
            switch(lower_case(cmd))
            {
            case "record":
                name=arg;
                records[name]=({});
                break;

             case "song":
                 records[name]+=({arg});
                 break;
            }
        }
    }
}

2.2.3 main() revisited

main() remains almost unchanged, except for the addition of two case statements with which we now can call the load and save functions. Note that you must provide a filename to load and save, respectively, otherwise they will return an error which will crash the program.
case "save":
    save(args);
    break;

case "load":
    load(args);
    break;

2.3 Completing the program

Now let's add the last functions we need to make this program useful: the ability to delete entries and search for songs.

2.3.1 delete()

If you sell one of your records it might be nice to able to delete that entry from the database. The delete function is quite simple. First we set up an array of record names (cf. the list_records function). Then we find the name of the record of the number num and use the builtin function m_delete() to remove that entry from records.
void delete_record(int num)
{
    array(string) record_names=sort(indices(records));
    string name=record_names[num-1];

    m_delete(records,name);
}

2.3.2 search()

Searching for songs is quite easy too. To count the number of hits we declare the variable hits. Note that it's not necessary to initialize variables, that is done automatically when the variable is declared if you do not do it explicitly. To be able to use the builtin function search(), which searches for the presence of a given string inside another, we put the search string in lowercase and compare it with the lowercase version of every song. The use of search() enables us to search for partial song titles as well. When a match is found it is immediately written to standard output with the record name followed by the name of the song where the search string was found and a newline. If there were no hits at all, the function prints out a message saying just that.
void find_song(string title)
{
    string name, song;
    int hits;

    title=lower_case(title);

    foreach(indices(records),name)
    {
        foreach(records[name],song)
        {
            if(search(lower_case(song), title) != -1)
            {
                write(name+"; "+song+"\n");
                hits++;
            }
        }
    }

    if(!hits) write("Not found.\n");
}

2.3.3 main() again

Once again main() is left unchanged, except for yet another two case statements used to call the search() and delete functions, respectively. Note that you must provide an argument to delete or it will not work properly.
case "delete":
    delete_record((int)args);
    break;

case "search":
    find_song(args);
    break;

2.4 Then what?

Well that's it! The example is now a complete working example of a Pike program. But of course there are plenty of details that we haven't attended to. Error checking is for example extremely sparse in our program. This is left for you to do as you continue to read this book. The complete listing of this example can be found in
appendix B "Register program". Read it, study it and enjoy!

2.5 Simple exercises

  • Make a program which writes hello world 10 times.
  • Modify hello_world.pike to write the first argument to the program.
  • Make a program that writes a hello_world program to stdout when executed.
  • Modify the register program to store data about programs and diskettes instead of songs and records.
  • Add code to the register program that checks that the user typed an argument when required. The program should notify the user and wait to receive more commands instead of exiting with an error message.
  • Add code to the register program to check that the arguments to show_record and delete_records are numbers. Also make sure that the number isn't less than one or bigger than the available number of records.
  • Rewrite the register program and put all the code in main().
  • Chapter 3, Control Structures

    In this chapter all the control structures in Pike will be explained. As mentioned earlier, control structures are used to control the flow of the program execution. Note that functions that make the program pause and simple function calls are not qualified as control structures.

    3.1 Conditions

    Pike only has two major condition control structures. We have already seen examples of both of them in Chapter two. But for completeness they will be described again in this chapter.

    3.1.1 if

    The simplest one is called the if statement. It can be written anywhere where a statement is expected and it looks like this:
    if( expression ) statement1; else statement2;
    Please note that there is no semicolon after the parenthesis or after the else. Step by step, if does the following:
    1. First it evaluates expression.
    2. If the result was false go to point 5.
    3. Execute statement1.
    4. Jump to point 6.
    5. Execute statement2.
    6. Done.
    This is actually more or less how the interpreter executes the if statement. In short, statement1 is executed if expression is true otherwise statement2 is executed. If you are interested in having something executed if the expression is false you can drop the whole else part like this:
    if( expression )
        statement1;
    If on the other hand you are not interested in evaluating something if the expression is false you should use the not operator to negate the true/false value of the expression. See chapter 5 for more information about the not operator. It would look like this:
    if( ! expression )
        statement2 ;
    Any of the statements here and in the rest of this chapter can also be a block of statements. A block is a list of statements, separated by semicolons and enclosed by brackets. Note that you should never put a semicolon after a block of statements. The example above would look like this;
    if ( ! expression )
    {
        statement;
        statement;
        statement;
    }

    3.1.2 switch

    A more sophisticated condition control structure is the switch statement. A switch lets you select one of many choices depending on the value of an expression and it can look something like this:
    switch ( expression )
    {
        case constant1:
            statement1;
            break;

        case constant2:
            statement2;
            break;

        case constant3 .. constant4:
            statement3;
            break;

        default:
            statement5;
    }
    As you can see, a switch statement is a bit more complicated than an if statement. It is still fairly simple however. It starts by evaluating the expression it then searches all the case statements in the following block. If one is found to be equal to the value returned by the expression, Pike will continue executing the code directly following that case statement. When a break is encountered Pike will skip the rest of the code in the switch block and continue executing after the block. Note that it is not strictly necessary to have a break before the next case statement. If there is no break before the next case statement Pike will simply continue executing and execute the code after that case statement as well.

    One of the case statements in the above example differs in that it is a range. In this case, any value between constant3 and constant4 will cause Pike to jump to statement3. Note that the ranges are inclusive, so the values constant3 and constant4 are also valid.

    3.2 Loops

    Loops are used to execute a piece of code more than once. Since this can be done in quite a few different ways there are four different loop control structures. They may all seem very similar, but using the right one at the right time makes the code a lot shorter and simpler.

    3.2.1 while

    While is the simplest of the loop control structures. It looks just like an if statement without the else part:
    while ( expression )
        statement;
    The difference in how it works isn't that big either, the statement is executed if the expression is true. Then the expression is evaluated again, and if it is true the statement is executed again. Then it evaluates the expression again and so forth... Here is an example of how it could be used:
    int e=1;
    while(e<5)
    {
        show_record(e);
        e=e+1;
    }
    This would call show_record with the values 1, 2, 3 and 4.

    3.2.2 for

    For is simply an extension of while. It provides an even shorter and more compact way of writing loops. The syntax looks like this:
    for ( initializer_statement ; expression ; incrementor_expression )
        statement ;
    For does the following steps:
    1. Executes the the initializer_statement. The initializer statement is executed only once and is most commonly used to initialize the loop variable.
    2. Evaluates expression
    3. If the result was false it exits the loop and continues with the program after the loop.
    4. Executes statement.
    5. Executes the increment_expression.
    6. Starts over from 2.
    This means that the example in the while section can be written like this:
    for(int e=1; e<5; e=e+1)
        show_record(e);

    3.2.3 do-while

    Sometimes it is unpractical that the expression is always evaluated before the first time the loop is executed. Quite often you want to execute something, and then do it over and over until some condition is satisfied. This is exactly when you should use the do-while statement.
    do
        statement;
    while ( expression );
    As usual, the statement can also be a block of statements, and then you do not need a semicolon after it. To clarify, this statement executes statement first, and then evaluates the expression. If the expression is true it executes the loop again. For instance, if you want to make a program that lets your modem dial your Internet provider, it could look something like this:
    do {
        modem->write("ATDT441-9109\n"); // Dial 441-9109
    } while(modem->gets()[..6]] != "CONNECT");
    This example assumes you have written something that can communicate with the modem by using the functions write and gets.

    3.2.4 foreach

    Foreach is unique in that it does not have an explicit test expression evaluated for each iteration in the loop. Instead, foreach executes the statement once for each element in an array. Foreach looks like this:
    foreach ( array_expression, variable )
        statement ;
    We have already seen an example of foreach in the find_song function in chapter 2. What foreach does is:
    1. It evaluates the array_expression which must return an array.
    2. If the array is empty, exit the loop.
    3. It then assigns the first element from the array to the variable.
    4. Then it executes the statement.
    5. If there are more elements in the array, the next one is assigned to the variable, otherwise exit the loop.
    6. Go to point 4.
    Foreach is not really necessary, but it is faster and clearer than doing the same thing with a for loop, as shown here:
    array tmp1= array_expression;
    for ( tmp2 = 0; tmp2 < sizeof(tmp1); tmp2++ )
    {
        variable = tmp1 [ tmp2 ];
        statement;
    }

    3.3 Breaking out of loops

    The loop control structures above are enough to solve any problem, but they are not enough to provide an easy solution to all problems. One thing that is still missing is the ability to exit a loop in the middle of it. There are three ways to do this:

    3.3.1 break

    break exits a loop or switch statement immediately and continues executing after the loop. Break can not be used outside of a loop or switch. It is quite useful in conjunction with while(1) to construct command parsing loops for instance:
    while(1)
    {
        string command=Stdio.Readline()->read("> ");
        if(command=="quit") break;
        do_command(command);
    }

    3.3.2 continue

    Continue does almost the same thing as break, except instead of breaking out of the loop it only breaks out of the loop body. It then continues to execute the next iteration in the loop. For a while loop, this means it jumps up to the top again. For a for loop, it jumps to the incrementor expression. For a do-while loop it jumps down to the expression at the end. To continue our example above, continue can be used like this:
    while(1)
    {
        string command=Stdio.Readline()->read("> ");
        if(strlen(command) == 0) continue;
        if(command=="quit") break;
        do_command(command);
    }
    This way, do_command will never be called with an empty string as argument.

    3.3.3 return

    Return doesn't just exit the loop, it exits the whole function. We have seen several examples how to use it chapter 2. None of the functions in chapter two returned anything in particular however. To do that you just put the return value right after return. Of course the type of the return value must match the type in the function declaration. If your function declaration is int main() the value after return must be an int. For instance, if we wanted to make a program that always returns an error code to the system, just like the UNIX command false this is how it would be done:
    #!/usr/local/bin/pike

    int main()
    {
        return 1;
    }
    This would return the error code 1 to the system when the program is run.

    3.4 Exercises

  • End all functions in the examples in chapter two with a return statement.
  • Change all foreach loops to for or while loops.
  • Make the find_song function in chapter 2 return when the first matching song is found.
  • Make the find_song function write the number of the record the song is on.
  • If you failed to get the program to work properly in the last exercise of chapter 2, try it again now.
  • Make a program that writes all the numbers from 1 to 1000.
  • Modify the program in the previous exercise to NOT write numbers divisible by 3, 7 or 17.
  • Make a program that writes all the prime numbers between 1 and 1000.
  • Chapter 4, Data types

    In this chapter we will discuss all the different ways to store data in Pike in detail. We have seen examples of many of these, but we haven't really gone into how they work. In this chapter we will also see which operators and functions work with the different types. There are two categories of data types in Pike: basic types, and pointer types. The difference is that basic types are copied when assigned to a variable. With pointer types, merely the pointer is copied, that way you get two variables pointing to the same thing.

    4.1 Basic types

    The basic types are int, float and string. For you who are accustomed to C or C++, it may seem odd that a string is a basic type as opposed to an array of char, but it is surprisingly easy to get used to.

    4.1.1 int

    Int is short for integer, or integer number. They are normally 32 bit integers, which means that they are in the range -2147483648 to 2147483647. Note that on some machines an int might be larger than 32 bits. Since they are integers, no decimals are allowed. An integer constant can be written in several ways:
    78 // decimal number
    0116 // octal number
    0x4e // hexadecimal number
    'N' // Ascii character
    All of the above represent the number 78. Octal notation means that each digit is worth 8 times as much as the one after. Hexadecimal notation means that each digit is worth 16 times as much as the one after. Hexadecimal notation uses the letters a, b, c, d, e and f to represent the numbers 10, 11, 12, 13, 14 and 15. The ASCII notation gives the ASCII value of the character between the single quotes. In this case the character is N which just happens to be 78 in ASCII.

    Integers are coded in 2-complement and overflows are silently ignored by Pike. This means that if your integers are 32-bit and you add 1 to the number 2147483647 you get the number -2147483648. This works exactly as in C or C++.

    All the arithmetic, bitwise and comparison operators can be used on integers. Also note these functions:

    int intp(mixed x)
    This function returns 1 if x is an int, 0 otherwise.
    int random(int x)
    This function returns a random number greater or equal to zero and smaller than x.
    int reverse(int x)
    This function reverses the order of the bits in x and returns the new number. It is not very useful.
    int sqrt(int x)
    This computes the square root of x. The value is always rounded down.

    4.1.2 float

    Although most programs only use integers, they are unpractical when doing trigonometric calculations, transformations or anything else where you need decimals. For this purpose you use float. Floats are normally 32 bit floating point numbers, which means that they can represent very large and very small numbers, but only with 9 accurate digits. To write a floating point constant, you just put in the decimals or write it in the exponential form:
    3.14159265358979323846264338327950288419716939937510 // Pi
    1.0e9 // A billion
    1.0e-9 // A billionth
    Of course you do not need this many decimals, but it doesn't hurt either. Usually digits after the ninth digit are ignored, but on some architectures float might have higher accuracy than that. In the exponential form, e means "times 10 to the power of", so 1.0e9 is equal to "1.0 times 10 to the power of 9".

    All the arithmetic and comparison operators can be used on floats. Also, these functions operates on floats:

    trigonometric functions
    The trigonometric functions are: sin, asin, cos, acos, tan and atan. If you do not know what these functions do you probably don't need them. Asin, acos and atan are of course short for arc sine, arc cosine and arc tangent. On a calculator they are often known as inverse sine, inverse cosine and inverse tangent.
    float log(float x)
    This function computes the natural logarithm of x,
    float exp(float x)
    This function computes e raised to the power of x.
    float pow(float|int x, float|int y)
    This function computes x raised to the power of y.
    float sqrt(float x)
    This computes the square root of x.
    float floor(float x)
    This function computes the largest integer value less than or equal to x. Note that the value is returned as a float, not an int.
    float ceil(float x),
    This function computes the smallest integer value greater than or equal to x and returns it as a float.
    float round(float x),
    This function computes the closest integer value to x and returns it as a float.

    4.1.3 string

    A string can be seen as an array of values from 0 to 2³²-1. Usually a string contains text such as a word, a sentence, a page or even a whole book. But it can also contain parts of a binary file, compressed data or other binary data. Strings in Pike are shared, which means that identical strings share the same memory space. This reduces memory usage very much for most applications and also speeds up string comparisons. We have already seen how to write a constant string:
    "hello world" // hello world
    "he" "llo" // hello
    "\116" // N (116 is the octal ASCII value for N)
    "\t" // A tab character
    "\n" // A newline character
    "\r" // A carriage return character
    "\b" // A backspace character
    "\0" // A null character
    "\"" // A double quote character
    "\\" // A singe backslash
    "\x4e" // N (4e is the hexadecimal ASCII value for N)
    "\d78" // N (78 is the decimal ACII value for N)
    "hello world\116\t\n\r\b\0\"\\" // All of the above
    "\xff" // the character 255
    "\xffff" // the character 65536
    "\xffffff" // the character 16777215
    "\116""3" // 'N' followed by a '3'
    As you can see, any sequence of characters within double quotes is a string. The backslash character is used to escape characters that are not allowed or impossible to type. As you can see, \t is the sequence to produce a tab character, \\ is used when you want one backslash and \" is used when you want a double quote (") to be a part of the string instead of ending it. Also, \XXX where XXX is an octal number from 0 to 37777777777 or \xXX where XX is 0 to ffffffff lets you write any character you want in the string, even null characters. From version 0.6.105, you may also use \dXXX where XXX is 0 to 2³²-1. If you write two constant strings after each other, they will be concatenated into one string.

    You might be surprised to see that individual characters can have values up to 2³²-1 and wonder how much memory that use. Do not worry, Pike automatically decides the proper amount of memory for a string, so all strings with character values in the range 0-255 will be stored with one byte per character. You should also beware that not all functions can handle strings which are not stored as one byte per character, so there are some limits to when this feature can be used.

    Although strings are a form of arrays, they are immutable. This means that there is no way to change an individual character within a string without creating a new string. This may seem strange, but keep in mind that strings are shared, so if you would change a character in the string "foo", you would change *all* "foo" everywhere in the program.

    However, the Pike compiler will allow you to to write code like you could change characters within strings, the following code is valid and works:

    string s="hello torld";
    s[6]='w';
    However, you should be aware that this does in fact create a new string and it may need to copy the string s to do so. This means that the above operation can be quite slow for large strings. You have been warned. Most of the time, you can use replace, sscanf, `/ or some other high-level string operation to avoid having to use the above construction too much.

    All the comparison operators plus the operators listed here can be used on strings:

    Summation
    Adding strings together will simply concatenate them. "foo"+"bar" becomes "foobar".
    Subtraction
    Subtracting one string from another will remove all occurrences of the second string from the first one. So "foobarfoogazonk" - "foo" results in "bargazonk".
    Indexing
    Indexing will let you get the ASCII value of any character in a string. The first index is zero.
    Range
    The range operator will let you copy any part of the string into a new string. Example: "foobar"[2..4] will return "oba".
    Division
    Division will let you divide a string at every occurrence of a word or character. For instance if you do "foobargazonk" / "o" the result would be ({"f","","bargaz","nk"}). It is also possible to divide the string into strings of length N by dividing the string by N. If N is converted to a float before dividing, the reminder of the division will be included in the result.
    Multiplication
    The inverse of the division operator can be accomplished by multiplying an array with a string. So if you evaluate ({"f","","bargaz","nk"}) * "o" the result would be "foobargazonk".
    Modulo
    To complement the division operator, you can do string % int. This operator will simply return the part of the string that was not included in the array returned by string / int

    Also, these functions operates on strings:

    string String.capitalize(string s)
    Returns s with the first character converted to upper case.
    int String.count(string haystack, string needle)
    Returns the number of occurances of needle in haystack. Equvivalent to sizeof(haystack/needle)-1.
    int String.width(string s)
    Returns the width s in bits (8, 16 or 32).
    string lower_case(string s)
    Returns s with all the upper case characters converted to lower case.
    string replace(string s, string from, string to)
    This function replaces all occurrences of the string from in s with to and returns the new string.
    string reverse(string s)
    This function returns a copy of s with the last byte from s first, the second last in second place and so on.
    int search(string haystack, string needle)
    This function finds the first occurrence of needle in haystack and returns where it found it.
    string sizeof(string s)
    Same as strlen(s), returns the length of the string.
    int stringp(mixed s)
    This function returns 1 if s is a string, 0 otherwise.
    int strlen(string s)
    Returns the length of the string s.
    string upper_case(string s)
    This function returns s with all lower case characters converted to upper case.

    4.2 Pointer types

    The basic types are, as the name implies, very basic. They are the foundation, most of the pointer types are merely interesting ways to store the basic types. The pointer types are array, mapping, multiset, program, object and function. They are all pointers which means that they point to something in memory. This "something" is freed when there are no more pointers to it. Assigning a variable with a value of a pointer type will not copy this "something" instead it will only generate a new reference to it. Special care sometimes has to be taken when giving one of these types as arguments to a function; the function can in fact modify the "something". If this effect is not wanted you have to explicitly copy the value. More about this will be explained later in this chapter.

    4.2.1 array

    Arrays are the simplest of the pointer types. An array is merely a block of memory with a fixed size containing a number of slots which can hold any type of value. These slots are called elements and are accessible through the index operator. To write a constant array you enclose the values you want in the array with ({ }) like this:
    ({ }) // Empty array
    ({ 1 }) // Array containing one element of type int
    ({ "" }) // Array containing a string
    ({ "", 1, 3.0 }) // Array of three elements, each of different type
    As you can see, each element in the array can contain any type of value. Indexing and ranges on arrays works just like on strings, except with arrays you can change values inside the array with the index operator. However, there is no way to change the size of the array, so if you want to append values to the end you still have to add it to another array which creates a new array. Figure 4.1 shows how the schematics of an array. As you can see, it is a very simple memory structure.


    fig 4.1

    Operators and functions usable with arrays:

    indexing ( arr [ c ] )
    Indexing an array retrieves or sets a given element in the array. The index c has to be an integer. To set an index, simply put the whole thing on the left side of an assignment, like this: arr [ c ] = new_value
    range ( arr [ from .. to ] )
    The range copies the elements from, from+1, , from+2 ... to into a new array. The new array will have the size to-from+1.
    comparing (a == b and a != b)
    The equal operator returns 1 if a and b are the same arrays. It is not enough that they have the same size and same data. They must be the same array. For example: ({1}) == ({1}) would return 0, while array(int) a=({1}); return a==a; would return 1. Note that you cannot use the operators >, >=, < or <= on arrays.
    Summation (a + b)
    As with strings, summation concatenates arrays. ({1})+({2}) returns ({1,2}).
    Subtractions (a - b)
    Subtracting one array from another returns a copy of a with all the elements that are also present in b removed. So ({1,3,8,3,2}) - ({3,1}) returns ({8,2}).
    Intersection (a & b)
    Intersection returns an array with all values that are present in both a and b. The order of the elements will be the same as the the order of the elements in a. Example: ({1,3,7,9,11,12}) & ({4,11,8,9,1}) will return: ({1,9,11}).
    Union (a | b)
    Union works almost as summation, but it only adds elements not already present in a. So, ({1,2,3}) | ({1,3,5}) will return ({1,2,3,5}). Note: the order of the elements in a can be changed!
    Xor (a ^ b)
    This is also called symmetric difference. It returns an array with all elements present in a or b but the element must NOT be present in both. Example: ({1,3,5,6}) ^ ({4,5,6,7}) will return ({1,3,4,7}).
    Division (a / b)
    This will split the array a into an array of arrays. If b is another array, a will be split at each occurance of that array. If b is an integer or float, a will be split between every bth element. Examples: ({1,2,3,4,5})/({2,3}) will return ({ ({1}), ({4,5}) }) and ({1,2,3,4})/2 will return ({ ({1,2}), ({3,4}) }).
    Modulo (a % b)
    This operation is valid only if b is an integer. It will return the part of the array that was not included by dividing a by b.
    array aggregate(mixed ... elems)
    This function does the same as the ({ }) operator; it creates an array from all arguments given to it. In fact, writing ({1,2,3}) is the same as writing aggregate(1,2,3).
    array allocate(int size)
    This function allocates a new array of size size. All the elements in the new array will be zeroes.
    int arrayp(mixed a)
    This function returns 1 if a is an array, 0 otherwise.
    array column(array(mixed) a, mixed ind)
    This function goes through the array a and indexes every element in it on ind and builds an array of the results. So if you have an array a in which each element is a also an array. This function will take a cross section, by picking out element ind from each of the arrays in a. Example: column( ({ ({1,2,3}), ({4,5,6}), ({7,8,9}) }), 2) will return ({3,6,9}).
    int equal(mixed a, mixed b)
    This function returns 1 if if a and b look the same. They do not have to be pointers to the same array, as long as they are the same size and contain equal data.
    array Array.filter(array a, mixed func, mixed ... args)
    filter returns every element in a for which func returns true when called with that element as first argument, and args for the second, third, etc. arguments.
    array Array.map(array a, mixed func, mixed ... args)
    This function works similar to Array.filter but returns the results of the function func instead of returning the elements from a for which func returns true.
    array replace(array a, mixed from, mixed to)
    This function will create a copy of a with all elements equal to from replaced by to.
    array reverse(array a)
    Reverse will create a copy of a with the last element first, the last but one second, and so on.
    array rows(array a, array indexes)
    This function is similar to column. It indexes a with each element from indexes and returns the results in an array. For example: rows( ({"a","b","c"}), ({ 2,1,2,0}) ) will return ({"c","b","c","a"}).
    int search(array haystack, mixed needle)
    This function returns the index of the first occurrence of an element equal (tested with ==) to needle in the array haystack.
    int sizeof(mixed arr)
    This function returns the number of elements in the array arr.
    array sort(array arr, array ... rest)
    This function sorts arr in smaller-to-larger order. Numbers, floats and strings can be sorted. If there are any additional arguments, they will be permutated in the same manner as arr. See chapter 16 "Builtin functions" for more details.
    array uniq(array a)
    This function returns a copy of the array a with all duplicate elements removed. Note that this function can return the elements in any order.

    4.2.2 mapping

    Mappings are are really just more generic arrays. However, they are slower and use more memory than arrays, so they cannot replace arrays completely. What makes mappings special is that they can be indexed on other things than integers. We can imagine that a mapping looks like this:


    fig 4.2

    Each index-value pair is floating around freely inside the mapping. There is exactly one value for each index. We also have a (magical) lookup function. This lookup function can find any index in the mapping very quickly. Now, if the mapping is called m and we index it like this: m [ i ] the lookup function will quickly find the index i in the mapping and return the corresponding value. If the index is not found, zero is returned instead. If we on the other hand assign an index in the mapping the value will instead be overwritten with the new value. If the index is not found when assigning, a new index-value pair will be added to the mapping. Writing a constant mapping is easy:

    ([ ]) // Empty mapping
    ([ 1:2 ]) // Mapping with one index-value pair, the 1 is the index
    ([ "one":1, "two":2 ]) // Mapping which maps words to numbers
    ([ 1:({2.0}), "":([]), ]) // Mapping with lots of different types

    As with arrays, mappings can contain any type. The main difference is that the index can be any type too. Also note that the index-value pairs in a mapping are not stored in a specific order. You can not refer to the fourteenth key-index pair, since there is no way of telling which one is the fourteenth. Because of this, you cannot use the range operator on mappings.

    The following operators and functions are important:

    indexing ( m [ ind ] )
    As discussed above, indexing is used to retrieve, store and add values to the mapping.
    addition, subtraction, union, intersection and xor
    All these operators works exactly as on arrays, with the difference that they operate on the indices. In those cases when the value can come from either mapping, it will be taken from the right side of the operator. This makes it easier to add new values to a mapping with +=. Some examples:
    ([1:3, 3:1]) + ([2:5, 3:7]) returns ([1:3, 2:5, 3:7 ])
    ([1:3, 3:1]) - ([2:5, 3:7]) returns ([1:3])
    ([1:3, 3:1]) | ([2:5, 3:7]) returns ([1:3, 2:5, 3:7 ])
    ([1:3, 3:1]) & ([2:5, 3:7]) returns ([3:7])
    ([1:3, 3:1]) ^ ([2:5, 3:7]) returns ([1:3, 2:5])
    same ( a == b )
    Returns 1 if a is the same mapping as b, 0 otherwise.
    not same ( a != b )
    Returns 0 if a is the same mapping as b, 1 otherwise.
    array indices(mapping m)
    Indices returns an array containing all the indices in the mapping m.
    mixed m_delete(mapping m, mixed ind)
    This function removes the index-value pair with the index ind from the mapping m. It will return the value that was removed.
    int mappingp(mixed m)
    This function returns 1 if m is a mapping, 0 otherwise.
    mapping mkmapping(array ind, array val)
    This function constructs a mapping from the two arrays ind and val. Element 0 in ind and element 0 in val becomes one index-value pair. Element 1 in ind and element 1 in val becomes another index-value pair, and so on..
    mapping replace(mapping m, mixed from, mixed to)
    This function creates a copy of the mapping m with all values equal to from replaced by to.
    mixed search(mapping m, mixed val)
    This function returns the index of the 'first' index-value pair which has the value val.
    int sizeof(mapping m)
    Sizeof returns how many index-value pairs there are in the mapping.
    array values(mapping m)
    This function does the same as indices, but returns an array with all the values instead. If indices and values are called on the same mapping after each other, without any other mapping operations in between, the returned arrays will be in the same order. They can in turn be used as arguments to mkmapping to rebuild the mapping m again.
    int zero_type(mixed t)
    When indexing a mapping and the index is not found, zero is returned. However, problems can arise if you have also stored zeroes in the mapping. This function allows you to see the difference between the two cases. If zero_type(m [ ind ]) returns 1, it means that the value was not present in the mapping. If the value was present in the mapping, zero_type will return something else than 1.

    4.2.3 multiset

    A multiset is almost the same thing as a mapping. The difference is that there are no values:


    fig 4.3

    Instead, the index operator will return 1 if the value was found in the multiset and 0 if it was not. When assigning an index to a multiset like this: mset[ ind ] = val the index ind will be added to the multiset mset if val is true. Otherwise ind will be removed from the multiset instead.

    Writing a constant multiset is similar to writing an array:

    (< >) // Empty multiset
    (< 17 >) // Multiset with one index: 17
    (< "", 1, 3.0, 1 >) // Multiset with 3 indices
    Note that you can actually have more than one of the same index in a multiset. This is normally not used, but can be practical at times.

    4.2.4 program

    Normally, when we say program we mean something we can execute from a shell prompt. However, Pike has another meaning for the same word. In Pike a program is the same as a class in C++. A program holds a table of what functions and variables are defined in that program. It also holds the code itself, debug information and references to other programs in the form of inherits. A program does not hold space to store any data however. All the information in a program is gathered when a file or string is run through the Pike compiler. The variable space needed to execute the code in the program is stored in an object which is the next data type we will discuss.


    fig 4.4
    Writing a program is easy, in fact, every example we have tried so far has been a program. To load such a program into memory, we can use compile_file which takes a file name, compiles the file and returns the compiled program. It could look something like this:
    program p = compile_file("hello_world.pike");
    You can also use the cast operator like this:
    program p = (program) "hello_world";
    This will also load the program hello_world.pike, the only difference is that it will cache the result so that next time you do (program)"hello_world" you will receive the _same_ program. If you call compile_file("hello_world.pike") repeatedly you will get a new program each time.

    There is also a way to write programs inside programs with the help of the class keyword:

    class class_name {
        inherits, variables and functions
    }
    The class keyword can be written as a separate entity outside of all functions, but it is also an expression which returns the program written between the brackets. The class_name is optional. If used you can later refer to that program by the name class_name. This is very similar to how classes are written in C++ and can be used in much the same way. It can also be used to create structs (or records if you program Pascal). Let's look at an example:
    class record {
        string title;
        string artist;
        array(string) songs;
    }

    array(record) records = ({});

    void add_empty_record()
    {
        records+=({ record() });
    }

    void show_record(record rec)
    {
        write("Record name: "+rec->title+"\n");
        write("Artist: "+rec->artist+"\n");
        write("Songs:\n");
        foreach(rec->songs, string song)
            write(" "+song+"\n");
    }
    This could be a small part of a better record register program. It is not a complete executable program in itself. In this example we create a program called record which has three identifiers. In add_empty_record a new object is created by calling record. This is called cloning and it allocates space to store the variables defined in the class record. Show_record takes one of the records created in add_empty_record and shows the contents of it. As you can see, the arrow operator is used to access the data allocated in add_empty_record. If you do not understand this section I suggest you go on and read the next section about objects and then come back and read this section again.

    cloning
    To create a data area for a program you need to instantiate or clone the program. This is accomplished by using a pointer to the program as if it was a function and call it. That creates a new object and calls the function create in the new object with the arguments. It is also possible to use the functions new() and clone() which do exactly the same thing except you can use a string to specify what program you want to clone.
    compiling
    All programs are generated by compiling a string. The string may of course be read from a file. For this purpose there are three functions:
    program compile(string p);
    program compile_file(string filename);
    program compile_string(string p, string filename);
    compile_file simply reads the file given as argument, compiles it and returns the resulting program. compile_string instead compiles whatever is in the string p. The second argument, filename, is only used in debug printouts when an error occurs in the newly made program. Both compile_file and compile_string calls compile to actually compile the string after calling cpp on it.
    casting
    Another way of compiling files to program is to use the cast operator. Casting a string to the type program calls a function in the master object which will compile the program in question for you. The master also keeps the program in a cache, so if you later need the same program again it will not be re-compiled.
    int programp(mixed p)
    This function returns 1 if p is a program, 0 otherwise.
    comparisons
    As with all data types == and != can be used to see if two programs are the same or not.

    The following operators and functions are important:

    cloning ( p ( args ) )
    Creates an object from a program. Discussed in the next section.
    indexing ( p [ string ], or p -> identifier )
    Retreives the value of the named constant from a program.
    array(string) indices(program p)
    Returns an array with the names of all non-static constants in the program.
    array(mixed) values(program p)
    Returns an array with the values of all non-static constants in the program.

    4.2.5 object

    Although programs are absolutely necessary for any application you might want to write, they are not enough. A program doesn't have anywhere to store data, it just merely outlines how to store data. To actually store the data you need an object. Objects are basically a chunk of memory with a reference to the program from which it was cloned. Many objects can be made from one program. The program outlines where in the object different variables are stored.

    fig 4.5
    Each object has its own set of variables, and when calling a function in that object, that function will operate on those variables. If we take a look at the short example in the section about programs, we see that it would be better to write it like this:
    class record {
        string title;
        string artist;
        array(string) songs;

        void show()
        {
            write("Record name: "+title+"\n");
            write("Artist: "+artist+"\n");
            write("Songs:\n");
            foreach(songs, string song)
                write(" "+song+"\n");
        }
    }

    array(record) records = ({});

    void add_empty_record()
    {
        records+=({ record() });
    }

    void show_record(object rec)
    {
        rec->show();
    }
    Here we can clearly see how the function show prints the contents of the variables in that object. In essence, instead of accessing the data in the object with the -> operator, we call a function in the object and have it write the information itself. This type of programming is very flexible, since we can later change how record stores its data, but we do not have to change anything outside of the record program.

    Functions and operators relevant to objects:

    indexing
    Objects can be indexed on strings to access identifiers. If the identifier is a variable, the value can also be set using indexing. If the identifier is a function, a pointer to that function will be returned. If the identifier is a constant, the value of that constant will be returned. Note that the -> operator is actually the same as indexing. This means that o->foo is the same as o["foo"]
    cloning
    As discussed in the section about programs, cloning a program can be done in two different ways:
    Whenever you clone an object, all the global variables will be initialized. After that the function create will be called with any arguments you call the program with.
    void destruct(object o)
    This function invalidates all references to the object o and frees all variables in that object. This function is also called when o runs out of references. If there is a function named destroy in the object, it will be called before the actual destruction of the object.
    array(string) indices(object o)
    This function returns a list of all identifiers in the object o.
    program object_program(object o)
    This function returns the program from which o was cloned.
    int objectp(mixed o)
    This function returns 1 if o is an object, 0 otherwise. Note that if o has been destructed, this function will return 0.
    object this_object()
    This function returns the object in which the interpreter is currently executing.
    array values(object o)
    This function returns the same as rows(o,indices(o)). That means it returns all the values of the identifiers in the object o.
    comparing
    As with all data types == and != can be used to check if two objects are the same or not.

    4.2.6 function

    When indexing an object on a string, and that string is the name of a function in the object a function is returned. Despite its name, a function is really a function pointer.

    fig 4.6
    When the function pointer is called, the interpreter sets this_object() to the object in which the function is located and proceeds to execute the function it points to. Also note that function pointers can be passed around just like any other data type:
    int foo() { return 1; }
    function bar() { return foo; }
    int gazonk() { return foo(); }
    int teleledningsanka() { return bar()(); }
    In this example, the function bar returns a pointer to the function foo. No indexing is necessary since the function foo is located in the same object. The function gazonk simply calls foo. However, note that the word foo in that function is an expression returning a function pointer that is then called. To further illustrate this, foo has been replaced by bar() in the function teleledningsanka.

    For convenience, there is also a simple way to write a function inside another function. To do this you use the lambda keyword. The syntax is the same as for a normal function, except you write lambda instead of the function name:

    lambda ( types ) { statements }
    The major difference is that this is an expression that can be used inside an other function. Example:
    function bar() { return lambda() { return 1; }; )
    This is the same as the first two lines in the previous example, the keyword lambda allows you to write the function inside bar.

    Note that unlike C++ and Java you can not use function overloading in Pike. This means that you cannot have one function called 'foo' which takes an integer argument and another function 'foo' which takes a float argument.

    This is what you can do with a function pointer.

    calling ( f ( mixed ... args ) )
    As mentioned earlier, all function pointers can be called. In this example the function f is called with the arguments args.
    string function_name(function f)
    This function returns the name of the function f is pointing at.
    object function_object(function f)
    This function returns the object the function f is located in.
    int functionp(mixed f)
    This function returns 1 if f is a function, 0 otherwise. If f is located in a destructed object, 0 is returned.
    function this_function()
    This function returns a pointer to the function it is called from. This is normally only used with lambda functions because they do not have a name.

    4.3 Sharing data

    As mentioned in the beginning of this chapter, the assignment operator (=) does not copy anything when you use it on a pointer type. Instead it just creates another reference to the memory object. In most situations this does not present a problem, and it speeds up Pike's performance. However, you must be aware of this when programming. This can be illustrated with an example:
    int main(int argc, array(string) argv)
    {
        array(string) tmp;
        tmp=argv;
        argv[0]="Hello world.\n";
        write(tmp[0]);
    }
    This program will of course write Hello world.

    Sometimes you want to create a copy of a mapping, array or object. To do so you simply call copy_value with whatever you want to copy as argument. Copy_value is recursive, which means that if you have an array containing arrays, copies will be made of all those arrays.

    If you don't want to copy recursively, or you know you don't have to copy recursively, you can use the plus operator instead. For instance, to create a copy of an array you simply add an empty array to it, like this: copy_of_arr = arr + ({}); If you need to copy a mapping you use an empty mapping, and for a multiset you use an empty multiset.

    4.4 Writing data types

    When declaring a variable, you also have to specify what type of variable it is. For most types, such as int and string this is very easy. But there are much more interesting ways to declare variables than that, let's look at a few examples:
    int x; // x is an integer
    int|string x; // x is a string or an integer
    array(string) x; // x is an array of strings
    array x; // x is an array of mixed
    mixed x; // x can be any type
    string *x; // x is an array of strings

    // x is a mapping from int to string
    mapping(string:int) x;

    // x implements Stdio.File
    Stdio.File x;

    // x implements Stdio.File
    object(Stdio.File) x;

    // x is a function that takes two integer
    // arguments and returns a string
    function(int,int:string) x;

    // x is a function taking any amount of
    // integer arguments and returns nothing.
    function(int...:void) x;

    // x is ... complicated
    mapping(string:function(string|int...:mapping(string:array(string)))) x;
    As you can see there are some interesting ways to specify types. Here is a list of what is possible:
    mixed
    This means that the variable can contain any type, or the function return any value
    array( type )
    This means an array of elements with the type type.
    type *
    This also means an array of elements with the type type.
    mapping( key type : value type )
    This is a mapping where the keys are of type key type and the values of value type.
    multiset ( type )
    This means a multiset containing values of the type type.
    object ( program )
    This means an object which 'implements' the specified program. The program can be a class, a constant, or a string. If the program is a string it will be casted to a program first. See the documentation for inherit for more information about this casting. The compiler will assume that any function or variable accessed in this object has the same type information as that function or variable has in program.
    program
    This too means 'an object which implements program'. program can be a class or a constant.
    function( argument types : return type )
    This is a function taking the specified arguments and returning return type. The argument types is a comma separated list of types that specify the arguments. The argument list can also end with ... to signify that there can be any amount of the last type.
    type1 | type2
    This means either type1 or type2
    void
    Void can only be used in certain places, if used as return type for a function it means that the function does not return a value. If used in the argument list for a function it means that that argument can be omitted. Example: function(int|void:void) this means a function that may or may not take an integer argument and does not return a value.

    Chapter 5, Operators

    To make it easier to write Pike, and to make the code somewhat shorter, some functions can be called with just one or two characters in the code. These functions are called operators and we have already seen how they work in plenty of examples. In this chapter I will describe in detail what they do. The operators are divided into categories depending on their function, but beware that some operators have meanings that go way beyond the scope of the category they are in.

    5.1 Arithmetic operators

    The arithmetic operators are the simplest ones, since they work just like you remember from math in school. The arithmetic operators are:

    Function Syntax Identifier Returns
    Addition a + b `+ the sum of a and b
    Subtraction a - b `- b subtracted from a
    Negation - a `- minus a
    Multiplication a * b `* a multiplied by b
    Division a / b `/ a divided by b
    Modulo a % b `% the remainder of a division between a and b

    The third column, "Identifier" is the name of the function that actually evaluates the operation. For instance, a + b can also be written as `+(a, b). I will show you how useful this can be at the end of this chapter.

    When applied to integers or floats these operators do exactly what they are supposed to do. The only operator in the list not known from basic math is the modulo operator. The modulo operator returns the remainder from an integer division. It is the same as calculating a - floor(a / b) * b. floor rounds the value down to closest lower integer value. Note that the call to floor isn't needed when operating on integers, since dividing two integers will return the result as an integer and it is always rounded down. For instance, 8 / 3 would return 2.

    If all arguments to the operator are integers, the result will also be an integer. If one is a float and the other is an integer, the result will be a float. If both arguments are float, the result will of course be a float.

    However, there are more types in Pike than integers and floats. Here is the complete list of combinations of types you can use with these operators:

    Operation Returned type Returned value
    int + int int the sum of the two values
    float + int
    int + float
    float + float
     float the sum of the two values
    string + string
    int + string
    float + string
    string + int
    string + float
     string In this case, any int or float is first converted to a string. Then the two strings are concatenated and the resulting string is returned.
    array + array array The two arrays are concatenated into a new array and that new array is returned.
    mapping + mapping mapping A mapping with all the index-value pairs from both mappings is returned. If an index is present in both mappings the index-value pair from the right mapping will be used.
    multiset + multiset multiset A multiset with all the indices from both multisets is returned.
    int - int int The right value subtracted from the left.
    float - int
    int - float
    float - float
     float The right value subtracted from the left.
    string - string string A copy of the left string with all occurrences of the right string removed.
    array - array array A copy of the left array with all elements present in the right array removed. Example: ({2,1,4,5,3,6,7}) - ({3,5,1}) will return ({2,4,6,7}).
    mapping - mapping mapping A new mapping with all index-value pairs from the left mapping, except those indices that are also present in the right mapping.
    multiset - multiset multiset A copy of the left multiset without any index present in the left multiset.
    int int Same as 0 - int.
    float float Same as 0 - float.
    int * int int the product of the two values
    float * int
    int * float
    float * float
     float the product of the two values
    array(string) * string string All the strings in the array are concatenated with the string on the right in between each string. Example: ({"foo","bar"})*"-" will return "foo-bar".
    array(array) * array array All the arrays in the left array are concatenated with the array on the right in between each array. Example: ({ ({"foo"}) ,({"bar"})})*({"-"}) will return ({ "foo","-","bar" }).
    string * int string This operation will concatenate the string N times. Example: "foo"*3 will return "foofoofoo".
    array * int string This operation will concatenate the array N times. Example: ({"foo"})*3 will return ({"foo","foo","foo"}).
    int / int int The right integer divided by the left integer rounded towards minus infinity.
    float / int
    int / float
    float / float
     float The right value divided by the left value.
    string / string array(string) In symmetry with the multiplication operator, the division operator can split a string into pieces. The right string will be split at every occurrence of the right string and an array containing the results will be returned. Example: "foo-bar"/"-" will return ({"foo","bar"})
    string / int array(string) This will split the string into pieces. The size of the pieces is given by the integer. Only complete pieces will be included in the result, the 'reminder' is discarded. Example: "foo-bar"/2 will return ({"fo","o-","ba"})
    string / float array(string) This is similar to dividing a string with an integer, but it allows fraction-sized segments and the reminder will always be included. Example: "foo-bar"/2.5 will return ({"fo","o-b","ar"})
    array / int array(array) This is similar to dividing a string with an integer, but splits an array. Example: ({1,2,3,4,5,6,7})/2 will return ({({1,2}),({3,4}),({5,6})})
    array / float array(array) You should be able to predict what this does by now. :) Example: ({1,2,3,4,5,6,7,8})/2.5 will return ({({1,2}),({3,4,5}),({6,7}),({8})})
    int % int int The remainder of a division. If a and b are integers, a%b is the same as a-(a/b)*b
    float % float
    int % float
    float % int
     float The remainder of a division. If a and b are floats, a%b is the same as a-floor(a/b)*b
    string % int string The remainder of a string division. Example: "foo-bar"%2 will return "r"
    array % int string The remainder of an array division. Example: ({1,2,3,4,5,6,7})%2 will return ({7})

    5.2 Comparison operators

    The arithmetic operators would be hard to use for anything interesting without the ability to compare the results to each other. For this purpose there are six comparison operators:

    Function Syntax Identifier Returns
    Same a == b `== 1 if a is the same value as b, 0 otherwise
    Not same a != b `!= 0 if a is the same value as b, 1 otherwise
    Greater than a > b `>  1 if a is greater than b, 0 otherwise
    Greater than or equal to a >= b `>= 1 if a is greater to or equal to b, 0 otherwise
    Lesser than a < b `<  1 if a is lesser than b, 0 otherwise
    Lesser than or equal to a <= b `<= 1 if a is lesser than or equal to b, 0 otherwise

    The == and != operators can be used on any type. For two values to be same they must be the same type. Therefore 1 and 1.0 are not same. Also, for two values of pointer types to be the same the two values must be pointers to the same object. It is not enough that the two objects are of the same size and contain the same data.

    The other operators in the table above can only be used with integers, floats and strings. If you compare an integer with a float, the int will be promoted to a float before the comparison. When comparing strings, lexical order is used and the values of the environment variables LC_CTYPE and LC_LANG are respected.

    5.3 Logical operators

    Logical operators are operators that operate with truth values. In Pike any value except zero is considered true. Logical operators are a very basic part of Pike. They can also decide which arguments to evaluate and which not to evaluate. Because of this the logical operators do not have any identifiers and can not be called as normal functions. There are four logical operators:

    Function Syntax Returns
    And a && b If a is false, a is returned and b is not evaluated. Otherwise, b is returned.
    Or a || b If a is true, a is returned and b is not evaluated. Otherwise, b is returned.
    Not ! a Returns 0 if a is true, 1 otherwise.
    If-else a ? b : c If a is true, b is returned and c is not evaluated. Otherwise c is returned and b is not evaluated.

    5.4 Bitwise/set operators

    These operators are used to manipulate bits as members in sets. They can also manipulate arrays, multisets and mappings as sets.

    Function Syntax Identifier Returns
    Shift left a << b `<< Multiplies a by 2 b times.
    Shift right a >> b `>> Divides a by 2 b times.
    Inverse (not) ~ a `~ Returns -1-a.
    Intersection (and) a & b `& All elements present in both a and b.
    Union (or) a | b `| All elements present in a or b.
    Symmetric difference (xor) a ^ b `^ All elements present in a or b, but not present in both.

    The first three operators can only be used with integers and should be pretty obvious. The other three, intersection, union and symmetric difference, can be used with integers, arrays, multisets and mappings. When used with integers, these operators considers each bit in the integer a separate element. If you do not know about how bits in integers work I suggest you go look it up in some other programming book or just don't use these operators on integers.

    When intersection, union or symmetric difference is used on an array each element in the array is considered by itself. So intersecting two arrays will result in an array with all elements that are present in both arrays. Example: ({7,6,4,3,2,1}) & ({1, 23, 5, 4, 7}) will return ({7,4,1}). The order of the elements in the returned array will always be taken from the left array. Elements in multisets are treated the same as elements in arrays. When doing a set operation on a mapping however, only the indices are considered. The values are just copied with the indices. If a particular index is present in both the right and left argument to a set operator, the one from the right side will be used. Example: ([1:2]) | ([1:3]) will return ([1:3]).

    5.5 Indexing

    The index and range operators are used to retrieve information from a complex data type.

    Function Syntax Identifier Returns
    Index a [ b ] `[] Returns the index b from a.
    Lookup a ->identifier `-> Looks up the identifier. Same as a["identifier"].
    Assign index a [ b ] = c `[]=; Sets the index b in a to c.
    Assign index a ->identifier = c `->= Sets the index "identifier" in a to c.
    Range a [ b .. c ] `[..] Returns a slice of a starting at the index b and ending at c.
    Range a [ .. c ] `[..] Returns a slice of a starting at the beginning of a and ending at c.
    Range a [ b .. ] `[..] Returns a slice of a from the index b to the end of a.

    The index operator can be written in two different ways. It can be written as ob [ index ] or ob->identifier. However, the latter syntax is equal to ob [ "identifier" ]. You can only index strings, arrays, mapping, multisets and objects, and some of these can only be indexed on certain things as shown in this list:

    Operation Returns
    string[int] Returns the ascii value of the Nth character in the string.
    array[int] Return the element in the array corresponding to the integer.
    array[int]=mixed Sets the element in the array to the mixed value.
    mapping[mixed]
    mapping->identifier
     Returns the value associated with the index, 0 if it is not found.
    mapping[mixed]=mixed
    mapping->identifier=mixed
     Associate the second mixed value with the first mixed value.
    multiset[mixed]
    multiset->identifier
     Returns 1 if the index (the value between the brackets) is present in the multiset, 0 otherwise.
    multiset[mixed]=mixed
    multiset->identifier=mixed
     If the mixed value is true the index is added to the multiset. Otherwise the index is removed from the multiset.
    object[string]
    object->identifier
     Returns the value of the named identifier in the object.
    object[string]=mixed
    object->identifier=mixed
     Set the given identifier in the object to the mixed value. Only works if the identifier references a variable in the object.
    program[string]
    program->identifier
     Returns the value of the named constant identifier in the program.
    string[int..int] Returns a piece of the string.
    array[int..int] Returns a slice of the array.

    When indexing an array or string it is sometimes convenient to access index from the end instead of from the beginning. This function can be performed by using a negative index. Thus arr[-i] is the same as arr[sizeof(arr)-i]. Note however that this behavior does not apply to the range operator. Instead the range operator clamps it's arguments to a suitable range. This means that a[b..c] will be treated as follows:

    5.6 The assignment operators

    There is really only one assignment operator, but it can be combined with lots of other operators to make the code shorter. An assignment looks like this:
    variable = expression;
    The variable can be a local variable, a global variable or an index in an array, object, multiset or mapping. This will of course set the value stored in variable to expression. Note that the above is also an expression which returns the value of the expression. This can be used in some interesting ways:
    variable1 = variable2 = 1; // Assign 1 to both variables
    variable1 =(variable2 = 1); // Same as above

    // Write the value of the expression, if any
    if(variable = expression)
        write(variable);
    Using assignments like this can however be confusing to novice users, or users who come from a Pascal or Basic background. Especially the if statement can be mistaken for if(variable == expression) which would mean something completely different. As I mentioned earlier, the assignment operator can be combined with another operator to form operators that modify the contents of a variable instead of just assigning it. Here is a list of all the combinations:

    Syntax Same as Function
    variable += expression  variable = variable + expression Add expression to variable
    variable -= expression  variable = variable - expression Subtract expression from variable
    variable *= expression  variable = variable * expression Multiply variable with expression
    variable /= expression  variable = variable / expression Divide variable by expression
    variable %= expression  variable = variable % expression Modulo variable by expression
    variable <<= expression  variable = variable << expression Shift variable expression bits left
    variable >>= expression  variable = variable >> expression Shift variable expression bits right
    variable |= expression  variable = variable | expression Or variable with expression
    variable &= expression  variable = variable & expression And variable with expression
    variable ^= expression  variable = variable ^ expression Xor variable with expression

    In all of the above expressions variable can actually be any of type of assignable values. Assignable values are also known as lvalues and here is a list of lvalues:

    Lvalue type Syntax Valid assignment type
    a local or global variable   identifier  same as variable
    an element in an array   array [ int ] any type
    elements in elements in an array   array [ string ] any type
    This is like map(arr, `[]=,string_indexing_element, assignment_element)
    an element in an string   string [ int ] integer
    an element in a mapping   mapping[mixed] or mapping->identifier  any type
    an element in a multiset   multiset[mixed] or multiset->identifier  true / false
    a variable in an object   object[string] or object->identifier  same type as named variable
    a list of lvalues   [ lvalue, lvalue ]   an array, first value in the array will be assigned to the first lvalue in the list, second value in the array to the second value in the list etc.

    5.7 The rest of the operators

    Now there are only a couple of operators left. I have grouped them together in this section, not because they are not important, but because they do not fit in any particular categories.

    Function Syntax Identifier Returns
           
    splice @ a none Sends each element in the array a as an individual argument to a function call.
    Increment ++ a none Increments a and returns the new value of a.
    Decrement -- a none Decrements a and returns the new value of a.
    Post increment a ++ none Increments a and returns the old value of a.
    Post decrement a -- none Decrements a and returns the old value of a.
    casting (type) a none Tries to convert a into a value of the specified type.
    Null a, b none Evaluates a and b, then returns b.

    The most important of these operators is the calling operator. It is used to call functions. The operator itself is just a set of parenthesis placed after the expression that returns the function. Any arguments to the function should be placed between the parenthesis, separated by commas. We have already seen many examples of this operator, although you might not have realized it was an operator at the time. The function call operator can do more than just calling functions though; if the 'function' is in fact an array, the operator will loop over the array and call each element in the array and returns an array with the results. If on the other hand, the 'function' is a program, the operator will clone an object from the program and call create() in the new object with the arguments given. In fact, the function clone is implemented like this:

    object clone(mixed p, mixed ... args) { ( (program)p )(@args); }

    On the subject of function calls, the splice operator should also be mentioned. The splice operator is an at sign in front of an expression. The expression should always be an array. The splice operator sends each of the elements in the array as a separate argument to the function call. The splice operator can only be used in an argument list for a function call.

    Then there are the increment and decrement operators. The increment and decrement operators are somewhat limited: they can only be used on integers. They provide a short and fast way to add or subtract one to an integer. If the operator is written before the variable (++a) the returned value will be what the variable is after the operator has added/subtracted one to it. If the operator is after the variable (a++) it will instead return the value of the variable before it was incremented/decremented.

    Casting is used to convert one type to another, not all casts are possible. Here is a list of all casts that actually _do_ anything:

    casting from to operation
    int string Convert the int to ASCII representation
    float string Convert the float to ASCII representation
    string int Convert decimal, octal or hexadecimal number to an int. Note that this will only work with decimal numbers in future versions.
    string float Convert ASCII number to a float.
    string program String is a filename, compile the file and return the program. Results are cached.
    string object This first casts the string to a program, (see above) and then clones the result. Results are cached.
    object type This calls the function 'cast' with a string containing the type as an argument.
    string array Same as doing values(string)
    array(int) string This does the inverse of the operation above. Ie. it constructs a string from an array of integers.
    array array(type) This recursively casts all values in the array to type.
    mapping array Same as Array.transpose(({indices(mapping),values(mapping)). Example: (array)([1:2,3:4]) will return ({ ({1,2}), ({3,4}) })
    multiset array Same as doing indices(multiset).
    int float Returns a float with the same value as the integer.
    float int Returns the integer closest to the float.
    function object Same as function_object(function).

    You can also use the cast operator to tell the compiler things. If a is a variable of type mixed containing an int, then the expression (int)a can be used instead of a and that will tell the compiler that the type of that expression is int.

    Last, and in some respect least, is the comma operator. It doesn't do much. In fact, it simply evaluates the two arguments and then returns the right hand one. This operator is mostly useful to produce smaller code, or to make defines that can be used in expressions.

    5.8 Operator precedence

    When evaluating an expression, you can always use parenthesis to tell the compiler in which order to evaluate things. Normally, the compiler will evaluate things from left to right, but it will evaluate operators with higher priority before those with lower. The following table shows the relative priority of all the operators in descending order:

    (a) a() a[b] a->b a[b..c] ({}) ([]) (<>)
    !a ~a (type)a ++a --a
    a++ a--
    a*b a/b a%b
    a+b a-b
    a>>b a<<b
    a>b a>=b a<b a<=b
    a==b a!=b
    a&b
    a^b
    a|b
    &&
    ||
    a?b:c
    =
    @a
    ,

    Examples:

    The expression is evaluated in this order:
    1+2*2   1+(2*2)
    1+2*2*4   1+((2*2)*4)
    (1+2)*2*4   ((1+2)*2)*4
    1+4,c=2|3+5   (1+4),(c=((2|3)+5))
    1+5 & 4 == 3   (1+(5 & 4)) == 3
    c=1,99   (c=1),99
    !a++ + ~--a()  (!(a++)) + (~((--a)()))

    5.9 Operator functions

    As mentioned earlier a + b can just as well be written as `+(a, b). Together with the function map which calls a function for every index in an array and the splice operator this can be used to create some very very fast and compact code. Let's look at some examples:
    map(arr, `-)
    This will return an array with each element negated.
    map(text/"\n",`/," ")
    This will divide a text into lines, each line will then be mapped through `/ and divided into an array of words.
    `+(0, @arr)
    This will add all the integers in the array arr together.
    int abs(int a) { return ( a>0 ? `+ : `-)(a); }
    This is a rather absurd but working function which will return the absolute value of a.

    Chapter 6, Object orientation

    As mentioned several times, Pike is object oriented. This does not mean that it is identical to C++ in any way. Pike uses a less strict approach to object orientation which creates a more relaxed programming style. If you have never come in contact with object oriented programming before, be warned that the ideas expressed in Pike and in this chapter are my own and do not necessarily reflect what other people think about object oriented programming.

    6.1 Terminology

    As mentioned before, Pike uses a different terminology than C++ does. This has historic reasons, but might be changed in the future. In the meantime, you might benefit from this mini-dictionary which translates Pike-ish terms to C++-ish terms:
    a class
    a class
    a clone
    an instance of a class
    to clone
    to create an instance
    an object
    an instance of a class
    a program
    a class

    6.2 The approach

    Think of the data type program as an executable file. Then we clone this program and create an object. The object is then a running program. The object has its own data and its own functions, however, it can work together with other programs by calling functions in those objects. The functions can be thought of as message carriers, TCP sockets or just a way for programs to communicate. Now we have a running system with many running programs, each performing only the task it was designed for.

    This analogy has one major flaw, when running programs in UNIX they actually run simultaneously. UNIX is multitasking, Pike is not. When one object is executing code, all the other objects has to wait until they are called. An exception is if you are using threads as will be discussed in a later chapter.

    6.3 How does this help?

    Ok, why is it a good idea to use object oriented programming? Well if you believe what you hear, the biggest advantage is that you can re-use your code in several projects. In my experience this is not the case.

    In my experience, the advantages of object oriented programming are:

    Modular programming made easy
    Using The approach makes it easy to divide a project into smaller pieces, these pieces are usually easier to write than the whole.
    Local data scope
    This is a very nifty thing with object oriented programs. If your program uses several files, windows, stacks, TCP connections or whatever, you simply write a program that handles one such file, window, stack or TCP connection. If correctly written, you can then just create many clones of that program.
    Using the same interface to different objects
    I can write a function that take a stream as an argument and writes data to this stream. Later I might wish to write this data to a window instead. I can then create an object that has the same methods as a stream (specifically the write method) and send that to the function that outputs the data.
    Most of these things can be done without object orientation, but it is object orientation that makes them easy.

    6.4 Pike and object orientation

    In most object oriented languages there is a way to write functions outside of all classes. Some readers might think this is what we have been doing until now. However, in Pike, all functions have to reside within a program. When you write a simple script in Pike, the file is first compiled into a program then cloned and then main() is called in that clone. All this is done by the master object, which is compiled and cloned before before all other objects. What the master object actually does is:
    program scriptclass=compile_file(argv[0]); // Load script
    object script=scriptclass(); // clone script
    int ret=script->main(sizeof(argv), argv); // call main()
    Similarly, if you want to load another file and call functions in it, you can do it with compile_file(), or you can use the cast operator and cast the filename to a string. You can also use the module system, which we will discuss further in the next chapter.

    If you don't want to put each program in a separate file, you can use the class keyword to write all your classes in one file. We have already seen an example how this in chapter 4 "Data types", but let's go over it in more detail. The syntax looks like this:

    class class_name {
        class_definition
    }
    This construction can be used almost anywhere within a normal program. It can be used outside all functions, but it can also be used as an expression in which case the defined class will be returned. In this case you may also leave out the class_name and leave the class unnamed. The class definition is simply the functions and programs you want to add to the class.

    To make it easier to program, defining a class is also to define a constant with that name. Essentially, these two lines of code do the same thing:

    class foo {};
    constant foo = class {};
    Because classes are defined as constants, it is possible to use a class defined inside classes you define later, like this:
    class foo
    {
        int test() { return 17; }
    };

    class bar
    {
        program test2() { return foo; }
    };

    6.5 Inherit

    A big part of writing object oriented code is the ability to add functionality to a program without changing (or even understanding) the original code. This is what inherit is all about. Let's say I want to change the hello_world program to write a version number before it writes hello world, using inherit I could do this like this:
    inherit "hello_world";
    int main(int argc, array(string) argv)
    {
        write("Hello world version 1.0\n");
        return ::main(argc,argv);
    }
    What inherit does is that it copies all the variables and functions from the inherited program into the current one. You can then re-define any function or variable you want, and you can call the original one by using a :: in front of the function name. The argument to inherit can be one of the following:
    A string
    This will have the same effect as casting the string to a program and then doing inherit on this program.
    A constant containing a program.
    Any constant from this program, module or inherited program that contains a program can be inherited.
    A class name
    A class defined with the class keyword is in fact added as a constant, so the same rule as above applies.

    Let's look at an example. We'll split up an earlier example into three parts and let each inherit the previous part. It would look something like this:


    Note that the actual code is not copied, only the list of references. Also note that the list of inherits is copied when you inherit a program. This does not mean you can access those copied inherits with the :: operator, it is merely an implementation detail. Although this example does not show an example of a re-defined function, it should be easy to see how that works by just changing what an identifier is pointing at.

    6.6 Multiple inherit

    You can inherit any number of programs in one program, you can even inherit the same thing more than once. If you do this you will get a separate set of functions and variables for each inherit. To access a specific function you need to name your inherits. Here is an example of named inherits:
    inherit Stdio.File; // This inherit is named File
    inherit Stdio.FILE; // This inherit is named FILE
    inherit "hello_word"; // This inherit is named hello_world
    inherit Stdio.File : test1; // This inherit is named test1
    inherit "hello_world" : test2; // This inherit is named test2

    void test()
    {
        File::read(); // Read data from the first inherit
        FILE::read(); // Read data from the second inherit
        hello_world::main(0,({})); // Call main in the third inherit
        test1::read(); // Read data from the fourth inherit
        test2::main(0,({})); // Call main in the fifth inherit
        ::read(); // Read data from all inherits
    }
    As you can see it would be impossible to separate the different read and main functions without using inherit names. If you tried calling just read without any :: or inherit name in front of it Pike will call the last read defined, in this case it will call read in the fourth inherit.

    If you leave the inherit name blank and just call ::read Pike will call all inherited read() functions. If there is more than one inherited read function the results will be returned in an array.

    Let's look at another example:

    #!/usr/local/bin/pike
                    
    inherit Stdio.File : input;
    inherit Stdio.File : output;
                    
    int main(int argc, array(string) argv)
    {
        output::create("stdout");
        for(int e=1;e<sizeof(argv);e++)
        {
            input::open(argv[e],"r");
            while(output::write(input::read(4096)) == 4096);
        }
    }
    This short piece of code works a lot like the UNIX command cat. It reads all the files given on the command line and writes them to stdout. As an example, I have inherited Stdio.File twice to show you that both files are usable from my program.

    6.7 Pike inherit compared to other languages

    Many other languages assign special meaning to inherit. Most common is the notion that if you inherit a class, it means that your class should obey the same rules as the inherited class. In Pike, this is not necessarily so. You may wish to use inherit in Pike like this, but you can just as well choose not to. This may confuse some programmers with previous experience in object oriented programming.

    6.8 Modifiers

    Sometimes, you may wish to hide things from inheriting programs, or prevent functions from being called from other objects. To do so you use modifiers. A modifier is simply a word written before a variable definition, function definition, class definition or an inherit that specifies how this identifier should interact with other objects and programs. These modifiers are available:
    static
    Static hides this identifier from the index and arrow operators, which makes it impossible for other objects to call this function unless a function pointer to it is returned from inside this program.
    final
    This prevents other objects from re-defining this identifier in programs that inherit this program.
    local
    This makes the identifier 'local' meaning that even if it is overloaded in an inheriting program, this program will still use this identifer.
    private
    This prevents inheriting programs from accessing this identifier. Note that inheriting program can still re-define the identifier. Also note that private does not imply static.
    public
    This is the opposite of private. This is the default for all identifiers. public can be used to override the effects of a private inherit.
    protected
    Reserved for future use.
    When modifiers are used in conjunction with inherit, all the variables, functions and classes copied from the inherited class will be modified with the keywords used. For instance, private inherit means that the identifiers from this inherit will not be available to program inheriting this program. static private inherit will also hide those identifiers from the index and arrow operators, making the inherit available only to the code in this program.

    6.9 Operator overloading

    Sometimes you want an object to act as if it was a string, an integer or some other data type. It is especially interesting to be able to use the normal operators on objects to allow short and readable syntax. In Pike, special methods are called when an operator is used with an object. To some extent, this is work in progress, but what has been done so far is very useful and will not be subject to change.

    The following table assumes that a and b are objects and shows what will be evaluated if you use that particular operation on an object. Note that some of these operators, notably == and ! have default behavior which will be used if the corresponding method is not defined in the object. Other operators will simply fail if called with objects. Refer to chapter 5 "Operators" for information on which operators can operate on objects without operator overloading.

    Operation Will call Or
    a+b a->`+(b) b->``+(a)
    a+b+c+d a->`+(b,c,d) (b->(a))+c+d
    a-b a->`-(b) b->``-(a)
    a&b a->`&(b) b->``&(a)
    a|b a->`|(b) b->``|(a)
    a^b a->`^(b) b->``^(a)
    a>>b a->`>>(b) b->``>>(a)
    a<<b a->`<<(b) b->``<<(a)
    a*b a->`*(b) b->``*(a)
    a*b*c*d a->`*(b,c,d) b->`*(a)*c*d
    a/b a->`/(b) b->``/(a)
    a%b a->`%(b) b->``%(a)
    ~a a->`~()  
    a==b a->`==(b) b->`==(a)
    a!=b !( a->`==(b) ) !( b->`==(a) )
    a<b a->`<(b) b->`>(a)
    a>b a->`>(b) b->`<(a)
    a<=b !( b->`>(a) ) !( a->`<(b) )
    a>=b !( b->`<(a) ) !( a->`>(b) )
    (int)a a->cast("int")  
    !a a->`!()  
    if(a) { ... }  !( a->`!() )  
    a[b] a->`[](b)  
    a[b]=c a->`[]=(b,c)  
    a->foo a->`->("foo")  
    a->foo=b a->`->=("foo",b)  
    sizeof(a) a->_sizeof()  
    indices(a) a->_indices()  
    values(a) a->_values()  
    a(b) a->`()(b)  

    Here is a really silly example of a program that will write 10 to stdout when executed.

    #!/usr/local/bin/pike
    class three {
        int `+(int foo) { return 3+foo; }
    };

    int main()
    {
        write(sprintf("%d\n",three()+7));
    }
    It is important to know that some optimizations are still performed even when operator overloading is in effect. If you define a multiplication operator and multiply your object with one, you should not be surprised if the multiplication operator is never called. This might not always be what you expect, in which case you are better off not using operator overloading.

    6.10 Simple exercises

  • Make a program that clones 10 hello world and then runs main() in each one of them.
  • Modify the register program to use an object for each record.
  • Modify the register program to use the following search function:
    void find_song(string title)
    {
        string name, song;
        int hits;

        title=lower_case(title);

        foreach(indices(records),name)
        {
            if(string song=records[name][title])
            {
                write(name+"; "+song+"\n");
                hits++;
            }
        }

        if(!hits) write("Not found.\n");
    }
  • Chapter 7, Miscellaneous functions

    There are some 'functions' in Pike that are not really functions at all but just as builtin as operators. These special functions can do things that no other functions can do, but they can not be re-defined or overloaded. In this chapter I will describe these functions and why they are implemented as special functions.

    7.1 sscanf

    Sscanf may look exactly like a normal function, but normal functions can not set the variables you send to it. The purpose of sscanf is to match one string against a format string and place the matching results into a list of variables. The syntax looks like this:
    int sscanf(string str, string fmt, lvalue ...)
    The string str will be matched against the format string fmt. fmt can contain strings separated by %d,%s,%c and %f. Every % corresponds to one lvalue. An lvalue is the name of a variable, a name of a local variable, an index in an array, mapping or object. It is because of these lvalues that sscanf can not be implemented as a normal function.

    Whenever a percent is found in the format string, a match is according to the following table:
    %b reads a binary integer
    %d reads a decimal integer
    %o reads an octal integer
    %x reads a hexadecimal integer
    %D reads an integer that is either octal (leading zero), hexadecimal (leading 0x) or decimal.
    %f reads a float
    %c matches one char and returns it as an integer
    %2c matches two chars and returns them as an integer (short)
    %4F matches four chars and returns them as a float (IEEE single precision)
    %8F matches eigth chars and returns them as a float (IEEE double precision)
    %s reads a string. If followed by %d, %s will read any non-numerical characters. If followed by a %[], %s will read any characters not present in the set. If followed by normal text, %s will match all characters up to but not including the first occurrence of that text.
    %5s gives a string of 5 characters (5 can be any number)
    %[set] matches a string containing a given set of characters (those given inside the brackets). %[^set] means any character except those inside brackets. Example: %[0-9H] means any number or 'H'.
    %{format%} Repeatedly matches 'format' as many times as possible and assigns an array of arrays with the results to the lvalue.

    If a * is put between the percent and the operator, the operator will only match its argument, not assign any variables.

    Sscanf does not use backtracking. Sscanf simply looks at the format string up to the next % and tries to match that with the string. It then proceeds to look at the next part. If a part does not match, sscanf immediately returns how many % were matched. If this happens, the lvalues for % that were not matched will not be changed.

    Let's look at a couple of examples:

    // a will be assigned "oo" and 1 will be returned
    sscanf("foo","f%s",a);

    // a will be 4711 and b will be "bar", 2 will be returned
    sscanf("4711bar","%d%s",a,b);

    // a will become "test"
    sscanf(" \t test","%*[ \t]%s",a)

    // Remove "the " from the beginning of a string
    // If 'str' does not begin with "the " it will not be changed
    sscanf(str,"the %s",str);

    SEE ALSO : sprintf

    7.2 catch & throw

    Catch is used to trap errors and other exceptions in Pike. It works by making a block of code into an expression, like this:
    catch { statements }
    If an error occurs, catch will return a description of the error. The description of the error has the following format:
    ({
         "error description",
         backtrace()
    })
    If no error occurs, catch will return zero. You may emulate your own errors using the function throw, described in
    chapter 16 "Builtin functions".

    Example:

    int x,y;
    // This might generate "division by zero"
    mixed error=catch { x/=y; };

    7.3 gauge

    The syntax for gauge is the same as the syntax for catch:
    gauge { statements }
    However, gauge simply returns how many seconds the code took to execute. This can be used to find out how fast your code actually is.. :) Only CPU time used by the Pike process is measured. This means that if it takes two seconds to execute but only uses 50 % CPU, this function will return 1.0.

    7.4 typeof

    This function returns the type of an expression as a string. It does not evaluate the expression at all, which might be somewhat confusing. Example:
    typeof( exit(1) )
    This will return the string "void" since exit is a function that returns void. It will not execute the function exit and exit the process as you might expect.

    If you want to know the type after evaluation, use sprintf("%t", expr).

    Chapter 8, Modules

    A module is a software package that plugs into the Pike programming environment. They provide you with simple interfaces to system routines and they also constitute a neat way to use your own C/C++ code from within Pike. Pike comes with a number of modules that are ready to use. In this chapter I will explain the basics of modules and how to use them.

    here is a list of the basic Pike modules:

    Stdio
    This module contains file I/O routines.
    Array
    This module contains functions that operate on arrays.
    Calendar
    Support for different calendar and date formats.
    Crypto *
    Cryptography routines.
    Gdbm *
    This module contains support for Gdbm databases.
    Getopt
    Routines to parse command line options
    Gmp *
    Support for large numbers.
    Gz *
    Deflate packing algorithms.
    Image
    Image manipulation routines.
    LR
    LALR(1) parser generator.
    Msql *
    Sql database support for the mini-SQL database server.
    MIME
    Support for coding and decoding MIME.
    Mysql *
    Sql database support for the mySQL database server.
    Process
    Functions to start and control other processes.
    Protocols
    Support for HTTP, NNTP, SMNT, DNS, TELNET and other protocols.
    Regexp
    Regexp matching routines.
    Simulate
    Routines to emulate old Pike routines.
    String
    Routines that operate on strings.
    Sql *
    Support for ASN1, PKCS and other standards.
    Standards
    Generic SQL database support.
    System
    Support for system specific functions.
    Thread *
    Thread support functions.
    Tools
    Complete programs available to use from within scripts.
    Yp *
    Network Information System support.

    * These modules might not be available depending on how Pike was compiled and whether support for these functions exist on your system.

    8.1 How to use modules

    A module is a bunch of functions, programs or other modules collected in one symbol. For instance, the module Stdio contains the objects stdin, stdout and stderr. To access these objects you can write Stdio.stdin, Stdio.stdout or Stdio.stderr anywhere in your program where an object of that type is acceptable. If you use Stdio a lot you can put import Stdio; in the beginning of your program. This will import all the identifiers from the module Stdio into your program, making it possible to write just stdin instead of Stdio.stdin. It is also possible to import all modules in a directory with import by putting the directory name in doublequtes. So, to import all modules in the current directory, you would use import ".";.

    8.2 Where do modules come from?

    Modules are not loaded until you use them, which saves memory unless you use all the modules. However, if you want to write your own modules it is important to know how modules are located and loaded.

    When you use Stdio Pike will look for that module:

    1. In imported directories.
    2. In directories added with add_module_path()
    3. In directories specified with -M on the command line.
    4. In directories in the environment variable PIKE_MODULE_PATH
    5. In the directory with builtin modules, usually /usr/local/lib/pike/modules/
    For each of these directories, Pike will do the following:
    1. If there is a file called Stdio.pmod.pike, Pike will load this Pike program, clone it and use that as a module.
    2. If there is a file called Stdio.pmod.so, Pike will load this with load_module(), clone it and use that as a module.
    3. If there is a directory called Stdio.pmod, Pike will create a module containing all the modules in that directory as identifiers. If there is a module called module in that directory, all identifiers from that module will overload any modules actually present in the directory.
    As you can see, quite a lot of work goes into finding the modules, this makes it possible to choose the most convenient way to build your own Pike modules.

    8.3 The . operator

    The period operator is not really an operator, as it is always evaluated during the compilation. It works similarly to the index and arrow operators, but can only be used on constant values such as modules. In most cases, modules are simply a clone of a program, in which case the identifiers in the module will be the same as those in the program. But some modules, like those created from directories, overload the index operator so that the identifiers in the module can be something other than those in the program. For directory modules, the index operator looks in the directory it was cloned for to find the identifiers.

    You can also use the . operator without an identifier preceeding it to access modules in the same directory as the program itself. For instance, .my_module.foo would mean 'the identifier foo in the module my_module in this directory.

    8.4 How to write a module

    Here is an example of a simple module:
    constant PI = 3.14159265358979323846264338327950;
    float cos2(float f) { return pow(cos(f),2.0); }
    if we save this short file as Trig.pmod we can now use this module like this:
    int main()
    {
        write(sprintf("%f\n",.Trig.cos2(.Trig.PI));
    }
    or like this:
    import .Trig;

    int main()
    {
        write(sprintf("%f\n",cos2(PI));
    }

    8.5 Simple exercises

  • Save the hello_world.pike program as hello_world.pike.pmod, then make a program that loads this module and calls its main().
  • Make a directory called Programs.pmod and put all the examples you have written so far in it. Make a program that runs one of those programs. Make sure the program can be modified to run another of your examples by changing what module it loads.
  • Copy the file hello_world.pike.pmod to programs/module.pike.pmod and then write a program that runs hello_world without actually using the identifier hello_world.
  • Try putting Programs.pmod in another directory and then try to run the programs from the last two examples.
  • Chapter 9, File I/O

    Programming without reading and writing data from files, sockets, keyboard etc. would be quite pointless. Luckily enough, Pike provides you with an object oriented interface to files, pipes and TCP sockets. All I/O functions and classes are collected in the module Stdio.

    9.1 File management - Stdio.File

    CLASS
    Stdio.File This is the basic I/O object, it provides socket communication as well as file access. It does not buffer reads and writes or provide line-by-line reading, that is done in the FILE object. Stdio.File is completely written in C. What follows is a description of all the functions in Stdio.File.

    METHOD
    Stdio.File.create - init file struct

    SYNTAX
    object(Stdio.File) Stdio.File();
    object(Stdio.File) Stdio.File(string filename);
    object(Stdio.File) Stdio.File(string filename, string mode);
    object(Stdio.File) Stdio.File(string filename, string mode, int mask);
    object(Stdio.File) Stdio.File(string fd);
    object(Stdio.File) Stdio.File(int fd);
    object(Stdio.File) Stdio.File(int fd, string mode);

    DESCRIPTION
    There are four different ways to clone a File. The first is to clone it without any arguments, in which case the you have to call open(), connect() or some other method which connects the File object with a stream.

    However, instead of cloning and then calling open(), you can clone the File with a filename and open mode. This is the same thing as cloning and then calling open, except shorter and faster. Default open mode is "r" and default mask is 0666.

    Alternatively, you can clone a File with "stdin", "stdout" or "stderr" as argument. This will open the specified standard stream.

    For the advanced users, you can use the file descriptors of the systems (note: emulated by pike on some systems - like NT). This is only useful for streaming purposes on unix systems. This is not recommended at all if you don't know what you're into. Default mode for this is "rw".

    NOTE
    Open mode will be filtered through the system UMASK. You might need to use chmod later.

    SEE ALSO
    clone and Stdio.File->open

    METHOD
    Stdio.File.open - open a file

    SYNTAX
    int open(string filename, string how);
    int open(string filename, string how, int mode);

    DESCRIPTION
    Open a file for read, write or append. The variable how should contain one or more of the following letters:

    'r' open file for reading
    'w' open file for writing
    'a' open file for append (use with 'w')
    't' truncate file at open (use with 'w')
    'c' create file if it doesn't exist (use with 'w')
    'x' fail if file already exist (use with 'c')

    How should _always_ contain at least one of 'r' or 'w'.

    The third argument is protection bits if the file is created. Default is 0666 (all read+write, in octal notation).

    RETURNS
    1 on success, 0 otherwise

    SEE ALSO
    Stdio.File->close

    METHOD
    Stdio.File.close - close a file

    SYNTAX
    int close(string how);
    int close();

    DESCRIPTION
    Close the file. Optionally, specify "r", "w" or "rw" to close just the read, just the write or both read and write part of the file respectively. Note that this function will not call the close_callback.

    SEE ALSO
    Stdio.File->open

    METHOD
    Stdio.File.read - read data from a file or stream

    SYNTAX
    string read(int nbytes);
    string read(int nbytes, int notall);
    string read();

    DESCRIPTION
    Read tries to read nbytes bytes from the file, and return it as a string. If something goes wrong, zero is returned.

    If a one is given as second argument to read(), read will not try its best to read as many bytes as you asked it to read, it will merely try to read as many bytes as the system read function will return. This mainly useful with stream devices which can return exactly one row or packet at a time.

    If no arguments are given, read will read to the end of the file/stream.

    SEE ALSO
    Stdio.File->read_oob and Stdio.File->write

    METHOD
    Stdio.File.read_oob - read out-of-band data from a stream

    SYNTAX
    string read_oob(int nbytes);
    string read_oob(int nbytes, int notall);
    string read_oob();

    DESCRIPTION
    Tries to read nbytes bytes of out-of-band data from the stream, and returns it as a string. If something goes wrong, zero is returned.

    If a one is given as a second argument to read_oob(), only as many bytes of out-of-band data as are currently available will be returned.

    If no arguments are given, read_oob will read to the end of the stream.

    NOTE
    This function is only available if the option '--with-oob' was specified when Pike was compiled.

    It is not guaranteed that all out-of-band data sent from the other end will be received. Most streams only allow for a single byte of out-of-band data at a time.

    SEE ALSO
    Stdio.File->read and Stdio.File->write_oob

    METHOD
    Stdio.File.write - write data to a file or stream

    SYNTAX
    int write(string data);

    DESCRIPTION
    Write data to file or stream and return how many bytes that were actually written. 0 is returned in nonblocking mode if it was not possible to write anything without blocking. -1 is returned if something went wrong and no bytes were written.

    SEE ALSO
    Stdio.File->read and Stdio.File->write_oob

    METHOD
    Stdio.File.write_oob - write out-of-band data to a stream

    SYNTAX
    int write_oob(string data);

    DESCRIPTION
    Writes out-of-band data to a stream and returns how many bytes that were actually written. -1 is returned if something went wrong and no bytes were written.

    NOTE
    This function is only available if the option '--with-oob' was specified when Pike was compiled.

    It is not guaranteed that all out-of-band data will be received at the other end. Most streams only allow for a single byte of out-of-band data at a time. Some streams will send the rest of the data as ordinary data.

    SEE ALSO
    Stdio.File->read_oob and Stdio.File->write

    METHOD
    Stdio.File.seek - seek to a position in a file

    SYNTAX
    int seek(int pos);

    DESCRIPTION
    Seek to a position in a file, if pos is less than zero, seek to position pos relative end of file. Returns -1 for failure, or the new position in the file when successful.

    SEE ALSO
    Stdio.File->tell

    METHOD
    Stdio.File.tell - tell where we are in a file

    SYNTAX
    int tell();

    DESCRIPTION
    Returns the current position in the file.

    SEE ALSO
    Stdio.File->seek

    METHOD
    Stdio.File.truncate - truncate a file

    SYNTAX
    int truncate(int length);

    DESCRIPTION
    Truncates a file to that length. Returns 1 if ok, 0 if failed.

    SEE ALSO
    Stdio.File->open

    METHOD
    Stdio.File.stat - do file_stat on an open file

    SYNTAX
    array(int) stat();

    DESCRIPTION
    This function returns the same information as the function file_stat, but for the file it is called in. If file is not an open file, zero will be returned. Zero is also returned if file is a pipe or socket.

    SEE ALSO
    file_stat

    METHOD
    Stdio.File.errno - what was last error?

    SYNTAX
    int errno();

    DESCRIPTION
    Returns the error code for the last command on this file. Error code is normally cleared when a command is successful.

    METHOD
    Stdio.File.set_buffer - set internal socket buffer

    SYNTAX
    void set_buffer(int bufsize, string mode);
    void set_buffer(int bufsize);

    DESCRIPTION
    This function sets the internal buffer size of a socket or stream. The second argument allows you to set the read or write buffer by specifying "r" or "w". It is not guaranteed that this function actually does anything, but it certainly helps to increase data transfer speed when it does.

    SEE ALSO
    Stdio.File->open_socket and Stdio.Port->accept

    METHOD
    Stdio.File.set_nonblocking - make stream nonblocking

    SYNTAX
    void set_nonblocking(function(mixed, string:void) read_callback,
    function(mixed:void) write_callback,
    function(mixed:void) close_callback);
    or
    void set_nonblocking(function(mixed, string:void) read_callback,
    function(mixed:void) write_callback,
    function(mixed:void) close_callback,
    function(mixed, string:void) read_oob_callback,
    function(mixed:void) write_oob_callback);
    or
    void set_nonblocking();

    DESCRIPTION
    This function sets a stream to nonblocking mode. When data arrives on the stream, read_callback will be called with some or all of this data. When the stream has buffer space over for writing, write_callback is called so you can write more data to it. If the stream is closed at the other end, close_callback is called.

    When out-of-band data arrives on the stream, read_oob_callback will be called with some or all of this data. When the stream allows out-of-band data to be sent, write_oob_callback is called so that you can write out-of-band data to it.

    All callbacks will have the id of file as first argument when called.

    If no arguments are given, the callbacks are not changed. The stream is just set to nonblocking mode.

    NOTE
    Out-of-band data is only supported if Pike was compiled with the option '--with-oob'.

    SEE ALSO
    Stdio.File->set_blocking and Stdio.File->set_id

    METHOD
    Stdio.File.set_read_callback - set the read callback

    SYNTAX
    void set_read_callback(function read_callback)

    DESCRIPTION
    This function sets the read callback for the file. The read callback is called whenever there is data to read from the file. Note that this function does not set the file nonblocking.

    SEE ALSO
    Stdio.File->set_nonblocking

    METHOD
    Stdio.File.set_write_callback - set the write callback

    SYNTAX
    void set_write_callback(function write_callback)

    DESCRIPTION
    This function sets the write callback for the file. The write callback is called whenever there is buffer space available to write to for the file. Note that this function does not set the file nonblocking.

    SEE ALSO
    Stdio.File->set_nonblocking

    METHOD
    Stdio.File.set_close_callback - set the close callback

    SYNTAX
    void set_close_callback(function close_callback)

    DESCRIPTION
    This function sets the close callback for the file. The close callback is called when the remote end of a socket or pipe is closed. Note that this function does not set the file nonblocking.

    SEE ALSO
    Stdio.File->set_nonblocking

    METHOD
    Stdio.File.set_blocking - make stream blocking

    SYNTAX
    void set_blocking();

    DESCRIPTION
    This function sets a stream to blocking mode. i.e. all reads and writes will wait until data has been written before returning.

    SEE ALSO
    Stdio.File->set_nonblocking

    METHOD
    Stdio.File.set_id - set id of this file

    SYNTAX
    void set_id(mixed id);

    DESCRIPTION
    This function sets the id of this file. The id is mainly used as an identifier that is sent as the first arguments to all callbacks. The default id is 0. Another possible use of the id is to hold all data related to this file in a mapping or array.

    SEE ALSO
    Stdio.File->query_id

    METHOD
    Stdio.File.query_id - get id of this file

    SYNTAX
    mixed query_id();

    DESCRIPTION
    This function returns the id of this file.

    SEE ALSO
    Stdio.File->set_id

    METHOD
    Stdio.File.query_read_callback - return the read callback function

    SYNTAX
    function query_read_callback();

    DESCRIPTION
    This function returns the read_callback, which is set with set_nonblocking or set_read_callback.

    SEE ALSO
    Stdio.File->set_nonblocking and Stdio.File->set_read_callback

    METHOD
    Stdio.File.query_write_callback - return the write callback function

    SYNTAX
    function query_write_callback();

    DESCRIPTION
    This function returns the write_callback, which is set with set_nonblocking or set_write_callback.

    SEE ALSO
    Stdio.File->set_nonblocking and Stdio.File->set_write_callback

    METHOD
    Stdio.File.query_close_callback - return the close callback function

    SYNTAX
    function query_close_callback();

    DESCRIPTION
    This function returns the close_callback, which is set with set_nonblocking or set_close_callback.

    SEE ALSO
    Stdio.File->set_nonblocking and Stdio.File->set_close_callback

    METHOD
    Stdio.File.dup - duplicate a file

    SYNTAX
    object(Stdio.File) dup();

    DESCRIPTION
    This function returns a clone of Stdio.File with all variables copied from this file. Note that all variables, even id, is copied.

    SEE ALSO
    Stdio.File->assign

    METHOD
    Stdio.File.dup2 - duplicate a file over another

    SYNTAX
    int dup2(object(Stdio.File) to);

    DESCRIPTION
    This function works similarly to Stdio.File->assign, but instead of making the argument a reference to the same file, it creates a new file with the same properties and places it in the argument.

    EXAMPLE
    /* Redirect stdin to come from the file 'foo' */
    object o=Stdio.File();
    o->open("foo","r");
    o->dup2(Stdio.File("stdin"));

    SEE ALSO
    Stdio.File->assign and Stdio.File->dup

    METHOD
    Stdio.File.assign - assign a file

    SYNTAX
    void assign(object f);

    DESCRIPTION
    This function takes a clone of Stdio.File and assigns all variables of this file from it. It can be used together with file->dup to move files around.

    SEE ALSO
    Stdio.File->dup

    METHOD
    Stdio.File.open_socket - open a socket

    SYNTAX
    int open_socket(int|void port, string|void address);

    DESCRIPTION
    This makes this file into a socket ready for connection. The reason for this function is so that you can set the socket to nonblocking or blocking (default is blocking) before you call Stdio.File->connect() This function returns 1 for success, 0 otherwise.

    If you give a port number to this function, the socket will be bound to this port locally before connecting anywhere. This is only useful for some silly protocols like FTP. You may also specify an address to bind to if your machine has many IP numbers.

    SEE ALSO
    Stdio.File->connect and Stdio.File->set_nonblocking

    METHOD
    Stdio.File.connect - connect a socket to something

    SYNTAX
    int connect(string IP,int port);

    DESCRIPTION
    This function connects a socket previously created with Stdio.File->open_socket to a remote socket. The argument is the IP name or number for he remote machine.

    This function returns 1 for success, 0 otherwise. Note that if the socket is in nonblocking mode, you have to wait for a write or close callback before you know if the connection failed or not.

    SEE ALSO
    Stdio.File->query_address

    METHOD
    Stdio.File.query_address - get addresses

    SYNTAX
    string query_address();
    string query_address(1);

    DESCRIPTION
    This function returns the remote or local address of a socket on the form "x.x.x.x port". Without argument, the remote address is returned, with argument the local address is returned. If this file is not a socket, not connected or some other error occurs, zero is returned.

    SEE ALSO
    Stdio.File->connect

    METHOD
    Stdio.File.pipe - create a two-way pipe

    SYNTAX
    object pipe();

    DESCRIPTION
    This function creates a pipe between the object it was called in and an object that is returned. The two ends of the pipe are indistinguishable. If the File object this function is called in was open to begin with, it is closed before the pipe is created.

    SEE ALSO
    fork

    METHOD
    Stdio.File.set_close_on_exec - set / clear the close on exec flag

    SYNTAX
    void set_close_on_exec(int onoff);

    DESCRIPTION
    This function determines whether this file will be closed when calling exece. Default is that the file WILL be closed on exec except for stdin, stdout and stderr.

    SEE ALSO
    exece

    Here is an example of how to use the TCP functions in Stdio.File in blocking mode. This short program takes a URL as first argument, connects to the WWW server, sends a HEAD request and writes the reply to stdout. For clarity, all calls to Stdio.File use File:: even if that is not strictly necessary.

    import Stdio;
    inherit File;

    int main(int argc, array(string) argv)
    {
        string host;
        string path="";
        int port=80;
        sscanf(argv[1],"http://%s",argv[1]);
        sscanf(argv[1],"%s/%s",host,path);
        sscanf(host,"%s:%d",host,port);

        if(!File::open_socket())
        {
            perror("Open socket failed");
            exit(1);
        }

        if(!File::connect(host,port))
        {
            perror("Failed to connect to remote host");
            exit(1);
        }

        File::write(sprintf("HEAD /%s HTTP/1.0\n",path));
        stdout::write(File::read());
    }

    9.2 Buffered file management - Stdio.FILE

    CLASS
    Stdio.FILE Stdio.FILE is a buffered version of Stdio.File, it inherits Stdio.File and has most of the functionality of Stdio.File. However, it has an input buffer that allows line-by-line input. Note that the output part of Stdio.FILE is not buffered at this moment. The added functionality of Stdio.FILE is described here:

    METHOD
    Stdio.FILE.gets - get one line

    SYNTAX
    string gets();

    DESCRIPTION
    This function returns one line from the FILE, it returns zero if no more lines are available.

    METHOD
    Stdio.FILE.printf - formatted print

    SYNTAX
    string printf(string format, mixed ... data);

    DESCRIPTION
    This function does approximately the same as: write(sprintf(format,@data))

    SEE ALSO
    sprintf

    METHOD
    Stdio.FILE.ungets - put a string back in the buffer

    SYNTAX
    string ungets(string s);

    DESCRIPTION
    This function puts a string back in the input buffer. The string can then be read with read, gets or getchar.

    METHOD
    Stdio.FILE.getchar - get one character from the input stream

    SYNTAX
    int getchar();

    DESCRIPTION
    This function returns one character from the input stream. Note that the return value is the ascii value of the character, not a string containing one character.

    9.3 Standard streams - Stdio.stdin, stdout and stderr

    Any UNIX program has three files open from the beginning. These are called standard input, standard output and standard error stream. These streams are available from Pike as well. They are called Stdio.stdin, Stdio.stdout and Stdio.stderr respectively. Standard input is a clone of Stdio.FILE, which means you can use the line oriented functions. Stdio.stdout and Stdio.stderr are simply clones of Stdio.File.

    Example:

    int main()
    {
        int line;
        while(string s=Stdio.stdin.gets())
            write(sprintf("%5d: %s\n",line++,s));
    }
    This example will read lines from standard input for as long as there are more lines to read. Each line will then be written to stdout together with the line number. We could use Stdio.stdout.write instead of just write because they are the same function.

    9.4 Listening to sockets - Stdio.Port

    CLASS
    Stdio.Port Stdio.File can handle connections to any TCP socket, but it can not listen to a local TCP port. For this purpose there is a special class called Stdio.Port. Stdio.Port cannot read or write any data, but it can accept connections which will be returned as clones of Stdio.File. These are the methods available in Stdio.Port:

    METHOD
    Stdio.Port.bind - open socket and bind it to a port

    SYNTAX
    int bind(int port);
    int bind(int port,function accept_callback);
    int bind(int port,function accept_callback, string IP);

    DESCRIPTION
    Bind opens a sockets and binds it to port number on the local machine. If the second argument is present, the socket is set to nonblocking and the callback function is called whenever something connects to the socket. The callback will receive the id for this port as argument. Bind returns 1 on success, and zero on failure.

    If the optional argument IP is given, bind will try to bind to this IP name (or number).

    SEE ALSO
    Stdio.Port->accept

    METHOD
    Stdio.Port.listen_fd - listen to an already open port

    SYNTAX
    int listen_fd(int fd);
    int listen_fd(int fd,function accept_callback);

    DESCRIPTION
    This function does the same as Stdio.Port->bind, except that instead of creating a new socket and bind it to a port, it expects that the file descriptor fd is an already open port.

    NOTE
    This function is only for the advanced user, and is generally used when sockets are passed to Pike at exec time.

    SEE ALSO
    Stdio.Port->bind and Stdio.Port->accept

    METHOD
    Stdio.Port.create - create and/or setup a port

    SYNTAX
    object(Stdio.Port) Stdio.Port("stdin")
    object(Stdio.Port) Stdio.Port("stdin",function accept_callback)
    object(Stdio.Port) Stdio.Port("stdin",function accept_callback)
    object(Stdio.Port) Stdio.Port(int port)
    object(Stdio.Port) Stdio.Port(int port,function accept_callback)
    object(Stdio.Port) Stdio.Port(int port,function accept_callback, string ip)

    DESCRIPTION
    When create is called with "stdin" as argument, a socket is created out of the file descriptor 0. This is only useful if that actually is a socket to begin with. When create is called with an int as first argument, it does the same as bind() would do with the same arguments. The second and third argument has the same function as in the bind() call.

    SEE ALSO
    clone and Stdio.Port->bind

    METHOD
    Stdio.Port.set_id - set the id of a port

    SYNTAX
    void set_id(mixed id);

    DESCRIPTION
    This function sets the id used for accept_callback by this port. The default id is this_object().

    SEE ALSO
    Stdio.Port->query_id

    METHOD
    Stdio.Port.query_id - Return the id for this port.

    SYNTAX
    mixed query_id();

    DESCRIPTION
    This function returns the id for this port. The id is normally the first argument to accept_callback.

    SEE ALSO
    Stdio.Port->set_id

    METHOD
    Stdio.Port.errno - return the last error

    SYNTAX
    int errno();

    DESCRIPTION
    If the last call done on this port failed, errno will return an integer describing what went wrong. Refer to your Unix manual for further information.

    SEE ALSO
    Stdio.Port->errno

    METHOD
    Stdio.Port.accept - accept a connection

    SYNTAX
    object accept();

    DESCRIPTION
    This function completes a connection made from a remote machine to this port. It returns a two-way stream in the form of a copy of Stdio.File. The new file is by default set to blocking.

    SEE ALSO
    Stdio.File

    9.5 UDP socket and message management - Stdio.UDP

    CLASS
    Stdio.UDP

    SYNTAX
    Stdio.UDP();

    DESCRIPTION
    Stdio.UDP is the way of transceiving UDP from Pike.

    SEE ALSO
    Stdio.File

    METHOD
    Stdio.UDP.bind

    SYNTAX
    bind(int port); bind(int port,string address);

    DESCRIPTION
    binds a port for recieving or transmitting UDP

    RETURNS
    the called object

    METHOD
    Stdio.UDP.enable_broadcast

    SYNTAX
    enable_broadcast()

    DESCRIPTION
    enable transmission of broadcasts. This is normally only avalable to root users.

    RETURNS
    1 upon success, 0 otherwise

    METHOD
    Stdio.UDP.read

    SYNTAX
    read()
    read(int flags)

    DESCRIPTION
    read from the UDP socket. Flags is a bitfield, 1 for out of band data and 2 for peek.

    RETURNS
    mapping(string:int|string) in the form
    ([
       "data" : string recieved data
       "ip" : string   recieved from this ip
       "port" : int    ...and this port
    ])
    

    SEE ALSO
    Stdio.UDP.set_nonblocking and Stdio.UDP.set_read_callback

    METHOD
    Stdio.UDP.send

    SYNTAX
    send(string to_addr,int to_port,string data)
    send(string to_addr,int to_port,string data,int flags)

    DESCRIPTION

    RETURNS
    number of bytes sent

    METHOD
    Stdio.UDP.set_nonblocking

    SYNTAX
    set_nonblocking();
    set_blocking();
    set_nonblocking(function, mixed ...);

    DESCRIPTION
    sets this object to be nonblocking or blocking. If set_nonblocking is given with argument, these are passed to set_read_callback().

    RETURNS
    the called object

    METHOD
    Stdio.UDP.set_read_callback

    SYNTAX
    set_read_callback(function(mapping(string:int|string), mixed...), mixed ... extra);

    DESCRIPTION
    The called function is recieving mapping similar to the return value of read:
    ([
       "data" : string recieved data
       "ip" : string   recieved from this ip
       "port" : int    ...and this port
    ])
    

    RETURNS
    the called object

    METHOD
    Stdio.UDP.query_address

    SYNTAX
    query_address()

    DESCRIPTION

    RETURNS
    the current address in format "<ip> <port>".

    SEE ALSO
    Stdio.File.query_address

    9.6 Terminal management - Stdio.Terminfo

    FUNCTION
    Stdio.Terminfo.getFallbackTerm - get fallback terminal

    SYNTAX
    object(Stdio.Terminfo.Terminfo)|object(Stdio.Terminfo.Termcap) getFallbackTerm(string term);

    DESCRIPTION
    Returns an object describing the fallback terminal for the terminal term. This is usually equvivalent to Stdio.Terminfo.getTerm("dumb").

    SEE ALSO
    Stdio.Terminfo.getTerm

    FUNCTION
    Stdio.Terminfo.getTerm - get terminal description

    SYNTAX
    object(Stdio.Terminfo.Terminfo)|object(Stdio.Terminfo.Termcap) getTerm();
    or
    object(Stdio.Terminfo.Terminfo)|object(Stdio.Terminfo.Termcap) getTerm(string term);

    DESCRIPTION
    Returns an object describing the terminal term. If term is not specified, it will default to getenv("TERM") or if that fails to "dumb".

    Lookup of terminal information will first be done in the systems terminfo database, and if that fails in the termcap database. If neither database exists, a hardcoded entry for "dumb" will be used.

    SEE ALSO
    Stdio.Terminfo.getTerminfo, Stdio.Terminfo.getTermcap and Stdio.getFallbackTerm

    FUNCTION
    Stdio.Terminfo.getTermcap - get termcap description

    SYNTAX
    object(Stdio.Terminfo.Termcap) getTermcap(string term);

    DESCRIPTION
    Return the terminal description of term from the systems termcap database. Returns 0 if not found.

    SEE ALSO
    Stdio.Terminfo.getTerm and Stdio.Terminfo.getTerminfo

    FUNCTION
    Stdio.Terminfo.getTerminfo - get terminfo description

    SYNTAX
    object(Stdio.Terminfo.Terminfo) getTerminfo(string term);

    DESCRIPTION
    Return the terminal description of term from the systems terminfo database. Returns 0 if not found.

    SEE ALSO
    Stdio.Terminfo.getTerm and Stdio.Terminfo.getTermcap

    9.6.1 Stdio.Terminfo.Termcap

    CLASS
    Stdio.Terminfo.Termcap Termcap terminal decription object.

    METHOD
    Stdio.Terminfo.Termcap.create - initialize termcap object

    SYNTAX
    object(Stdio.Terminfo.Termcap) Termcap(string cap);
    or
    object(Stdio.Terminfo.Termcap) Termcap(string cap, object tcdb);
    or
    object(Stdio.Terminfo.Termcap) Termcap(string cap, object tcdb, int maxrecurse);

    METHOD
    Stdio.Terminfo.Termcap.tputs - put termcap string

    9.6.2 Stdio.Terminfo.Terminfo

    CLASS
    Stdio.Terminfo.Terminfo Terminfo terminal description object.

    9.7 Simple input-by-prompt - Stdio.Readline

    CLASS
    Stdio.Readline

    METHOD
    Stdio.Readline.create - init readline

    SYNTAX
    object(Stdio.Readline) Stdio.Readline();
    or
    object(Stdio.Readline) Stdio.Readline(object infd);
    or
    object(Stdio.Readline) Stdio.Readline(object infd,
    object|string interm);

    or
    object(Stdio.Readline) Stdio.Readline(object infd,
    object|string interm,
    object outfd);

    or
    object(Stdio.Readline) Stdio.Readline(object infd,
    object|string interm,
    object outfd,
    object|string outterm);

    DESCRIPTION
    Creates a Readline object, that takes input from infd, and has output on outfd.

    infd defaults to Stdio.stdout.

    interm defaults to Stdio.Terminfo.getTerm().

    outfd defaults to infd, unless infd is 0, in which case outfd defaults to Stdio.stdout.

    outterm defaults to interm.

    9.8 Other Stdio functions

    The Stdio module also contains a collection of high level IO functions to make it easy to write short and readable Pike programs. Most of these functions are implemented using Stdio.File and Stdio.FILE.

    FUNCTION
    Stdio.append_path - append paths in a secure way

    SYNTAX
    string append_path(string absolute, string ... relative);

    DESCRIPTION
    Append relative paths to an absolute path and remove any "//", "../" or "/." to produce a straightforward absolute path as a result. "../" is ignorded in the relative paths if it makes the created path begin with something else than the absolute path (or so far created path).

    EXAMPLE
    > Stdio.append_path("/foo/bar/","..");
    Result: /foo/bar/
    > Stdio.append_path("/foo/bar/","../apa.c");
    Result: /foo/bar/apa.c
    > Stdio.append_path("/foo/bar","./sune.c");
    Result: /foo/bar/sune.c
    > Stdio.append_path("/foo/bar","bertil/../../sune.c");
    Result: /foo/bar/sune.c
    > Stdio.append_path("/foo/bar","klas","bertil/../../sune.c");
    Result: /foo/bar/klas/sune.c

    SEE ALSO
    combine_path

    FUNCTION
    Stdio.file_size - return the size of a file in bytes

    SYNTAX
    int file_size(string file);

    DESCRIPTION
    Give the size of a file. Size -1 indicates that the file either does not exist, or that it is not readable by you. Size -2 indicates that it is a directory.

    SEE ALSO
    Stdio.write_file and Stdio.read_bytes

    FUNCTION
    Stdio.exist - check if a path exists

    SYNTAX
    int exist(string path);

    DESCRIPTION
    Returns true if the given path exists (is a directory or file), otherwise false.

    SEE ALSO
    Stdio.is_dir, Stdio.is_file and Stdio.is_link

    FUNCTION
    Stdio.is_dir - check if a path is a directory

    SYNTAX
    int is_dir(string path);

    DESCRIPTION
    Returns true if the given path is a directory, otherwise false.

    SEE ALSO
    Stdio.exist, Stdio.is_file and Stdio.is_link

    FUNCTION
    Stdio.is_file - check if a path is a file

    SYNTAX
    int is_file(string path);

    DESCRIPTION
    Returns true if the given path is a file, otherwise false.

    SEE ALSO
    Stdio.exist, Stdio.is_dir and Stdio.is_link

    FUNCTION
    Stdio.is_link - check if a path is a symbolic link

    SYNTAX
    int is_link(string path);

    DESCRIPTION
    Returns true if the given path is a symbolic link, otherwise false.

    SEE ALSO
    Stdio.exist, Stdio.is_dir and Stdio.is_file

    FUNCTION
    Stdio.mkdirhier - make a directory hierarchy

    SYNTAX
    int mkdirhier(string pathname, void|int mode);

    DESCRIPTION
    Creates zero or more directories to ensure that the given pathname is a directory. Returns zero if it fails and nonzero if it is successful. If a mode is given, it's used for the new directories after being &'ed with the current umask (on OS'es that supports this).

    SEE ALSO
    mkdir

    FUNCTION
    Stdio.perror - print error

    SYNTAX
    void perror(string s);

    DESCRIPTION
    This function prints a message to stderr along with a description of what went wrong if available. It uses the system errno to find out what went wrong, so it is only applicable to IO errors.

    SEE ALSO
    Stdio.werror

    FUNCTION
    Stdio.read_bytes - read a number of bytes into a string from a file

    SYNTAX
    string read_bytes(string file,int start,int len);
    string read_bytes(string file,int start);
    string read_bytes(string file);

    DESCRIPTION
    Read len number of bytes from file file staring at byte start and return it as a string. If len is omitted, the rest of the file will be returned. If start is also omitted, the entire file will be returned.

    SEE ALSO
    Stdio.write_file

    FUNCTION
    Stdio.read_file - read a number of lines into a string from file

    SYNTAX
    string read_file(string file, int start, int len);
    string read_file(string file);

    DESCRIPTION
    Read len lines from the file file after skipping start lines and return those lines as a string. If start and len are omitted the whole file is read.

    SEE ALSO
    Stdio.read_bytes and Stdio.write_file

    FUNCTION
    Stdio.readline - read a line from stdin

    SYNTAX
    string readline(string prompt);

    DESCRIPTION
    This function is gone. Use Stdio.Readline()->read instead.

    SEE ALSO
    Stdio.File

    FUNCTION
    Stdio.recursive_rm - remove a file or a directory tree recursively

    SYNTAX
    int recursive_rm(string path);

    DESCRIPTION
    Remove a file or directory a directory tree, return 0 if it fails. Nonzero otherwise.

    SEE ALSO
    rm

    FUNCTION
    Stdio.sendfile - send contents from one file to another

    SYNTAX
    object sendfile(array(string) headers,
    object from, int offset, int len,
    array(string) trailers,
    object to);

    or
    object sendfile(array(string) headers,
    object from, int offset, int len,
    array(string) trailers,
    object to,
    function(int, mixed ...:void) callback,
    mixed ... args);

    DESCRIPTION
    Sends headers followed by len bytes starting at offset from the file from followed by trailers to the file to. When completed callback is called with the total number of bytes sent as the first argument, followed by args.

    Any of headers, from and trailers may be left out by setting them to 0.

    Setting offset to -1 means send from the current position in from.

    Setting len to -1 means send until from's end of file is reached.

    NOTE
    The sending is performed asynchronously, and may complete before the function returns.

    For callback to be called, the backend must be active (ie main() must have returned -1).

    In some cases, the backend must also be active for any sending to be performed at all.

    SEE ALSO
    Stdio.File->set_nonblocking

    FUNCTION
    Stdio.werror - write to stderr

    SYNTAX
    void werror(string s);

    DESCRIPTION
    Writes a message to stderr. Stderr is normally the console, even if the process output has been redirected to a file or pipe.

    FUNCTION
    Stdio.write_file - append a string to a file

    SYNTAX
    int write_file(string file, string str)

    DESCRIPTION
    Append the string str onto the file file. Returns number of bytes written.

    SEE ALSO
    Stdio.read_bytes

    9.9 A simple example

    Here is a simple example of how to use the Stdio-functions.

    string grep(string indata, string needle)
    {
        object out=Stdio.File(),in=Stdio.File();
        object process=
         Process.create_process(({"/bin/grep",needle}),
            (["stdin":out->pipe(),
            "stdout":in->pipe()]) );
        out->write(indata);
        out->close();
        process->wait();
        return in->read();
    }
    This function filters the indata through the UNIX-command /bin/grep and return the result.

    9.10 A more complex example - a simple WWW server

    As most of you know, WWW WWW (World Wide Web), works by using a client program which will fetch files from remote servers when asked. Usually by clicking a picture or text. This example is a program for the server which will send files to any computer that requests them. The protocol used to send the file is called HTTP. (Hyper-Text Transfer Protocol)

    Usually WWW involves HTML. HTML (Hyper-Text Markup Language) is a way to write documents with embedded pictures and links to other pages. These links are normally displayed underlined and if you click them your WWW- browser will load whatever document that link leads to.

    #!/usr/local/bin/pike
                    
    /* A very small httpd capable of fetching files only. * Written by Fredrik Hübinette as a demonstration of Pike. */
                    
    inherit Stdio.Port;
    We inherit Stdio.Port into this program so we can bind a TCP socket to accept incoming connection. A socket is simply a number to separate communications to and from different programs on the same computer.

    Next are some constants that will affect how uHTTPD will operate. This uses the preprocessor directive #define. The preprocessor is the first stage in the compiling process and can make textual processing of the code before it is compiled. As an example, after the first define below, all occurrences of 'BLOCK' will be replaced with 16060.

    /* Amount of data moved in one operation */
    #define BLOCK 16060
                    
    /* Where do we have the html files ? */
    #define BASE "/usr/local/html/"
                    
    /* File to return when we can't find the file requested */
    #define NOFILE "/user/local/html/nofile.html"
                    
    /* Port to open */
    #define PORT 1905
    A port is a destination for a TCP connection. It is simply a number on the local computer. 1905 is not the standard port for HTTP connections though, which means that if you want to access this WWW server from a browser you need to specify the port like this: http://my.host.my.domain:1905/

    Next we declare a class called output_class. Later we will clone one instance of this class for each incoming HTTP connection.

    class output_class
    {
        inherit Stdio.File : socket;
        inherit Stdio.File : file;
    Our new class inherits Stdio.File twice. To be able to separate them they are then named 'socket' and 'file'.

        int offset=0;
    Then there is a global variable called offset which is initialized to zero. (Each instance of this class will have its own instance of this variable, so it is not truly global, but...) Note that the initialization is done when the class is cloned (or instantiated if you prefer C++ terminology).

    Next we define the function write_callback(). Later the program will go into a 'waiting' state, until something is received to process, or until there is buffer space available to write output to. When that happens a callback will be called to do this. The write_callback() is called when there is buffer space available. In the following lines 'void' means that it does not return a value. Write callback will be used further down as a callback and will be called whenever there is room in the socket output buffer.

        void write_callback()
        {
            int written;
            string data;
    The following line means: call seek in the inherited program 'file'.
            file::seek(offset);
    Move the file pointer to the where we want to the position we want to read from. The file pointer is simply a location in the file, usually it is where the last read() ended and the next will begin. seek() can move this pointer to where we want it though.

            data=file::read(BLOCK);
    Read BLOCK (16060) bytes from the file. If there are less that that left to read only that many bytes will be returned.

            if(strlen(data))
            {
    If we managed to read something...

                written=socket::write(data);
    ... we try to write it to the socket.

                if(written >= 0)
                {
                    offset+=written;
                    return;
                }
    Update offset if we managed to write to the socket without errors.

                werror("Error: "+socket::errno()+".\n");
            }
    If something went wrong during writing, or there was nothing left to read we destruct this instance of this class.

            destruct(this_object());
        }
    That was the end of write_callback()

    Next we need a variable to buffer the input received in. We initialize it to an empty string.

        string input="";
    And then we define the function that will be called when there is something in the socket input buffer. The first argument 'id' is declared as mixed, which means that it can contain any type of value. The second argument is the contents of the input buffer.

        void read_callback(mixed id,string data)
        {
            string cmd;
                    
            input+=data;
    Append data to the string input. Then we check if we have received a a complete line yet. If so we parse this and start outputting the file.

            if(sscanf(input,"%s %s%*[\012\015 \t]",cmd,input)>2)
            {
    This sscanf is pretty complicated, but in essence it means: put the first word in 'input' in 'cmd' and the second in 'input' and return 2 if successful, 0 otherwise.

                if(cmd!="GET")
                {
                    werror("Only method GET is supported.\n");
                    destruct(this_object());
                    return;
                }
    If the first word isn't GET print an error message and terminate this instance of the program. (and thus the connection)
                sscanf(input,"%*[/]%s",input);
    Remove the leading slash.

                input=BASE+combine_path("/",input);
    Combine the requested file with the base of the HTML tree, this gives us a full filename beginning with a slash. The HTML tree is the directory on the server in which the HTML files are located. Normally all files in this directory can be accessed by anybody by using a WWW browser. So if a user requests 'index.html' then that file name is first added to BASE (/home/hubbe/www/html/ in this case) and if that file exists it will be returned to the browser.
                if(!file::open(input,"r"))
                {
    Try opening the file in read-only mode. If this fails, try opening NOFILE instead. Opening the file will enable us to read it later.

    if(!file::open(NOFILE,"r"))
    {
    If this fails too. Write an error message and destruct this object.

        werror("Couldn't find default file.\n");
        destruct(this_object());
        return;
    }
                }
    Ok, now we set up the socket so we can write the data back.
                socket::set_buffer(65536,"w");
    Set the buffer size to 64 kilobytes.

                socket::set_nonblocking(0,write_callback,0);
    Make it so that write_callback is called when it is time to write more data to the socket.

                write_callback();
    Jump-start the writing.
            }
        }
    That was the end of read_callback().

    This function is called if the connection is closed while we are reading from the socket.

        void selfdestruct() { destruct(this_object()); }

    This function is called when the program is instantiated. It is used to set up data the way we want it. Extra arguments to clone() will be sent to this function. In this case it is the object representing the new connection.

        void create(object f)
        {
            socket::assign(f);
    We insert the data from the file f into 'socket'.

            socket::set_nonblocking(read_callback,0,selfdestruct);
    Then we set up the callback functions and sets the file nonblocking. Nonblocking mode means that read() and write() will rather return that wait for I/O to finish. Then we sit back and wait for read_callback to be called.

        }
    End of create()

    };
    End of the new class.

    Next we define the function called when someone connects.

    void accept_callback()
    {
        object tmp_output;
    This creates a local variable of type 'object'. An object variable can contain a clone of any program. Pike does not consider clones of different programs different types. This also means that function calls to objects have to be resolved at run time.

        tmp_output=accept();
    The function accept clones a Stdio.File and makes this equal to the newly connected socket.

        if(!tmp_output) return;
    If it failed we just return.

        output_class(tmp_output);
    Otherwise we clone an instance of 'output_class' and let it take care of the connection. Each clone of output_class will have its own set of global variables, which will enable many connections to be active at the same time without data being mixed up. Note that the programs will not actually run simultaneously though.

        destruct(tmp_output);
    Destruct the object returned by accept(), output_class has already copied the contents of this object.

    }

    Then there is main, the function that gets it all started.
    int main(int argc, array(string) argv)
    {
        werror("Starting minimal httpd\n");
    Write an encouraging message to stderr.
        if(!bind(PORT, accept_callback))
        {
            werror("Failed to open socket (already bound?)\n");
            return 17;
        }

    Bind PORT and set it up to call accept_callback as soon as someone connects to it. If the bind() fails we write an error message and return the 17 to indicate failure.
        return - 17; /* Keep going */
    If everything went ok, we return -17, any negative value returned by main() means that the program WON'T exit, it will hang around waiting for events instead. (like someone connecting)
    }
    That's it, this simple program can be used as the basis for a simple WWW-server. Note that today most WWW servers are very complicated programs, and the above program can never replace a modern WWW server. However, it is very fast if you only want a couple of web pages and have a slow machine available for the server.

    Chapter 10, Threads

    Threads are used to run several Pike functions at the same time without having to start several Pike processes. Using threads often simplifies coding and because the threads are within the same process, data can be shared or sent to other threads very fast. Threads are not supported on all systems, you may test if you have thread support with the preprocessor construction #if constant(thread_create). Pike needs POSIX or UNIX thread support when compiled to support threads.

    10.1 Starting a thread

    Starting a thread is very easy. You simply call thread_create with a function pointer and any arguments it needs and that function will be executed in a separate thread. The function thread_create will return immediately and both the calling function and the called function will execute at the same time. Example:
    void foo(int x)
    {
        for(int e=0;e<5;e++)
        {
            sleep(1);
            write("Hello from thread "+x+".\n");
        }
    }

    int main()
    {
        thread_create(foo, 2);
        thread_create(foo, 3);
        foo(1);
    }
    This may all seem very simple, but there are a few complications to watch out for:
    Accessing unlocked data
    Look at this code:
    void mapadd(mapping m, int i, int j)
    {
        if(map[i])
            map[i]+=({j});
        else
            map[i]=({j});
    }
    This is quite harmless as long as it is only used from one thread at a time, but if two threads call it it at the same time, there is a slight chance that both threads will discover that map[i] is zero and both threads will then do map[i]=({j}); and one value of j will be lost. This type of bug can be extremely hard to debug. The above problem can be solved with the help of Mutexes and Condition variables. Mutexes are basically a way to keep other threads out while a task is being performed. Conditions, or condition variables, are used to inform other threads that they don't have to wait any longer. Pike also provides two different kinds of pipelines to send data from one thread to another, which makes it very simple to write threaded programs. Let's look at an example:
    #!/usr/local/bin/pike
    import Thread; // We need fifos
    inherit Fifo; // Fifo used to supply workers
    inherit Fifo : ended; // Fifo used to wait for workers

    void worker(string lookfor)
    {
        while(string file=Fifo::read())
        {
            int linenum=1;
            object o=Stdio.FILE(file,"r");
            while(string line=o->gets())
            {
                if(search(line, lookfor) >=0)
                    write(sprintf("%s:%d: %s\n",file, linenum, line));

                linenum++;
            }
        }
        ended::write(0);
    }

    int main(int argc, array(string) argv)
    {
        for(int e=0;e<4;e++) // Start workers
            thread_create(worker,argv[e]);
        for(int e=2;e<argc;e++) // Feed workers
            Fifo::write(argv[1]);
        for(int e=0;e<4;e++) // Tell workers to die
            Fifo::write(0);
        for(int e=0;e<4;e++) // Wait for workers to die
            ended::read();
        exit(0);
    }
    This is an example of a simple grep-like program. It looks for the string given as first argument to the program in the files given as the rest of the arguments. Don't worry if you do not understand it yet. Read the descriptions of the functions and classes below and come back and read this example again.
    Deadlocks
    Deadlocks arise when two threads are waiting for each other to do something. This bug can often arise when several threads need access to a number of resources such as files or other I/O devices. What may happen is that one thread has locked device #1 and is trying to lock device #2 while another thread has locked device #2 and is trying to lock device #1. This type of bug is generally easier to find, but may require a lot of work to fix.

    10.2 Threads reference section

    This section describes all thread-related functions and classes.

    FUNCTION
    Thread.thread_create - create a thread

    SYNTAX
    object thread_create(function f, mixed ... args);

    DESCRIPTION
    This function creates a new thread which will run simultaneously to the rest of the program. The new thread will call the function f with the arguments args. When f returns the thread will cease to exist. All Pike functions are 'thread safe' meaning that running a function at the same time from different threads will not corrupt any internal data in the Pike process. The returned value will be the same as the return value of this_thread() for the new thread.

    NOTE
    This function is only available on systems with POSIX or UNIX threads support.

    SEE ALSO
    Thread.Mutex, Thread.Condition and Thread.this_thread

    FUNCTION
    Thread.this_thread - return thread id

    SYNTAX
    object this_thread();

    DESCRIPTION
    This function returns the object that identifies this thread.

    SEE ALSO
    Thread.thread_create

    FUNCTION
    Thread.all_threads - return all thread ids

    SYNTAX
    array(object) all_threads();

    DESCRIPTION
    This function returns an array with the thread ids of all threads.

    SEE ALSO
    Thread.thread_create

    CLASS
    Thread.Mutex - mutex locks

    DESCRIPTION
    Thread.Mutex is a pre-compiled Pike program that implements mutual exclusion locks. Mutex locks are used to prevent multiple threads from simultaneously execute sections of code which access or change shared data. The basic operations for a mutex is locking and unlocking, if a thread attempts to lock an already locked mutex the thread will sleep until the mutex is unlocked.

    NOTE
    Mutex locks are only available on systems with POSIX or UNIX threads support.

    In POSIX threads, mutex locks can only be unlocked by the same thread that locked them. In Pike any thread can unlock a locked mutex.

    EXAMPLE
    /* This simple program can be used to exchange data between two
     * programs. It is similar to Thread.Fifo, but can only hold one
     * element of data.
     */
    
    inherit Thread.Mutex : r_mutex;
    inherit Thread.Mutex : w_mutex;
    object r_lock=r_mutex::lock();
    object w_lock;
    mixed storage;

    void write(mixed data)
    {
        w_lock=w_mutex::lock();
        storage=data;
        destruct(r_lock);
    }

    mixed read()
    {
        mixed tmp;
        r_lock=r_mutex::lock();
        tmp=storage;
        storage=0;
        destruct(w_lock);
        return tmp;
    }

    METHOD
    Thread.Mutex.lock - lock the mutex

    SYNTAX
    object lock();

    DESCRIPTION
    This function attempts to lock the mutex, if the mutex is already locked the current thread will sleep until the lock is unlocked by some other thread. The value returned is the 'key' to the lock. When the key is destructed or has no more references the lock will automatically be unlocked. The key will also be destructed if the lock is destructed.

    METHOD
    Thread.Mutex.trylock - try to lock the mutex

    SYNTAX
    object trylock();

    DESCRIPTION
    This function performs the same operation as lock(), but if the mutex is already locked zero will be returned instead of sleeping until the lock is unlocked.

    CLASS
    Thread.Condition - condition variables

    DESCRIPTION
    Thread.Condition is a pre-compiled Pike program that implements condition variables. Condition variables are used by threaded programs to wait for events happening in other threads.

    NOTE
    Condition variables are only available on systems with POSIX or UNIX threads support.

    EXAMPLE
    // This program implements a fifo that can be used to send
    // data between two threads.
    inherit Thread.Condition : r_cond;
    inherit Thread.Condition: w_cond;
    inherit Thread.Mutex: lock;

    array buffer = allocate(128);
    int r_ptr, w_ptr;

    int query_messages() { return w_ptr - r_ptr; }

    // This function reads one mixed value from the fifo.
    // If no values are available it blocks until a write has been done.
    mixed read()
    {
        mixed tmp;
        // We use this mutex lock to make sure no write() is executed
        // between the query_messages and the wait() call. If it did
        // we would wind up in a deadlock.
        object key=lock::lock();
        while(!query_messages()) r_cond::wait(key);
        tmp=buffer[r_ptr++ % sizeof(buffer)];
        w_cond::signal();
        return tmp;
    }

    // This function pushes one mixed value on the fifo.
    // If the fifo is full it blocks until a value has been read.
    void write(mixed v)
    {
        object key=lock::lock();
        while(query_messages() == sizeof(buffer)) w_cond::wait(key);
        buffer[w_ptr++ % sizeof(buffer)]=v;
        r_cond::signal();
    }

    SEE ALSO
    Thread.Mutex

    METHOD
    Thread.Condition.wait - wait for condition

    SYNTAX
    void wait();
    void wait(object mutex_key);

    DESCRIPTION
    This function makes the current thread sleep until the condition variable is signalled. The optional argument should be the 'key' to a mutex lock. If present the mutex lock will be unlocked before waiting for the condition in one atomic operation. After waiting for the condition the mutex referenced by mutex_key will be re-locked.

    SEE ALSO
    Thread.Mutex->lock

    METHOD
    Thread.Condition.signal - signal a condition variable

    SYNTAX
    void signal();

    DESCRIPTION
    Signal wakes up one of the threads currently waiting for the condition.

    BUGS
    It sometimes wakes up more than one thread.

    METHOD
    Thread.Condition.broadcast - signal all waiting threads

    SYNTAX
    void broadcast();

    DESCRIPTION
    This function wakes up all threads currently waiting for this condition.

    CLASS
    Thread.Fifo - first in, first out object

    DESCRIPTION
    Thread.Fifo implements a fixed length fifo. A fifo is a queue of values and is often used as a stream of data between two threads.

    NOTE
    Fifos are only available on systems with POSIX threads support.

    SEE ALSO
    Thread.Queue

    METHOD
    Thread.Fifo.create - initialize the fifo

    SYNTAX
    void create(int size);
    object(Thread.Fifo) Thread.Fifo();
    object(Thread.Fifo) Thread.Fifo(int size);

    DESCRIPTION
    The function create() is called when the fifo is cloned, if the optional size argument is present it sets how many values can be written to the fifo without blocking. The default size is 128.

    METHOD
    Thread.Fifo.write - queue a value

    SYNTAX
    void write(mixed value);

    DESCRIPTION
    This function puts a value last in the fifo. If there is no more room in the fifo the current thread will sleep until space is available.

    METHOD
    Thread.Fifo.read - read a value from the fifo

    SYNTAX
    mixed read();

    DESCRIPTION
    This function retrieves a value from the fifo. Values will be returned in the order they were written. If there are no values present in the fifo the current thread will sleep until some other thread writes a value to the fifo.

    METHOD
    Thread.Fifo.size - return number of values in fifo

    SYNTAX
    int size();

    DESCRIPTION
    This function returns how many values are currently in the fifo.

    CLASS
    Thread.Queue - a queue of values

    DESCRIPTION
    Thread.Queue implements a queue, or a pipeline. The main difference between Thread.Queue and Thread.Fifo is that queues will never block in write(), only allocate more memory.

    NOTE
    Queues are only available on systems with POSIX or UNIX threads support.

    SEE ALSO
    Thread.Fifo

    METHOD
    Thread.Queue.write - queue a value

    SYNTAX
    void write(mixed value);

    DESCRIPTION
    This function puts a value last in the queue. If the queue is too small to hold the value the queue will be expanded to make room for it.

    METHOD
    Thread.Queue.read - read a value from the queue

    SYNTAX
    mixed read();

    DESCRIPTION
    This function retrieves a value from the queue. Values will be returned in the order they were written. If there are no values present in the queue the current thread will sleep until some other thread writes a value to the queue.

    METHOD
    Thread.Queue.size - return number of values in queue

    SYNTAX
    int queue->size();

    DESCRIPTION
    This function returns how many values are currently in the queue.

    CLASS
    Thread.thread_local - Thread local variable class

    DESCRIPTION
    This class allows you to have variables which are separate for each thread that uses it. It has two methods: get and set. A value stored in an instance of thread_local can only be retreived by that same thread.

    METHOD
    Thread.thread_local.thread_local.set - Set the thread_local value

    SYNTAX
    mixed set(mixed value);

    DESCRIPTION
    This sets the value returned by the get method. Note that this value can only be retreived by the same thread. Calling this method does not affect the value returned by get when called by another thread.

    METHOD
    Thread.thread_local.thread_local.get - Get the thread_local value

    SYNTAX
    mixed set(mixed value);

    DESCRIPTION
    This returns the value prevoiusly stored in the thread_local by the set method by this thread.

    10.3 Threads example

    Let's look at an example of how to work with threads. This program is the same minimal WWW server as in
    chapter 9 "File I/O" but it has been re-written to use threads, as you can see it is a lot smaller this way. This is because we can use blocking I/O operations instead of non-blocking and callbacks. This also makes the program much easier to follow:
    #!/usr/local/bin/pike

    /* A very small threaded httpd capable of fetching files only. * Written by Fredrik Hübinette as a demonstration of Pike */

    import Thread;
    inherit Stdio.Port;

    /* number of bytes to read for each write */
    #define BLOCK 16384

    /* Where do we have the html files ? */
    #define BASE "/home/hubbe/pike/src/"

    /* File to return when we can't find the file requested */
    #define NOFILE "/home/hubbe/www/html/nofile.html"

    /* Port to open */
    #define PORT 1905

    /* Number of threads to start */
    #define THREADS 5

    // There will be one of these for each thread
    class worker
    {
        inherit Stdio.FILE : socket; // For communication with the browser
        inherit Stdio.File : file; // For reading the file from disc

        void create(function accept)
        {
            string cmd, input, tmp;

            while(1)
            {
                socket::close(); // Close previous connection
                file::close();

                object o=accept(); // Accept a connection
                if(!o) continue;
                socket::assign(o);
                destruct(o);

                // Read request
                sscanf(socket::gets(),"%s %s%*[\012\015 \t]",cmd, input);
                if(cmd!="GET")
                {
                    werror("Only method GET is supported.\n");
                    continue;
                }

                // Open the requested file
                sscanf(input,"%*[/]%s",input);
                input=BASE+combine_path("/",input);
                
                if(!file::open(input,"r"))
                {
                    if(!file::open(NOFILE,"r"))
                    {
                        werror("Couldn't find default file.\n");
                        continue;
                    }
                }

                // Copy data to socket
                while(socket::write(file::read(BLOCK))==BLOCK);
            }
        }
    };

    int main(int argc, array(string) argv)
    {
        werror("Starting minimal threaded httpd\n");

        // Bind the port, don't set it nonblocking
        if(!bind(PORT))
        {
            werror("Failed to open socket (already bound?)\n");
            return 17;
        }

        // Start worker threads
        for(int e=1;e<THREADS;e++) thread_create(worker,accept);
        worker(accept);
    }

    As stated in the beginning of this chapter; Pike threads are only available on some UNIX systems. The above example does not work if your system does not have threads.

    Chapter 11, Modules for specific data types

    There are a few modules that provide extra functions that operate specifically on one data type. These modules have the same name as the data type, but are capitalized so you can tell the difference. At the time of writing, the only such modules are String and Array, but more are expected to show up in the future.

    11.1 String

    The module String contains some extra string functionality which is not always used. These functions are mostly implemented in Pike as a complement to those written in C.

    FUNCTION
    String.fuzzymatch - make a fuzzy compare of two strings

    SYNTAX
    int fuzzymatch(string word1, string word2);

    DESCRIPTION
    This function compares two strings using a fuzzy matching routine. The higher the resulting value, the better the strings match.

    EXAMPLE
    > fuzzymatch("cat", "hat");
    Result: 66
    > fuzzymatch("cat", "dog");
    Result: 0
    > fuzzymatch("United States", "United Nations");
    Result: 70

    SEE ALSO
    Array.diff, Array.diff_compare_table and Array.diff_longest_sequence

    FUNCTION
    String.implode_nicely - make an English comma separated list

    SYNTAX
    string implode_nicely(array(string|float|int) words, string|void separator);

    DESCRIPTION
    This function implodes a list of words to a readable string. If the separator is omitted, the default is "and". If the words are numbers they are converet to strings first.

    EXAMPLE
    > implode_nicely(({"green"}));
    Result: green
    > implode_nicely(({"green","blue"}));
    Result: green and blue
    > implode_nicely(({"green","blue","white"}));
    Result: green, blue and white
    > implode_nicely(({"green","blue","white"}),"or");
    Result: green, blue or white
    > implode_nicely(({1,2,3}),"or even");
    Result: 1, 2 or even 3

    SEE ALSO
    `*

    FUNCTION
    String.capitalize - capitalize a string

    SYNTAX
    string capitalize(string str);

    DESCRIPTION
    Convert the first character in str to upper case, and return the new string.

    SEE ALSO
    lower_case and upper_case

    FUNCTION
    String.common_prefix - find the longest common beginning

    SYNTAX
    string common_prefix(array(string) strs);

    DESCRIPTION
    Find the longest common beginning from an array of strings.

    EXAMPLE
    > String.common_prefix(({ "muzzle", "muzzy" }));
    Result: "muzz"
    > String.common_prefix(({ "labyrinth", "diatom" }));
    Result: ""
    > String.common_prefix(({}));
    Result: ""

    FUNCTION
    String.sillycaps - Sillycaps A String

    SYNTAX
    string sillycaps(string str);

    DESCRIPTION
    Convert the first character in each word (separated by spaces) in str to upper case, and return the new string.

    FUNCTION
    String.strmult - multiply strings

    SYNTAX
    string strmult(string s, int num);

    DESCRIPTION
    This function multiplies 's' by 'num'. The return value is the same as appending 's' to an empty string 'num' times.

    FUNCTION
    String.count - count needles in a haystack string

    SYNTAX
    string count(string haystack, string needle);

    DESCRIPTION
    This function counts the number of times the needle can be found in haystack. Intersections between needles are not counted, ie count("....","..") is 2.

    FUNCTION
    String.width - return width of string

    SYNTAX
    int width(string s);

    DESCRIPTION
    Returns the width in bits (8, 16 or 32) of the widest character in s.

    FUNCTION
    String.trim_whites - trim spaces and tabs from a string

    SYNTAX
    string trim_whites(string s);

    DESCRIPTION
    Trim leading and trailing spaces and tabs from the string s.

    FUNCTION
    String.trim_all_whites - trim all white spaces from a string

    SYNTAX
    string trim_all_whites(string s);

    DESCRIPTION
    Trim leading and trailing white space characters (" \t\r\n") from the string s.

    11.2 Array

    As with String these functions are mostly Pike functions written to supplement those written in C.

    FUNCTION
    Array.diff - gives the difference of two arrays

    SYNTAX
    array(array(array)) diff(array a, array b);

    DESCRIPTION
    Calculates which parts of the arrays that are common to both, and which parts that are not. Returns an array with two elements, the first is an array of parts in array a, and the second is an array of parts in array b.

    EXAMPLE
    > Array.diff("Hello world!"/"","Help!"/"");
    Result: ({ /* 2 elements */
        ({ /* 3 elements */
            ({ /* 3 elements */
                "H",
                "e",
                "l"
            }),
            ({ /* 8 elements */
                "l",
                "o",
                " ",
                "w",
                "o",
                "r",
                "l",
                "d"
            }),
            ({ /* 1 elements */
                "!"
            })
        }),
        ({ /* 3 elements */
            ({ /* 3 elements */
                "H",
                "e",
                "l"
            }),
            ({ /* 1 elements */
                "p"
            }),
            ({ /* 1 elements */
                "!"
            })
        })
    })
    

    SEE ALSO
    Array.diff_compare_table, Array.diff_longest_sequence and String.fuzzymatch

    FUNCTION
    Array.diff_compare_table - gives the comparison-table used by Array.diff()

    SYNTAX
    array(array(int)) diff_compare_table(array a, array b);

    DESCRIPTION
    Returns an array which maps from index in a to corresponding indices in b.

    EXAMPLE
    > Array.diff_compare_table("Hello world!"/"","Help!"/"");
    Result: ({ /* 12 elements */
        ({ /* 1 elements */
            0
        }),
        ({ /* 1 elements */
            1
        }),
        ({ /* 1 elements */
            2
        }),
        ({ /* 1 elements */
            2
        }),
        ({ }),
        ({ }),
        ({ }),
        ({ }),
        ({ }),
        ({ /* 1 elements */
            2
        }),
        ({ }),
        ({ /* 1 elements */
            4
        })
    })
    

    SEE ALSO
    Array.diff, Array.diff_longest_sequence and String.fuzzymatch

    FUNCTION
    Array.diff_longest_sequence - gives the longest common sequence of two arrays

    SYNTAX
    array(int) diff_longest_sequence(array a, array b);

    DESCRIPTION
    Gives the longest sequence of indices in b that have corresponding values in the same order in a.

    EXAMPLE
    > Array.diff_longest_sequence("Hello world!"/"","Help!"/"");
    Result: ({ /* 4 elements */
        0,
        1,
        2,
        4
    })

    SEE ALSO
    Array.diff, Array.diff_compare_table and String.fuzzymatch

    FUNCTION
    Array.everynth - return every n:th element of an array

    SYNTAX
    array(mixed) Array.everynth(array(mixed) arr1, void|int nth, void|int start);

    DESCRIPTION
    This function returns an array with every n:th element of an other array. If nth is zero, every second element is returned.

    EXAMPLE
    > Array.everynth(({"1","2","3","4","5","6"}),2,1);
    Result: ({ /* 3 elements */
        "2",
        "4",
        "6"
    })
    

    SEE ALSO
    Array.splice and `/

    FUNCTION
    Array.filter - filter an array or mapping through a function

    SYNTAX
    array filter(array arr,function fun,mixed ... args);
    array filter(array(object) arr,string fun,mixed ... args);
    array filter(array(function) arr,-1,mixed ... args);

    DESCRIPTION
    First syntax:
    Filter array returns an array holding the items of arr for which fun returns true.

    Second syntax:
    Filter array calls fun in all the objects in the array arr, and return all objects that returned true.

    Third syntax:
    Filter array calls all function pointers in the array arr, and return all that returned true.

    SEE ALSO
    Array.sum_arrays and Array.map

    FUNCTION
    Array.longest_ordered_sequence - find the longest ordered sequence of elements

    SYNTAX
    array(int) longest_ordered_sequence(array a);

    DESCRIPTION
    This function returns an array of the indices in the longest ordered sequence of elements in the array.

    EXAMPLE
    > array a = ({ 1,2,3,4,2,3,5,6 });
    Result: ({ /* 8 elements */
        1,
        2,
        3,
        4,
        2,
        3,
        5,
        6
    })
    > array seq = Array.longest_ordered_sequence(a);
    Result: ({ /* 6 elements */
        0,
        1,
        4,
        5,
        6,
        7
    })
    > rows(a,seq);
    Result: ({ /* 6 elements */
        1,
        2,
        2,
        3,
        5,
        6
    })
    

    SEE ALSO
    Array.diff

    FUNCTION
    Array.map - map an array or mapping over a function

    SYNTAX
    array map(array arr,function fun,mixed ... args);
    array map(array(object) arr,string fun,mixed ... args);
    array map(array(function) arr,-1,mixed ... arg);

    DESCRIPTION
    First syntax:
    Map array returns an array holding the items of arr mapped through the function fun. i.e. arr[x]=fun(arr[x], @args) for all x.

    Second syntax:
    Map array calls function fun in all objects in the array arr. i.e. arr[x]=arr[x]->fun(@ args);

    Third syntax:
    Map array calls the functions in the array arr: arr[x]=arr[x]->fun(@ args);

    SEE ALSO
    Array.sum_arrays and Array.filter

    FUNCTION
    Array.permute - Give a specified permutation of an array

    SYNTAX
    array permute(array in,int number);

    DESCRIPTION
    permute gives you a selected permutation of an array. The numbers of permutations equal sizeof(in)! (the factorial of the size of the given array).

    EXAMPLE
    Array.permute( ({ 1,2,3 }), 0) -> ({1,2,3})
    Array.permute( ({ 1,2,3 }), 3) -> ({1,3,2})

    SEE ALSO
    Array.shuffle

    FUNCTION
    Array.reduce - Iterativily apply a function on an array

    SYNTAX
    mixed reduce(function f, array arr, void|mixed zero);

    DESCRIPTION
    reduce sends the first two elements in arr to f, then the result and the next element in arr to f and so on. Then it returns the result. The function will return zero if arr is the empty array. If arr has size 1 it will return the element in arr.

    EXAMPLE
    Array.reduce(aggregate, indices(allocate(4)))  ->
       ({ ({ ({ 0, 1 }), 2 }), 3 })
    Array.reduce(`+, ({}), "FOO?")  ->  
       "FOO?"
    Array.reduce(lambda(int a, int b) {
                 while(b) { int t=b; b=a%b; a=t; }
                 return a;
                 }, ({7191,21573,64719,33694}))
      -> 17
    

    SEE ALSO
    Array.rreduce

    FUNCTION
    Array.rreduce - Iterativily apply a function on an array backwards

    SYNTAX
    mixed rreduce(function f, array arr, void|mixed zero);

    DESCRIPTION
    reduce sends the last two elements in arr to f, then the third last element in arr and the result to f and so on. Then it returns the result. The function will return zero if arr is the empty array. If arr has size 1 it will return the element in arr.

    EXAMPLE
    Array.rreduce(aggregate, indices(allocate(4)))  ->
       ({ 0, ({ 1, ({ 2, 3 }) }) })
    

    SEE ALSO
    Array.reduce

    FUNCTION
    Array.search_array - search for something in an array

    SYNTAX
    int search_array(array arr,function fun,mixed arg, ...);
    int search_array(array(object) arr,string fun,mixed arg, ...);
    int search_array(array(function) arr,-1,mixed arg, ...);

    DESCRIPTION
    search_array works like map_array, only it returns the index of the first call that returned true instead or returning an array of the returned values. If no call returns true, -1 is returned.

    SEE ALSO
    Array.sum_arrays and Array.filter

    FUNCTION
    Array.shuffle - Random-shuffle an array

    SYNTAX
    array shuffle(array in);

    DESCRIPTION
    shuffle gives back the same elements, but by random order.

    EXAMPLE
    Array.shuffle( ({"this","is","an","ordered","array"}) ) -> ({"is","ordered","array","this","an"})

    SEE ALSO
    Array.permute

    FUNCTION
    Array.sort_array - sort an array

    SYNTAX
    array sort_array(array arr,function fun,mixed ... args);

    DESCRIPTION
    This function sorts an array after a compare-function fun which takes two arguments and should return 1 if the first argument is larger then the second. The rest of the arguments args will be sent as 3rd, 4th etc. argument to fun. If fun is omitted, `> is used instead.

    SEE ALSO
    Array.map and sort

    FUNCTION
    Array.splice - splice two or more arrays

    SYNTAX
    array(mixed) Array.splice(array(mixed) arr1, array(mixed) arr2, array(mixed) ...);

    DESCRIPTION
    This function splice two or more arrays together. This means that the the array becomes an array of the first element in the first given array, the first argument in next array and so on for all arrays. Then the second elements are added.

    EXAMPLE
    > Array.splice(({1,2,3}),({"1","2","3"}),({"a","b","c"}));
    Result: ({ /* 9 elements */
        1,
        "1",
        "a",
        2,
        "2",
        "b",
        3,
        "3",
        "c"
    })
    > Array.splice(({1,2,3}),({"1","2","3"}),({"a","b"}));
    Result: ({ /* 9 elements */
        1,
        "1",
        "a",
        2,
        "2",
        "b",
    })
    

    SEE ALSO
    `/, `*, `+, `- and Array.everynth

    FUNCTION
    Array.sum_arrays - map any number of arrays over a function

    SYNTAX
    array sum_arrays(function fun,array arr1,...);

    DESCRIPTION
    Works like this:
    array sum_arrays(function fun,array arr1,...)
    {
        int e;
        array res=allocate(sizeof(arr1));
        for(e=0;e<sizeof(arr1);e++)
        {
            res[e]=fun(arr1[e],arr2[e],...);
        }
        return res;
    }

    Simple ehh?

    SEE ALSO
    Array.map, Array.filter and Array.search_array

    FUNCTION
    Array.uniq - remove elements that are duplicates

    SYNTAX
    array uniq(array a);

    DESCRIPTION
    This function returns an copy of the array a with all duplicate values removed. The order of the values in the result is undefined.

    Chapter 12, Image

    The Image module is used to manipulate bit-mapped color images. It can read PPM images and do various manipulations, or it can be used to create completely new images. The created images can be saved as PPM or converted to GIF.

    All images handled by this module are stored as 24-bit RGB images. This means that a 1024 pixel wide and 1024 pixel high image will use 1024*1024*3 bytes = 3 megabytes. It is quite easy to mess up and use up all the memory by giving the wrong argument to one of the scaling functions.

    Most functions in this module work by creating a new Image and then returning that instead of changing the Image you are working with. This makes it possible to share the same image between many variables without having to worry that it will be changed by accident. This can reduce the amount of memory used.

    Many functions in this module work with the 'current color', this can be thought of as the background color if you wish. To change the current color you use 'setcolor'.

    Let's look at an example of how this can be used:

    #!/usr/local/bin/pike

    int main()
    {
        write("Content-type: image/gif\n\n");
        object font=Image.font();
        font->load("testfont");
        object image=font->write(ctime(time));
        write(Image.GIF.encode(image));
    }
    This very simple example can be used as a CGI script to produce a gif image which says what time it is in white text on a black background.

    DESCRIPTION
    This module adds image-drawing and -manipulating capabilities to pike.

    Image.Image Basic image manipulation
    Image.Font Creating images from text
    Image.Colortable Color reduction, quantisation and dither
    Image.Color Color names, objects and conversion
    encoding/decoding image files
    Image.GIF GIF encoding/decoding capabilities
    Image.JPEG JPEG encoding/decoding capabilities (needs libjpeg)
    Image.PNG PNG encoding/decoding capabilities (needs libz)
    Image.PNM PNM (PBM/PGM/PPM) encoding/decoding capabilities
    Image.XFace XFace encoding/decoding capabilities (needs libgmp)
    Image.XWD XWD (X-windows dump) decoding capabilities
    advanced
    Image.X module for supporting X-windows low-level formats, keeping a lot of bit-mending stuff.
    $Id: module.pmod,v 1.8 2000/03/22 18:12:19 peter Exp $

    NOTE
    Image module documentation is based on these file versions:
    
    

    $Id: blit.c,v 1.37 1999/06/18 19:19:14 mirar Exp $ $Id: blit_layer_include.h,v 1.6 1999/04/13 12:32:11 mirar Exp $ $Id: colors.c,v 1.30 2000/02/03 19:02:22 grubba Exp $ $Id: colortable.c,v 1.77 1999/10/30 11:51:41 mirar Exp $ $Id: colortable.h,v 1.17 1999/04/11 12:55:43 mirar Exp $ $Id: colortable_lookup.h,v 1.9 1999/04/10 02:02:07 mirar Exp $ $Id: dct.c,v 1.14 1999/06/18 19:19:20 mirar Exp $ $Id: any.c,v 1.16 2000/03/22 18:12:19 peter Exp $ $Id: bmp.c,v 1.20 2000/02/03 12:25:18 grubba Exp $ $Id: gif.c,v 1.50 1999/11/14 22:16:08 mast Exp $ $Id: gif_lzw.c,v 1.6 1999/05/30 20:11:14 mirar Exp $ $Id: gif_lzw.h,v 1.7 1999/05/30 20:11:15 mirar Exp $ $Id: ilbm.c,v 1.13 2000/02/03 19:01:29 grubba Exp $ $Id: pnm.c,v 1.20 1999/06/19 11:08:00 mirar Exp $ $Id: x.c,v 1.26 2000/03/27 07:42:35 hubbe Exp $ $Id: xwd.c,v 1.13 2000/01/11 01:42:36 mast Exp $ $Id: font.c,v 1.58 2000/03/25 23:34:32 hubbe Exp $ $Id: image.c,v 1.160 2000/04/09 06:15:17 per Exp $ $Id: image.h,v 1.35 1999/09/25 19:58:48 grubba Exp $ $Id: layers.c,v 1.43 2000/02/22 03:41:27 per Exp $ $Id: matrix.c,v 1.22 2000/04/09 06:15:17 per Exp $ $Id: operator.c,v 1.25 1999/09/14 23:17:15 marcus Exp $ $Id: orient.c,v 1.13 1999/06/19 20:24:48 hubbe Exp $ $Id: pattern.c,v 1.18 1999/07/16 11:44:20 mirar Exp $ $Id: poly.c,v 1.3 1999/07/28 09:26:20 mirar Exp $ $Id: polyfill.c,v 1.30 1999/06/19 20:24:49 hubbe Exp $ Experimental functions.

    METHOD
    Image.lay

    SYNTAX
    Image.Layer lay(array(Image.Layer|mapping))
    Image.Layer lay(array(Image.Layer|mapping), int xoffset, int yoffset, int xsize, int ysize)

    DESCRIPTION
    Combine layers.

    RETURNS
    a new layer object.

    SEE ALSO
    Image.Layer

    METHOD
    Image.load,
    Image.load_layer,
    Image._load

    SYNTAX
    object(Image.Image) load()
    object(Image.Image) load(object file)
    object(Image.Image) load(string filename)
    object(Image.Layer) load_layer()
    object(Image.Layer) load_layer(object file)
    object(Image.Layer) load_layer(string filename)
    mapping _load()
    mapping _load(object file)
    mapping _load(string filename)

    DESCRIPTION
    Helper function to load an image from a file. If no filename is given, Stdio.stdin is used. The result is the same as from the decode functions in Image.ANY.

    NOTE
    All data is read, ie nothing happens until the file is closed. Throws upon error.

    12.1 Image.Image

    CLASS
    Image.Image

    DESCRIPTION
    The main object of the Image module, this object is used as drawing area, mask or result of operations.

    basic:
    clear, clone, create, xsize, ysize

    plain drawing:
    box, circle, getpixel, line, setcolor, setpixel, threshold, polyfill

    operators:
    `&, `*, `+, `-, `==, `>, `<, `|

    pasting images:
    paste, paste_alpha, paste_alpha_color, paste_mask

    getting subimages, scaling, rotating:
    autocrop, clone, copy, dct, mirrorx, rotate, rotate_ccw, rotate_cw, rotate_expand, scale, skewx, skewx_expand, skewy, skewy_expand

    calculation by pixels:
    apply_matrix, change_color, color, distancesq, grey, invert, modify_by_intensity, outline select_from, rgb_to_hsv, hsv_to_rgb,

    average, max, min, sum, sumf, find_min, find_max

    special pattern drawing:
    noise, turbulence, test, tuned_box, gradients, random

    SEE ALSO
    Image, Image.Font, Image.Colortable and Image.X

    METHOD
    Image.Image.apply_matrix

    SYNTAX
    object apply_matrix(array(array(int|array(int))) matrix)
    object apply_matrix(array(array(int|array(int))) matrix, int r, int g, int b)
    object apply_matrix(array(array(int|array(int))) matrix, int r, int g, int b, int|float div)

    DESCRIPTION
    Applies a pixel-transform matrix, or filter, to the image.

    2   2
    pixel(x,y)= base+ k ( sum sum pixel(x+k-1,y+l-1)*matrix(k,l) )
    k=0 l=0
    

    1/k is sum of matrix, or sum of matrix multiplied with div. base is given by r,g,b and is normally black.

    blur (ie a 2d gauss function):
    ({({1,2,1}),
    ({2,5,2}),
    ({1,2,1})})
    
    original
    sharpen (k>8, preferably 12 or 16):
    ({({-1,-1,-1}),
    ({-1, k,-1}),
    ({-1,-1,-1})})
    
    edge detect:
    ({({1, 1,1}),
    ({1,-8,1}),
    ({1, 1,1})})
    
    horisontal edge detect (get the idea):
    ({({0, 0,0}),
    ({1,-2,1}),
    ({0, 0,0})})
    
    emboss (might prefer to begin with a grey image):
    ({({2, 1, 0}),
    ({1, 0,-1}),
    ({0,-1,-2})}), 128,128,128, 3
    
    greyed

    This function is not very fast.

    ARGUMENTS
    argument(s)description
    array(array(int|array(int)))
    the matrix; innermost is a value or an array with red, green, blue values for red, green, blue separation.
    int r
    int g
    int b
    base level of result, default is zero
    int|float div
    division factor, default is 1.0.

    RETURNS
    the new image object

    METHOD
    Image.Image.apply_max

    SYNTAX
    object apply_max(array(array(int|array(int))) matrix)
    object apply_max(array(array(int|array(int))) matrix, int r, int g, int b)
    object apply_max(array(array(int|array(int))) matrix, int r, int g, int b, int|float div)

    DESCRIPTION
    This is the same as apply_matrix, but it uses the maximum instead.

    This function is not very fast.

    ARGUMENTS
    argument(s)description
    array(array(int|array(int)))
    the matrix; innermost is a value or an array with red, green, blue values for red, green, blue separation.
    int r
    int g
    int b
    base level of result, default is zero
    int|float div
    division factor, default is 1.0.

    RETURNS
    the new image object

    NOTE
    experimental status; may not be exact the same output in later versions

    METHOD
    Image.Image.autocrop,
    Image.Image.find_autocrop

    SYNTAX
    object autocrop()
    object autocrop(int border)
    object autocrop(int border, Color color)
    object autocrop(int border, int left, int right, int top, int bottom)
    object autocrop(int border, int left, int right, int top, int bottom, Color color)
    array(int) find_autocrop()
    array(int) find_autocrop(int border)
    array(int) find_autocrop(int border, int left, int right, int top, int bottom)

    DESCRIPTION
    Removes "unneccesary" borders around the image, adds one of its own if wanted to, in selected directions.

    "Unneccesary" is all pixels that are equal -- ie if all the same pixels to the left are the same color, that column of pixels are removed.

    The find_autocrop() function simply returns x1,y1,x2,y2 for the kept area. (This can be used with copy later.)

    ARGUMENTS
    argument(s)description
    int border
    int left
    int right
    int top
    int bottom
    which borders to scan and cut the image; a typical example is removing the top and bottom unneccesary pixels:
    img=img->autocrop(0, 0,0,1,1);

    RETURNS
    the new image object

    SEE ALSO
    copy

    METHOD
    Image.Image.max,
    Image.Image.min,
    Image.Image.sumf,
    Image.Image.sum,
    Image.Image.average

    SYNTAX
    array(float) average()
    array(int) min()
    array(int) max()
    array(int) sum()
    array(float) sumf()

    DESCRIPTION
    Gives back the average, minimum, maximum color value, and the sum of all pixel's color value.

    NOTE
    sum() values can wrap! Most systems only have 31 bits available for positive integers. (Meaning, be careful with images that have more than 8425104 pixels.)

    average() and sumf() may also wrap, but on a line basis. (Meaning, be careful with images that are wider than 8425104 pixels.) These functions may have a precision problem instead, during to limits in the 'double' C type and/or 'float' Pike type.

    METHOD
    Image.Image.bitscale

    SYNTAX
    object bitscale(float factor)
    object bitscale(float xfactor, float yfactor)

    DESCRIPTION
    scales the image with a factor, without smoothing. This routine is faster than scale, but gives less correct results

    ARGUMENTS
    argument(s)description
    float factor
    factor to use for both x and y
    float xfactor
    float yfactor
    separate factors for x and y

    RETURNS
    the new image object

    METHOD
    Image.Image.bitscale

    SYNTAX
    object bitscale(int newxsize, int newysize)
    object bitscale(0, int newysize)
    object bitscale(int newxsize, 0)

    DESCRIPTION
    scales the image to a specified new size, if one of newxsize or newysize is 0, the image aspect ratio is preserved.

    ARGUMENTS
    argument(s)description
    int newxsize
    int newysize
    new image size in pixels

    RETURNS
    the new image object

    NOTE
    resulting image will be 1x1 pixels, at least

    METHOD
    Image.Image.box

    SYNTAX
    object box(int x1, int y1, int x2, int y2)
    object box(int x1, int y1, int x2, int y2, int r, int g, int b)
    object box(int x1, int y1, int x2, int y2, int r, int g, int b, int alpha)

    DESCRIPTION
    Draws a filled rectangle on the image.

    original ->box(40,10,10,80,0,255,0)

    ARGUMENTS
    argument(s)description
    int x1
    int y1
    int x2
    int y2
    box corners
    int r
    int g
    int b
    color of the box
    int alpha
    alpha value

    RETURNS
    the object called

    METHOD
    Image.Image.cast

    SYNTAX
    string cast(string type)

    DESCRIPTION
    Cast the image to another datatype. Currently supported are string ("rgbrgbrgb...") and array (double array of Image.Color objects).

    SEE ALSO
    Image.Color and Image.X

    METHOD
    Image.Image.change_color

    SYNTAX
    object change_color(int tor, int tog, int tob)
    object change_color(int fromr, int fromg, int fromb,  int tor, int tog, int tob)

    DESCRIPTION
    Changes one color (exakt match) to another. If non-exakt-match is preferred, check distancesq and paste_alpha_color.

    ARGUMENTS
    argument(s)description
    int tor
    int tog
    int tob
    destination color and next current color
    int fromr
    int fromg
    int fromb
    source color, default is current color

    RETURNS
    a new (the destination) image object

    METHOD
    Image.Image.circle

    SYNTAX
    object circle(int x, int y, int rx, int ry)
    object circle(int x, int y, int rx, int ry, int r, int g, int b)
    object circle(int x, int y, int rx, int ry, int r, int g, int b, int alpha)

    DESCRIPTION
    Draws a circle on the image. The circle is not antialiased.

    original ->circle(50,50,30,50,0,255,255)

    ARGUMENTS
    argument(s)description
    int x
    int y
    circle center
    int rx
    int ry
    circle radius in pixels
    int r
    int g
    int b
    color
    int alpha
    alpha value

    RETURNS
    the object called

    METHOD
    Image.Image.clear

    SYNTAX
    void clear()
    void clear(int r, int g, int b)
    void clear(int r, int g, int b, int alpha)

    DESCRIPTION
    gives a new, cleared image with the same size of drawing area

    original ->clear(0,128,255)

    ARGUMENTS
    argument(s)description
    int r
    int g
    int b
    color of the new image
    int alpha
    new default alpha channel value

    SEE ALSO
    copy and clone

    METHOD
    Image.Image.clone

    SYNTAX
    object clone()
    object clone(int xsize, int ysize)
    object clone(int xsize, int ysize, int r, int g, int b)
    object clone(int xsize, int ysize, int r, int g, int b, int alpha)

    DESCRIPTION
    Copies to or initialize a new image object.

    original clone clone(50,50)

    ARGUMENTS
    argument(s)description
    int xsize
    int ysize
    size of (new) image in pixels, called image is cropped to that size
    int r
    int g
    int b
    current color of the new image, default is black. Will also be the background color if the cloned image is empty (no drawing area made).
    int alpha
    new default alpha channel value

    RETURNS
    the new object

    SEE ALSO
    copy and create

    METHOD
    Image.Image.color

    SYNTAX
    object color()
    object color(int value)
    object color(int r, int g, int b)

    DESCRIPTION
    Colorize an image.

    The red, green and blue values of the pixels are multiplied with the given value(s). This works best on a grey image...

    The result is divided by 255, giving correct pixel values.

    If no arguments are given, the current color is used as factors.

    original ->color(128,128,255);

    ARGUMENTS
    argument(s)description
    int r
    int g
    int b
    red, green, blue factors
    int value
    factor

    RETURNS
    the new image object

    SEE ALSO
    grey, `* and modify_by_intensity

    METHOD
    Image.Image.copy

    SYNTAX
    object copy()
    object copy(int x1, int y1, int x2, int y2)
    object copy(int x1, int y1, int x2, int y2, int r, int g, int b)
    object copy(int x1, int y1, int x2, int y2, int r, int g, int b, int alpha)

    DESCRIPTION
    Copies this part of the image. The requested area can be smaller, giving a cropped image, or bigger - the new area will be filled with the given or current color.

    original ->copy(5,5,XSIZE-6,YSIZE-6) ->copy(-5,-5,XSIZE+4,YSIZE+4,10,75,10)

    ARGUMENTS
    argument(s)description
    int x1
    int y1
    int x2
    int y2
    The requested new area. Default is the old image size.
    int r
    int g
    int b
    color of the new image
    int alpha
    new default alpha channel value

    RETURNS
    a new image object

    NOTE
    clone(void) and copy(void) does the same operation

    SEE ALSO
    clone and autocrop

    METHOD
    Image.Image.create

    SYNTAX
    void create()
    void create(int xsize, int ysize)
    void create(int xsize, int ysize, Color color)
    void create(int xsize, int ysize, int r, int g, int b)
    void create(int xsize, int ysize, int r, int g, int b, int alpha)
    void create(int xsize, int ysize, string method, method ...)

    DESCRIPTION
    Initializes a new image object.

    Image.Image(XSIZE,YSIZE) Image.Image(XSIZE,YSIZE,255,128,0)

    The image can also be calculated from some special methods, for convinience:

    channel modes; followed by a number of 1-char-per-pixel strings
    or image objects (where red channel will be used),
    or an integer value:
    "grey" : make a grey image (needs 1 source: grey)
    "rgb"  : make an rgb image (needs 3 sources: red, green and blue)
    "cmyk" : make a rgb image from cmyk (cyan, magenta, yellow, black)
    
    

    generate modes; all extra arguments is given to the generation function. These has the same name as the method: "test," "gradients" "noise" "turbulence" "random" "randomgrey" specials cases: "tuned_box" (coordinates is automatic)

    ARGUMENTS
    argument(s)description
    int xsize
    int ysize
    size of (new) image in pixels
    Color color
    int r
    int g
    int b
    background color (will also be current color), default color is black
    int alpha
    default alpha channel value

    BUGS
    SIGSEGVS can be caused if the size is too big, due to unchecked overflow - (xsize*ysize)&MAXINT is small enough to allocate.

    SEE ALSO
    copy, clone and Image.Image

    METHOD
    Image.Image.dct

    SYNTAX
    object dct(int newx, int newy)

    DESCRIPTION
    Scales the image to a new size.

    Method for scaling is rather complex; the image is transformed via a cosine transform, and then resampled back.

    This gives a quality-conserving upscale, but the algorithm used is n*n+n*m, where n and m is pixels in the original and new image.

    Recommended wrapping algorithm is to scale overlapping parts of the image-to-be-scaled.

    This functionality is actually added as an true experiment, but works...

    ARGUMENTS
    argument(s)description
    int newx
    int newy
    new image size in pixels

    RETURNS
    the new image object

    NOTE
    Do NOT use this function if you don't know what you're dealing with! Read some signal theory first...

    It write's dots on stderr, to indicate some sort of progress. It doesn't use any fct (compare: fft) algorithms.

    METHOD
    Image.Image.distancesq

    SYNTAX
    object distancesq()
    object distancesq(int r, int g, int b)

    DESCRIPTION
    Makes an grey-scale image, for alpha-channel use.

    The given value (or current color) are used for coordinates in the color cube. Each resulting pixel is the distance from this point to the source pixel color, in the color cube, squared, rightshifted 8 steps:

    p = pixel color
    o = given color
    d = destination pixel
    d.red=d.blue=d.green=
    ((o.red-p.red)²+(o.green-p.green)²+(o.blue-p.blue)²)>>8
    

    original distance² to cyan ...to purple ...to yellow

    ARGUMENTS
    argument(s)description
    int r
    int g
    int b
    red, green, blue coordinates

    RETURNS
    the new image object

    SEE ALSO
    select_from

    METHOD
    Image.Image.find_min,
    Image.Image.find_max

    SYNTAX
    array(int) find_min()
    array(int) find_max()
    array(int) find_min(int r, int g, int b)
    array(int) find_max(int r, int g, int b)

    DESCRIPTION
    Gives back the position of the minimum or maximum pixel value, weighted to grey.

    ARGUMENTS
    argument(s)description
    int r
    int g
    int b
    weight of color, default is r=87,g=127,b=41, same as the grey() method.

    METHOD
    Image.Image.gamma

    SYNTAX
    object gamma(float g)
    object gamma(float gred, ggreen, gblue)

    DESCRIPTION
    Calculate pixels in image by gamma curve.

    Intensity of new pixels are calculated by:
    i' = i^g

    For example, you are viewing your image on a screen with gamma 2.2. To correct your image to the correct gamma value, do something like:

    my_display_image(my_image()->gamma(1/2.2);

    ARGUMENTS
    argument(s)description
    int g
    int gred
    int ggreen
    int gblue
    gamma value

    RETURNS
    a new image object

    SEE ALSO
    grey, `* and color

    METHOD
    Image.Image.getpixel

    SYNTAX
    array(int) getpixel(int x, int y)

    DESCRIPTION

    ARGUMENTS
    argument(s)description
    int x
    int y
    position of the pixel

    RETURNS
    color of the requested pixel -- ({int red,int green,int blue})

    METHOD
    Image.Image.gradients

    SYNTAX
    int gradients(array(int) point,  ...)
    int gradients(array(int) point,  ...,  float grad)

    DESCRIPTION
    original 2 color
    gradient
    10 color
    gradient
    3 colors,
    grad=4.0
    3 colors,
    grad=1.0
    3 colors,
    grad=0.25

    RETURNS
    the new image

    METHOD
    Image.Image.grey

    SYNTAX
    object grey()
    object grey(int r, int g, int b)

    DESCRIPTION
    Makes a grey-scale image (with weighted values).

    original ->grey(); ->grey(0,0,255);

    ARGUMENTS
    argument(s)description
    int r
    int g
    int b
    weight of color, default is r=87,g=127,b=41, which should be pretty accurate of what the eyes see...

    RETURNS
    the new image object

    SEE ALSO
    color, `* and modify_by_intensity

    METHOD
    Image.Image.rgb_to_hsv,
    Image.Image.hsv_to_rgb

    SYNTAX
    object rgb_to_hsv()
    object hsv_to_rgb()

    DESCRIPTION
    Converts RGB data to HSV data, or the other way around. When converting to HSV, the resulting data is stored like this: pixel.r = h; pixel.g = s; pixel.b = v;

    When converting to RGB, the input data is asumed to be placed in the pixels as above.

    original ->hsv_to_rgb(); ->rgb_to_hsv();
    tuned box (below) the rainbow (below) same, but rgb_to_hsv()

    HSV to RGB calculation:

    in = input pixel
    out = destination pixel
    h=-pos*c_angle*3.1415/(float)NUM_SQUARES;
    out.r=(in.b+in.g*cos(in.r));
    out.g=(in.b+in.g*cos(in.r + pi*2/3));
    out.b=(in.b+in.g*cos(in.r + pi*4/3));
    

    RGB to HSV calculation: Hmm.

    
    
    

    Example: Nice rainbow.

    object i = Image.Image(200,200);
    i = i->tuned_box(0,0, 200,200,
    ({ ({ 255,255,128 }), ({ 0,255,128 }),
    ({ 255,255,255 }), ({ 0,255,255 })}))
    ->hsv_to_rgb();
    

    RETURNS
    the new image object

    METHOD
    Image.Image.invert

    SYNTAX
    object invert()

    DESCRIPTION
    Invert an image. Each pixel value gets to be 255-x, where x is the old value.

    original ->invert(); ->rgb_to_hsv()->invert()->hsv_to_rgb();

    RETURNS
    the new image object

    METHOD
    Image.Image.line

    SYNTAX
    object line(int x1, int y1, int x2, int y2)
    object line(int x1, int y1, int x2, int y2, int r, int g, int b)
    object line(int x1, int y1, int x2, int y2, int r, int g, int b, int alpha)

    DESCRIPTION
    Draws a line on the image. The line is not antialiased.

    original ->line(50,10,10,50,255,0,0)

    ARGUMENTS
    argument(s)description
    int x1
    int y1
    int x2
    int y2
    line endpoints
    int r
    int g
    int b
    color
    int alpha
    alpha value

    RETURNS
    the object called

    METHOD
    Image.Image.make_ascii

    SYNTAX
    string make_ascii(object orient1,  object orient2,  object orient3,  object orient4,  int|void xsize,  int|void ysize)

    DESCRIPTION
    This method creates a string that looks like the image. Example:
    //Stina is an image with a cat.
    array(object) Stina4=Stina->orient4();
    Stina4[1]*=215;
    Stina4[3]*=215;
    string foo=Stina->make_ascii(@Stina4,40,4,8);
    

    RETURNS
    some nice acsii-art.

    NOTE
    experimental status; may not be exact the same output in later versions
          |      /    -    \
    hue=  0     64   128  192  (=red in an hsv image)
    

    SEE ALSO
    orient and orient4

    METHOD
    Image.Image.map_closest,
    Image.Image.select_colors,
    Image.Image.map_fast,
    Image.Image.map_fs

    SYNTAX
    object map_closest(array(array(int)) colors)
    object map_fast(array(array(int)) colors)
    object map_fs(array(array(int)) colors)
    array select_colors(int num)

    DESCRIPTION
    Compatibility functions. Do not use!

    Replacement examples:

    Old code:

    img=map_fs(img->select_colors(200));
    New code:
    img=Image.Colortable(img,200)->floyd_steinberg()->map(img);

    Old code:

    img=map_closest(img->select_colors(17)+({({255,255,255}),({0,0,0})}));
    New code:
    img=Image.Colortable(img,19,({({255,255,255}),({0,0,0})}))->map(img);

    METHOD
    Image.Image.match

    SYNTAX
    object match(int|float scale,  object needle)
    object match(int|float scale,  object needle,  object haystack_cert,  object needle_cert)
    object match(int|float scale,  object needle,  object haystack_avoid,  int foo)
    object match(int|float scale,  object needle,  object haystack_cert,  object needle_cert,  object haystack_avoid,  int foo)

    DESCRIPTION
    This method creates an image that describes the match in every pixel in the image and the needle-Image.

    new pixel value =
    sum( my_abs(needle_pixel-haystack_pixel))
    

    The new image only have the red rgb-part set.

    ARGUMENTS
    argument(s)description
    int|float scale
    Every pixel is divided with this value. Note that a proper value here depends on the size of the neadle.
    object needle
    The image to use for the matching.
    object haystack_cert
    This image should be the same size as the image itselves. A non-white-part of the haystack_cert-image modifies the output by lowering it.
    object needle_cert
    The same, but for the needle-image.
    int foo
    object haystack_avoid
    This image should be the same size as the image itselves. If foo is less than the red value in haystack_avoid the corresponding matching-calculating is not calculated. The avoided parts are drawn in the color 0,100,0.

    RETURNS
    the new image object

    NOTE
    experimental status; may not be exact the same output in later versions

    SEE ALSO
    phasev and phaseh

    METHOD
    Image.Image.mirrorx

    SYNTAX
    object mirrorx()

    DESCRIPTION
    mirrors an image:
    original ->mirrorx();

    RETURNS
    the new image object

    METHOD
    Image.Image.mirrory

    SYNTAX
    object mirrory()

    DESCRIPTION
    mirrors an image:
    original ->mirrory();

    METHOD
    Image.Image.modify_by_intensity

    SYNTAX
    object modify_by_intensity(int r, int g, int b, int|array(int) v1, ..., int|array(int) vn)

    DESCRIPTION
    Recolor an image from intensity values.

    For each color an intensity is calculated, from r, g and b factors (see grey), this gives a value between 0 and max.

    The color is then calculated from the values given, v1 representing the intensity value of 0, vn representing max, and colors between representing intensity values between, linear.

    original ->grey()->modify_by_intensity(1,0,0, 0,({255,0,0}),({0,255,0}));

    ARGUMENTS
    argument(s)description
    int r
    int g
    int b
    red, green, blue intensity factors
    int|array(int) v1
    int|array(int) vn
    destination color

    RETURNS
    the new image object

    SEE ALSO
    grey, `* and color

    METHOD
    Image.Image.noise

    SYNTAX
    void noise(array(float|int|array(int)) colorrange)
    void noise(array(float|int|array(int)) colorrange, float scale, float xdiff, float ydiff, float cscale)

    DESCRIPTION
    Gives a new image with the old image's size, filled width a 'noise' pattern.

    The random seed may be different with each instance of pike.

    Example: ->noise( ({0,({255,0,0}), 0.3,({0,255,0}), 0.6,({0,0,255}), 0.8,({255,255,0})}), 0.2,0.0,0.0,1.0 );

    ARGUMENTS
    argument(s)description
    array(float|int|array(int)) colorrange
    colorrange table
    float scale
    default value is 0.1
    float xdiff
    float ydiff
    default value is 0,0
    float cscale
    default value is 1

    SEE ALSO
    turbulence

    METHOD
    Image.Image.orient,
    Image.Image.orient4

    SYNTAX
    object orient(void|array(object))
    array(object) orient4()

    DESCRIPTION
    Draws images describing the orientation of the current image.

    orient gives an HSV image (run a hsv_to_rgb pass on it to get a viewable image). corresponding to the angle of the orientation:

          |      /    -    \
    hue=  0     64   128  192  (=red in an hsv image)
    purple cyan green red
    
    Red, green and blue channels are added and not compared separately.

    If you first use orient4 you can give its output as input to this function.

    The orient4 function gives back 4 image objects, corresponding to the amount of different directions, see above.

    RETURNS
    an image or an array of the four new image objects

    NOTE
    experimental status; may not be exact the same output in later versions

    METHOD
    Image.Image.outline,
    Image.Image.outline_mask

    SYNTAX
    object outline()
    object outline(int olr, int olg, int olb)
    object outline(int olr, int olg, int olb, int bkgr, int bkgg, int bkgb)
    object outline(array(array(int)) mask)
    object outline(array(array(int)) mask, int olr, int olg, int olb)
    object outline(array(array(int)) mask, int olr, int olg, int olb, int bkgr, int bkgg, int bkgb)
    object outline_mask()
    object outline_mask(int bkgr, int bkgg, int bkgb)
    object outline_mask(array(array(int)) mask)
    object outline_mask(array(array(int)) mask, int bkgr, int bkgg, int bkgb)

    DESCRIPTION
    Makes an outline of this image, ie paints with the given color around the non-background pixels.

    Default is to paint above, below, to the left and the right of these pixels.

    You can also run your own outline mask.

    The outline_mask function gives the calculated outline as an alpha channel image of white and black instead.

    original masked
    through
    threshold
    ...and
    outlined
    with red

    ARGUMENTS
    argument(s)description
    array(array(int)) mask
    mask matrix. Default is ({({0,1,0}),({1,1,1}),({0,1,0})}).
    int olr
    int olg
    int olb
    outline color. Default is current.
    int bkgr
    int bkgg
    int bkgb
    background color (what color to outline to); default is color of pixel 0,0.
    int|float div
    division factor, default is 1.0.

    RETURNS
    the new image object

    NOTE
    no antialias!

    METHOD
    Image.Image.paste

    SYNTAX
    object paste(object image)
    object paste(object image, int x, int y)

    DESCRIPTION
    Pastes a given image over the current image.

    ARGUMENTS
    argument(s)description
    object image
    image to paste (may be empty, needs to be an image object)
    int x
    int y
    where to paste the image; default is 0,0

    RETURNS
    the object called

    SEE ALSO
    paste_mask, paste_alpha and paste_alpha_color

    METHOD
    Image.Image.paste_alpha

    SYNTAX
    object paste_alpha(object image, int alpha)
    object paste_alpha(object image, int alpha, int x, int y)

    DESCRIPTION
    Pastes a given image over the current image, with the specified alpha channel value.

    An alpha channel value of 0 leaves nothing of the original image in the paste area, 255 is meaningless and makes the given image invisible.

    ARGUMENTS
    argument(s)description
    object image
    image to paste
    int alpha
    alpha channel value
    int x
    int y
    where to paste the image; default is 0,0

    RETURNS
    the object called

    SEE ALSO
    paste_mask, paste and paste_alpha_color

    METHOD
    Image.Image.paste_alpha_color

    SYNTAX
    object paste_alpha_color(object mask)
    object paste_alpha_color(object mask, int x, int y)
    object paste_alpha_color(object mask, int r, int g, int b)
    object paste_alpha_color(object mask, int r, int g, int b, int x, int y)
    object paste_alpha_color(object mask, Color color)
    object paste_alpha_color(object mask, Color color, int x, int y)

    DESCRIPTION
    Pastes a given color over the current image, using the given mask as opaque channel.

    A pixel value of 255 makes the result become the color given, 0 doesn't change anything.

    The masks red, green and blue values are used separately. If no color are given, the current is used.

    ARGUMENTS
    argument(s)description
    object mask
    mask image
    int r
    int g
    int b
    what color to paint with; default is current
    int x
    int y
    where to paste the image; default is 0,0

    RETURNS
    the object called

    SEE ALSO
    paste_mask, paste_alpha and paste_alpha_color

    METHOD
    Image.Image.paste_mask

    SYNTAX
    object paste_mask(object image, object mask)
    object paste_mask(object image, object mask, int x, int y)

    DESCRIPTION
    Pastes a given image over the current image, using the given mask as opaque channel.

    A pixel value of 255 makes the result become a pixel from the given image, 0 doesn't change anything.

    The masks red, green and blue values are used separately.

    ARGUMENTS
    argument(s)description
    object image
    image to paste
    object mask
    mask image
    int x
    int y
    where to paste the image; default is 0,0

    RETURNS
    the object called

    SEE ALSO
    paste, paste_alpha and paste_alpha_color

    METHOD
    Image.Image.phasev,
    Image.Image.phaseh,
    Image.Image.phasehv,
    Image.Image.phasevh

    SYNTAX
    object phaseh()
    object phasev()
    object phasevh()
    object phasehv()

    DESCRIPTION
    Draws images describing the phase of the current image. phaseh gives the horizontal phase and phasev the vertical phase.

    phaseh gives an image where

    max  falling   min  rising
    value=  0     64      128   192
    

    0 is set if there is no way to determine if it is rising or falling. This is done for the every red, green and blue part of the image.

    Phase images can be used to create ugly effects or to find meta-information in the orginal image.

    original phaseh() phasev() phasevh() phasehv()

    RETURNS
    the new image object

    NOTE
    experimental status; may not be exact the same output in later versions

    BUGS
    0 should not be set as explained above.

    METHOD
    Image.Image.polyfill

    SYNTAX
    object polyfill(array(int|float) ... curve)

    DESCRIPTION
    fills an area with the current color

    ARGUMENTS
    argument(s)description
    array(int|float) curve
    curve(s), ({x1,y1,x2,y2,...,xn,yn}), automatically closed.

    If any given curve is inside another, it will make a hole.

    RETURNS
    the current object

    NOTE
    Lines in the polygon may not be crossed without the crossing coordinate specified in both lines.

    BUGS
    Inverted lines reported on Intel and Alpha processors.

    SEE ALSO
    setcolor

    METHOD
    Image.Image.random,
    Image.Image.randomgrey

    SYNTAX
    object random()
    object random(int seed)
    object randomgrey()
    object random(greyint seed)

    DESCRIPTION
    Gives a randomized image;
    original ->random() ->random(17) greyed
    (same again)
    color(red)
    (same again)
    ...red channel

    Use with ->grey() or ->color() for one-color-results.

    RETURNS
    a new image

    SEE ALSO
    test and noise

    METHOD
    Image.Image.write_lsb_rgb,
    Image.Image.read_lsb_grey,
    Image.Image.write_lsb_grey,
    Image.Image.read_lsb_rgb

    SYNTAX
    object write_lsb_rgb(string what)
    object write_lsb_grey(string what)
    string read_lsb_rgb()
    string read_lsb_grey()

    DESCRIPTION
    These functions read/write in the least significant bit of the image pixel values. The _rgb() functions read/write on each of the red, green and blue values, and the grey keeps the same lsb on all three.

    The string is nullpadded or cut to fit.

    ARGUMENTS
    argument(s)description
    string what
    the hidden message

    RETURNS
    the current object or the read string

    METHOD
    Image.Image.rotate,
    Image.Image.rotate_expand

    SYNTAX
    object rotate(int|float angle)
    object rotate(int|float angle, int r, int g, int b)
    object rotate_expand(int|float angle)
    object rotate_expand(int|float angle, int r, int g, int b)

    DESCRIPTION
    Rotates an image a certain amount of degrees (360° is a complete rotation) counter-clockwise:

    original ->rotate(15,255,0,0); ->rotate_expand(15);

    The "expand" variant of functions stretches the image border pixels rather then filling with the given or current color.

    This rotate uses the skewx() and skewy() functions.

    ARGUMENTS
    argument(s)description
    int|float angle
    the number of degrees to rotate
    int r
    int g
    int b
    color to fill with; default is current

    RETURNS
    the new image object

    METHOD
    Image.Image.rotate_ccw

    SYNTAX
    object rotate_ccw()

    DESCRIPTION
    rotates an image counter-clockwise, 90 degrees.

    original ->rotate_ccw();

    RETURNS
    the new image object

    METHOD
    Image.Image.rotate_cw

    SYNTAX
    object rotate_cw()

    DESCRIPTION
    rotates an image clockwise, 90 degrees.

    original ->rotate_cw();

    RETURNS
    the new image object

    METHOD
    Image.Image.scale

    SYNTAX
    object scale(float factor)
    object scale(0.5)
    object scale(float xfactor, float yfactor)

    DESCRIPTION
    scales the image with a factor, 0.5 is an optimized case.

    ARGUMENTS
    argument(s)description
    float factor
    factor to use for both x and y
    float xfactor
    float yfactor
    separate factors for x and y

    RETURNS
    the new image object

    METHOD
    Image.Image.scale

    SYNTAX
    object scale(int newxsize, int newysize)
    object scale(0, int newysize)
    object scale(int newxsize, 0)

    DESCRIPTION
    scales the image to a specified new size, if one of newxsize or newysize is 0, the image aspect ratio is preserved.

    ARGUMENTS
    argument(s)description
    int newxsize
    int newysize
    new image size in pixels

    RETURNS
    the new image object

    NOTE
    resulting image will be 1x1 pixels, at least

    METHOD
    Image.Image.select_from

    SYNTAX
    object select_from(int x, int y)
    object select_from(int x, int y, int edge_value)

    DESCRIPTION
    Makes an grey-scale image, for alpha-channel use.

    This is very close to a floodfill.

    The image is scanned from the given pixel, filled with 255 if the color is the same, or 255 minus distance in the colorcube, squared, rightshifted 8 steps (see distancesq).

    When the edge distance is reached, the scan is stopped. Default edge value is 30. This value is squared and compared with the square of the distance above.

    ARGUMENTS
    argument(s)description
    int x
    int y
    originating pixel in the image

    RETURNS
    the new image object

    SEE ALSO
    distancesq

    METHOD
    Image.Image.setcolor

    SYNTAX
    object setcolor(int r, int g, int b)
    object setcolor(int r, int g, int b, int alpha)

    DESCRIPTION
    set the current color

    ARGUMENTS
    argument(s)description
    int r
    int g
    int b
    new color
    int alpha
    new alpha value

    RETURNS
    the object called

    METHOD
    Image.Image.setpixel

    SYNTAX
    object setpixel(int x, int y)
    object setpixel(int x, int y, int r, int g, int b)
    object setpixel(int x, int y, int r, int g, int b, int alpha)

    DESCRIPTION
    original ->setpixel(10,10,255,0,0)

    ARGUMENTS
    argument(s)description
    int x
    int y
    position of the pixel
    int r
    int g
    int b
    color
    int alpha
    alpha value

    RETURNS
    the object called

    METHOD
    Image.Image.skewx,
    Image.Image.skewx_expand

    SYNTAX
    object skewx(int x)
    object skewx(int yfactor)
    object skewx(int x, int r, int g, int b)
    object skewx(int yfactor, int r, int g, int b)
    object skewx_expand(int x)
    object skewx_expand(int yfactor)
    object skewx_expand(int x, int r, int g, int b)
    object skewx_expand(int yfactor, int r, int g, int b)

    DESCRIPTION
    Skews an image an amount of pixels or a factor; a skew-x is a transformation:

    original ->skewx(15,255,0,0); ->skewx_expand(15);

    ARGUMENTS
    argument(s)description
    int x
    the number of pixels The "expand" variant of functions stretches the image border pixels rather then filling with the given or current color.
    float yfactor
    best described as: x=yfactor*this->ysize()
    int r
    int g
    int b
    color to fill with; default is current

    RETURNS
    the new image object

    METHOD
    Image.Image.skewy,
    Image.Image.skewy_expand

    SYNTAX
    object skewy(int y)
    object skewy(int xfactor)
    object skewy(int y, int r, int g, int b)
    object skewy(int xfactor, int r, int g, int b)
    object skewy_expand(int y)
    object skewy_expand(int xfactor)
    object skewy_expand(int y, int r, int g, int b)
    object skewy_expand(int xfactor, int r, int g, int b)

    DESCRIPTION
    Skews an image an amount of pixels or a factor; a skew-y is a transformation:

    original ->skewy(15,255,0,0); ->skewy_expand(15);

    The "expand" variant of functions stretches the image border pixels rather then filling with the given or current color.

    ARGUMENTS
    argument(s)description
    int y
    the number of pixels
    float xfactor
    best described as: t=xfactor*this->xsize()
    int r
    int g
    int b
    color to fill with; default is current

    RETURNS
    the new image object

    METHOD
    Image.Image.test

    SYNTAX
    object test()
    object test(int seed)

    DESCRIPTION
    Generates a test image, currently random gradients.

    original ->test() ...and again

    RETURNS
    the new image

    NOTE
    May be subject to change or cease without prior warning.

    SEE ALSO
    gradients and tuned_box

    METHOD
    Image.Image.threshold

    SYNTAX
    object threshold()
    object threshold(int level)
    object threshold(int r, int g, int b)
    object threshold(Color color)

    DESCRIPTION
    Makes a black-white image.

    If any of red, green, blue parts of a pixel is larger then the given value, the pixel will become white, else black.

    This method works fine with the grey method.

    If no arguments are given, it will paint all non-black pixels white. (Ie, default is 0,0,0.)

    original ->threshold(100); ->threshold(0,100,0);

    RETURNS
    the new image object

    NOTE
    The above statement "any ..." was changed from "all ..." in Pike 0.7 (9906). It also uses 0,0,0 as default input, instead of current color. This is more useful.

    SEE ALSO
    grey

    METHOD
    Image.Image.tuned_box

    SYNTAX
    object tuned_box(int x1, int y1, int x2, int y2, array(array(int)) corner_color)

    DESCRIPTION
    Draws a filled rectangle with colors (and alpha values) tuned between the corners.

    Tuning function is (1.0-x/xw)*(1.0-y/yw) where x and y is the distance to the corner and xw and yw are the sides of the rectangle.

    original tuned box solid tuning
    (blue,red,green,yellow)
    tuning transparency
    (as left + 255,128,128,0)

    ARGUMENTS
    argument(s)description
    int x1
    int y1
    int x2
    int y2
    rectangle corners
    array(array(int)) corner_color
    colors of the corners:
    ({x1y1,x2y1,x1y2,x2y2})
    
    each of these is an array of integeres:
    ({r,g,b}) or ({r,g,b,alpha})
    
    Default alpha channel value is 0 (opaque).

    RETURNS
    the object called

    METHOD
    Image.Image.turbulence

    SYNTAX
    void turbulence(array(float|int|array(int)) colorrange)
    void turbulence(array(float|int|array(int)) colorrange, int octaves, float scale, float xdiff, float ydiff, float cscale)

    DESCRIPTION
    gives a new image with the old image's size, filled width a 'turbulence' pattern

    The random seed may be different with each instance of pike.

    Example:
    ->turbulence( ({0,({229,204,204}), 0.9,({229,20,20}), 0.9,Color.black}) );

    ARGUMENTS
    argument(s)description
    array(float|int|array(int)) colorrange
    colorrange table
    int octaves
    default value is 3
    float scale
    default value is 0.1
    float xdiff
    float ydiff
    default value is 0,0
    float cscale
    default value is 1

    SEE ALSO
    noise and Image.Color

    METHOD
    Image.Image.xsize

    SYNTAX
    int xsize()

    RETURNS
    the width of the image

    METHOD
    Image.Image.ysize

    SYNTAX
    int ysize()

    RETURNS
    the height of the image

    METHOD
    Image.Image.`/,
    Image.Image.`%

    SYNTAX
    object `/(object operand)
    object `/(Color color)
    object `/(int value)
    object `%(object operand)
    object `%(Color color)
    object `%(int value)

    DESCRIPTION
    Divides pixel values and creates a new image from the result or the rest.

    ARGUMENTS
    argument(s)description
    object operand
    the other image to divide with; the images must have the same size.
    Color color
    int value
    if specified as color or value, it will act as a whole image of that color (or value).

    RETURNS
    the new image object

    NOTE
    It isn't possible to do a modulo 256 either. (why?)

    SEE ALSO
    `-, `+, `|, `&, `* and Image.Layer

    METHOD
    Image.Image.`&

    SYNTAX
    object `&(object operand)
    object `&(array(int) color)
    object `&(int value)

    DESCRIPTION
    makes a new image out of the minimum pixels values

    ARGUMENTS
    argument(s)description
    object operand
    the other image to compare with; the images must have the same size.
    array(int) color
    an array in format ({r,g,b}), this is equal to using an uniform-colored image.
    int value
    equal to ({value,value,value}).

    RETURNS
    the new image object

    SEE ALSO
    `-, `+, `|, `* and Image.Layer

    METHOD
    Image.Image.`==,
    Image.Image.`<,
    Image.Image.`<

    SYNTAX
    int `==(object operand)
    int `==(array(int) color)
    int `==(int value)
    int `<(object operand)
    int `<(array(int) color)
    int `<(int value)
    int `>(object operand)
    int `>(array(int) color)
    int `>(int value)

    DESCRIPTION
    Compares an image with another image or a color.

    Comparision is strict and on pixel-by-pixel basis. (Means if not all pixel r,g,b values are correct compared with the corresponding pixel values, 0 is returned.)

    ARGUMENTS
    argument(s)description
    object operand
    the other image to compare with; the images must have the same size.
    array(int) color
    an array in format ({r,g,b}), this is equal to using an uniform-colored image.
    int value
    equal to ({value,value,value}).

    RETURNS
    true (1) or false (0).

    NOTE
    `< or `> on empty ("no image") image objects or images with different size will result in an error. `== is always true on two empty image objects and always false if one and only one of the image objects is empty or the images differs in size.

    a>=b and a<=b between objects is equal to !(a<b) and !(a>b), which may not be what you want (since both < and > can return false, comparing the same images).

    SEE ALSO
    `-, `+, `|, `* and `&

    METHOD
    Image.Image.`*

    SYNTAX
    object `*(object operand)
    object `*(array(int) color)
    object `*(int value)

    DESCRIPTION
    Multiplies pixel values and creates a new image.

    This can be useful to lower the values of an image, making it greyer, for instance:

    image=image*128+64;

    ARGUMENTS
    argument(s)description
    object operand
    the other image to multiply with; the images must have the same size.
    array(int) color
    an array in format ({r,g,b}), this is equal to using an uniform-colored image.
    int value
    equal to ({value,value,value}).

    RETURNS
    the new image object

    SEE ALSO
    `-, `+, `|, `& and Image.Layer

    METHOD
    Image.Image.`+

    SYNTAX
    object `+(object operand)
    object `+(array(int) color)
    object `+(int value)

    DESCRIPTION
    adds two images; values are truncated at 255.

    ARGUMENTS
    argument(s)description
    object operand
    the image which to add.
    array(int) color
    an array in format ({r,g,b}), this is equal to using an uniform-colored image.
    int value
    equal to ({value,value,value}).

    RETURNS
    the new image object

    SEE ALSO
    `-, `|, `&, `* and Image.Layer

    METHOD
    Image.Image.`-

    SYNTAX
    object `-(object operand)
    object `-(array(int) color)
    object `-(int value)

    DESCRIPTION
    makes a new image out of the difference

    ARGUMENTS
    argument(s)description
    object operand
    the other image to compare with; the images must have the same size.
    array(int) color
    an array in format ({r,g,b}), this is equal to using an uniform-colored image.
    int value
    equal to ({value,value,value}).

    RETURNS
    the new image object

    SEE ALSO
    `+, `|, `&, `*, Image.Layer, min, max and `==

    METHOD
    Image.Image.`|

    SYNTAX
    object `|(object operand)
    object `|(array(int) color)
    object `|(int value)

    DESCRIPTION
    makes a new image out of the maximum pixels values

    ARGUMENTS
    argument(s)description
    object operand
    the other image to compare with; the images must have the same size.
    array(int) color
    an array in format ({r,g,b}), this is equal to using an uniform-colored image.
    int value
    equal to ({value,value,value}).

    RETURNS
    the new image object

    SEE ALSO
    `-, `+, `&, `* and Image.Layer

    12.2 Image.Colortable

    CLASS
    Image.Colortable

    DESCRIPTION
    This object keeps colortable information, mostly for image re-coloring (quantization).

    The object has color reduction, quantisation, mapping and dithering capabilities.

    SEE ALSO
    Image, Image.Image, Image.Font and Image.GIF

    METHOD
    Image.Colortable.create,
    Image.Colortable.add

    SYNTAX
    void create()
    void create(array(array(int)) colors)
    void create(object(Image.Image) image, int number)
    void create(object(Image.Image) image, int number, array(array(int)) needed)
    void create(int r, int g, int b)
    void create(int r, int g, int b,  array(int) from1, array(int) to1, int steps1,  ...,  array(int) fromn, array(int) ton, int stepsn)
    object add(array(array(int)) colors)
    object add(object(Image.Image) image, int number)
    object add(object(Image.Image) image, int number, array(array(int)) needed)
    object add(int r, int g, int b)
    object add(int r, int g, int b,  array(int) from1, array(int) to1, int steps1,  ...,  array(int) fromn, array(int) ton, int stepsn)

    DESCRIPTION
    create initiates a colortable object. Default is that no colors are in the colortable.

    add takes the same argument(s) as create, thus adding colors to the colortable.

    The colortable is mostly a list of colors, or more advanced, colors and weight.

    The colortable could also be a colorcube, with or without additional scales. A colorcube is the by-far fastest way to find colors.

    Example:

    ct=colortable(my_image,256); // the best 256 colors
    ct=colortable(my_image,256,({0,0,0})); // black and the best other 255
    
    

    ct=colortable(({({0,0,0}),({255,255,255})})); // black and white

    ct=colortable(6,7,6); // a colortable of 252 colors ct=colortable(7,7,5, ({0,0,0}),({255,255,255}),11); // a colorcube of 245 colors, and a greyscale of the rest -> 256

    ARGUMENTS
    argument(s)description
    array(array(int)) colors
    list of colors
    object(Image.Image) image
    source image
    int number
    number of colors to get from the image

    0 (zero) gives all colors in the image.

    Default value is 256.

    array(array(int)) needed
    needed colors (to optimize selection of others to these given)

    this will add to the total number of colors (see argument 'number')

    int r
    int g
    int b
    size of sides in the colorcube, must (of course) be equal or larger than 2 - if smaller, the cube is ignored (no colors). This could be used to have only scales (like a greyscale) in the output.
    array(int) fromi
    array(int) toi
    int stepi
    This is to add the possibility of adding a scale of colors to the colorcube; for instance a grayscale using the arguments ({0,0,0}),({255,255,255}),17, adding a scale from black to white in 17 or more steps.

    Colors already in the cube is used again to add the number of steps, if possible.

    The total number of colors in the table is therefore r*b*g+step1+...+stepn.

    NOTE
    max hash size is (probably, set by a #define) 32768 entries, giving maybe half that number of colors as maximum.

    METHOD
    Image.Colortable.cast

    SYNTAX
    object cast(string to)

    DESCRIPTION
    cast the colortable to an array or mapping, the array consists of Image.Color objects and are not in index order. The mapping consists of index:Image.Color pairs, where index is the index (int) of that color.

    example: (mapping)Image.Colortable(img)

    ARGUMENTS
    argument(s)description
    string to
    must be "string", "array" or "mapping".

    METHOD
    Image.Colortable.corners

    SYNTAX
    array(object) corners()

    DESCRIPTION
    Gives the eight corners in rgb colorspace as an array. The "black" and "white" corners are the first two.

    METHOD
    Image.Colortable.cubicles

    SYNTAX
    object cubicles()
    object cubicles(int r, int g, int b)
    object cubicles(int r, int g, int b, int accuracy)

    DESCRIPTION
    Set the colortable to use the cubicles algorithm to lookup the closest color. This is a mostly very fast and very accurate way to find the correct color, and the default algorithm.

    The colorspace is divided in small cubes, each cube containing the colors in that cube. Each cube then gets a list of the colors in the cube, and the closest from the corners and midpoints between corners.

    When a color is needed, the algorithm first finds the correct cube and then compares with all the colors in the list for that cube.

    example: colors=Image.Colortable(img)->cubicles();

    algorithm time: between O[m] and O[m * n], where n is numbers of colors and m is number of pixels

    The arguments can be heavy trimmed for the usage of your colortable; a large number (10×10×10 or bigger) of cubicles is recommended when you use the colortable repeatedly, since the calculation takes much more time than usage.

    recommended values:

    image size  setup
    100×100     cubicles(4,5,4) (default)
    1000×1000   cubicles(12,12,12) (factor 2 faster than default)
    

    In some cases, the full method is faster.

    original default cubicles,
    16 colors
    accuracy=200

    ARGUMENTS
    argument(s)description
    int r
    int g
    int b
    Size, ie how much the colorspace is divided. Note that the size of each cubicle is at least about 8b, and that it takes time to calculate them. The number of cubicles are r*g*b, and default is 4,5,4, ie 80 cubicles. This works good for 200±100 colors.
    int accuracy
    Accuracy when checking sides of cubicles. Default is 16. A value of 1 gives complete accuracy, ie cubicle() method gives exactly the same result as full(), but takes (in worst case) 16× the time to calculate.

    RETURNS
    the called object

    NOTE
    this method doesn't figure out the cubicles, this is done on the first use of the colortable.

    Not applicable to colorcube types of colortable.

    METHOD
    Image.Colortable.floyd_steinberg

    SYNTAX
    object floyd_steinberg()
    object floyd_steinberg(int bidir, int|float forward, int|float downforward, int|float down, int|float downback, int|float factor)

    DESCRIPTION
    Set dithering method to floyd_steinberg.

    The arguments to this method is for fine-tuning of the algorithm (for computer graphics wizards).

    original floyd_steinberg to a 4×4×4 colorcube floyd_steinberg to 16 chosen colors

    ARGUMENTS
    argument(s)description
    int bidir
    Set algorithm direction of forward. -1 is backward, 1 is forward, 0 for toggle of direction each line (default).
    int|float forward
    int|float downforward
    int|float down
    int|float downback
    Set error correction directions. Default is forward=7, downforward=1, down=5, downback=3.
    int|float factor
    Error keeping factor. Error will increase if more than 1.0 and decrease if less than 1.0. A value of 0.0 will cancel any dither effects. Default is 0.95.

    RETURNS
    the called object

    METHOD
    Image.Colortable.full

    SYNTAX
    object full()

    DESCRIPTION
    Set the colortable to use full scan to lookup the closest color.

    example: colors=Image.Colortable(img)->full();

    algorithm time: O[n*m], where n is numbers of colors and m is number of pixels

    RETURNS
    the called object

    NOTE
    Not applicable to colorcube types of colortable.

    SEE ALSO
    cubicles and map

    METHOD
    Image.Colortable.image

    SYNTAX
    object image()

    DESCRIPTION
    cast the colortable to an image object

    each pixel in the image object is an entry in the colortable

    RETURNS
    the resulting image object

    METHOD
    Image.Colortable.`*,
    Image.Colortable.``*,
    Image.Colortable.map

    SYNTAX
    object map(object image)
    object `*(object image)
    object ``*(object image)
    object map(string data, int xsize, int ysize)
    object `*(string data, int xsize, int ysize)
    object ``*(string data, int xsize, int ysize)

    DESCRIPTION
    Map colors in an image object to the colors in the colortable, and creates a new image with the closest colors.

    no dither
    floyd_steinberg dither
    ordered dither
    randomcube dither
    original 2 4 8 16 32 colors

    RETURNS
    a new image object

    NOTE
    Flat (not cube) colortable and not 'full' method: this method does figure out the data needed for the lookup method, which may take time the first use of the colortable - the second use is quicker.

    SEE ALSO
    cubicles and full

    METHOD
    Image.Colortable.nodither

    SYNTAX
    object nodither()

    DESCRIPTION
    Set no dithering (default).

    RETURNS
    the called object

    METHOD
    Image.Colortable.ordered

    SYNTAX
    object ordered()
    object ordered(int r, int g, int b)
    object ordered(int r, int g, int b, int xsize, int ysize)
    object ordered(int r, int g, int b, int xsize, int ysize, int x, int y)
    object ordered(int r, int g, int b, int xsize, int ysize, int rx, int ry, int gx, int gy, int bx, int by)

    DESCRIPTION
    Set ordered dithering, which gives a position-dependent error added to the pixel values.

    original mapped to
    Image.Colortable(6,6,6)->
    ordered
    (42,42,42,2,2)
    ordered() ordered
    (42,42,42, 8,8,
    0,0, 0,1, 1,0)

    ARGUMENTS
    argument(s)description
    int r
    int g
    int b
    The maximum error. Default is 32, or colorcube steps (256/size).
    int xsize
    int ysize
    Size of error matrix. Default is 8×8. Only values which factors to multiples of 2 and 3 are possible to choose (2,3,4,6,8,12,...).
    int x
    int y
    int rx
    int ry
    int gx
    int gy
    int bx
    int by
    Offset for the error matrix. x and y is for both red, green and blue values, the other is individual.

    RETURNS
    the called object

    SEE ALSO
    randomcube, nodither, floyd_steinberg and create

    METHOD
    Image.Colortable.randomcube,
    Image.Colortable.randomgrey

    SYNTAX
    object randomcube()
    object randomcube(int r, int g, int b)
    object randomgrey()
    object randomgrey(int err)

    DESCRIPTION
    Set random cube dithering. Color choosen is the closest one to color in picture plus (flat) random error; color±random(error).

    The randomgrey method uses the same random error on red, green and blue and the randomcube method has three random errors.

    original mapped to
    Image.Colortable(4,4,4)->
    randomcube() randomgrey()

    ARGUMENTS
    argument(s)description
    int r
    int g
    int b
    int err
    The maximum error. Default is 32, or colorcube step.

    RETURNS
    the called object

    NOTE
    randomgrey method needs colorcube size to be the same on red, green and blue sides to work properly. It uses the red colorcube value as default.

    SEE ALSO
    ordered, nodither, floyd_steinberg and create

    METHOD
    Image.Colortable.reduce,
    Image.Colortable.reduce_fs

    SYNTAX
    object reduce(int colors)
    object reduce_fs(int colors)

    DESCRIPTION
    reduces the number of colors

    All needed (see create) colors are kept.

    reduce_fs creates and keeps the outmost corners of the color space, to improve floyd-steinberg dithering result. (It doesn't work very well, though.)

    ARGUMENTS
    argument(s)description
    int colors
    target number of colors

    RETURNS
    the new Colortable object

    NOTE
    this algorithm assumes all colors are different to begin with (!)

    reduce_fs keeps the "corners" as "needed colors".

    SEE ALSO
    corners

    METHOD
    Image.Colortable.rigid

    SYNTAX
    object rigid()
    object rigid(int r, int g, int b)

    DESCRIPTION
    Set the colortable to use the "rigid" method of looking up colors.

    This is a very simple way of finding the correct color. The algorithm initializes a cube with r x g x b colors, where every color is chosen by closest match to the corresponding coordinate.

    This format is not recommended for exact match, but may be usable when it comes to quickly view pictures on-screen.

    It has a high init-cost and low use-cost. The structure is initiated at first usage.

    RETURNS
    the called object

    NOTE
    Not applicable to colorcube types of colortable.

    SEE ALSO
    cubicles, map and full

    METHOD
    Image.Colortable.spacefactors

    SYNTAX
    object spacefactors(int r, int g, int b)

    DESCRIPTION
    Colortable tuning option, this sets the color space distance factors. This is used when comparing distances in the colorspace and comparing grey levels.

    Default factors are 3, 4 and 1; blue is much darker than green. Compare with Image.Image->grey().

    RETURNS
    the called object

    NOTE
    This has no sanity check. Some functions may bug if the factors are to high - color reduction functions sums grey levels in the image, this could exceed maxint in the case of high factors. Negative values may also cause strange effects. *grin*

    METHOD
    Image.Colortable.`+

    SYNTAX
    object `+(object with, ...)

    DESCRIPTION
    sums colortables

    ARGUMENTS
    argument(s)description
    object(Colortable) with
    Colortable object with colors to add

    RETURNS
    the resulting new Colortable object

    METHOD
    Image.Colortable.`-

    SYNTAX
    object `-(object with, ...)

    DESCRIPTION
    subtracts colortables

    ARGUMENTS
    argument(s)description
    object(Colortable) with
    Colortable object with colors to subtract

    RETURNS
    the resulting new Colortable object

    12.3 Image.Layer

    CLASS
    Image.Layer

    DESCRIPTION

    SEE ALSO
    layers

    METHOD
    Image.Layer.alpha,
    Image.Layer.image,
    Image.Layer.set_image

    SYNTAX
    object set_image(object(Image.Image) image)
    object set_image(object(Image.Image) image, object(Image.Image) alpha_channel)
    object|int(0) image()
    object|int(0) alpha()

    DESCRIPTION
    Set/change/get image and alpha channel for the layer. You could also cancel the channels giving 0 instead of an image object.

    NOTE
    image and alpha channel must be of the same size, or canceled.

    METHOD
    Image.Layer.alpha_value,
    Image.Layer.set_alpha_value

    SYNTAX
    object set_alpha_value(float value)
    double alpha_value()

    DESCRIPTION
    Set/get the general alpha value of this layer. This is a float value between 0 and 1, and is multiplied with the alpha channel.

    METHOD
    Image.Layer.autocrop,
    Image.Layer.find_autocrop

    SYNTAX
    object autocrop()
    object autocrop(int(0..1) left, int(0..1) right, int(0..1) top, int(0..1) bottom)
    array(int) find_autocrop()
    array(int) find_autocrop(int(0..1) left, int(0..1) right, int(0..1) top, int(0..1) bottom)

    DESCRIPTION
    This crops (of finds) a suitable crop, non-destructive crop. The layer alpha channel is checked, and edges that is transparent is removed.

    (What really happens is that the image and alpha channel is checked, and edges equal the fill setup is cropped away.)

    find_autocrop() returns an array of xoff,yoff,xsize,ysize, which can be fed to crop().

    NOTE
    A tiled image will not be cropped at all.

    left...bottom arguments can be used to tell what sides cropping are ok on.

    SEE ALSO
    crop and Image.Image->autocrop

    METHOD
    Image.Layer.mode,
    Image.Layer.set_mode,
    Image.Layer.available_modes

    SYNTAX
    object set_mode(string mode)
    string mode()
    array(string) available_modes()

    DESCRIPTION
    Set/get layer mode. Mode is one of these:

    "normal", "add", "subtract", "multiply", "divide", "modulo", "invsubtract", "invdivide", "invmodulo", "difference", "max", "min", "bitwise_and", "bitwise_or", "bitwise_xor",

    "replace", "red", "green", "blue",

    "replace_hsv", "hue", "saturation", "value", "color",

    "darken", "lighten",

    "dissolve", "behind", "erase",

    available_modes() simply gives an array containing the names of these modes.

    NOTE
    image and alpha channel must be of the same size, or canceled.

    METHOD
    Image.Layer.clone

    SYNTAX
    object clone()

    DESCRIPTION
    Creates a copy of the called object.

    RETURNS
    the copy

    METHOD
    Image.Layer.create

    SYNTAX
    void create(object image, object alpha, string mode)
    void create(mapping info)
    void create()
    void create(int xsize, int ysize, object color)
    void create(object color)

    DESCRIPTION
    The Layer construct either three arguments, the image object, alpha channel and mode, or a mapping with optional elements:
    "image":image,
    // default: black
    
    

    "alpha":alpha, // alpha channel object // default: full opaque

    "mode":string mode, // layer mode, see mode. // default: "normal"

    "alpha_value":float(0.0-1.0), // layer general alpha value // default is 1.0; this is multiplied // with the alpha channel.

    "xoffset":int, "yoffset":int, // offset of this layer

    "fill":Color, "fill_alpha":Color, // fill color, ie what color is used // "outside" the image. default: black // and black (full transparency).

    "tiled":int(0|1), // select tiling; if 1, the image // will be tiled. deafult: 0, off

    The layer can also be created "empty", either giving a size and color - this will give a filled opaque square, or a color, which will set the "fill" values and fill the whole layer with an opaque color.

    All values can be modified after object creation.

    NOTE
    image and alpha channel must be of the same size.

    METHOD
    Image.Layer.crop

    SYNTAX
    object crop(int xoff, int yoff, int xsize, int ysize)

    DESCRIPTION
    Crops this layer at this offset and size. Offset is not relative the layer offset, so this can be used to crop a number of layers simuntaneously.

    The fill values are used if the layer is enlarged.

    RETURNS
    a new layer object

    NOTE
    The new layer object may have the same image object, if there was no cropping to be done.

    METHOD
    Image.Layer.fill,
    Image.Layer.fill_alpha,
    Image.Layer.set_fill

    SYNTAX
    object set_fill(Color color)
    object set_fill(Color color, Color alpha)
    object fill()
    object fill_alpha()

    DESCRIPTION
    Set/query fill color and alpha, ie the color used "outside" the image. This is mostly useful if you want to "frame" a layer.

    METHOD
    Image.Layer.set_misc_value,
    Image.Layer.get_misc_value

    SYNTAX
    mixed set_misc_value( mixed what,  mixed to )
    mixed get_misc_value( mixed what )

    DESCRIPTION
    Set or query misc. attributes for the layer.

    As an example, the XCF and PSD image decoders set the 'name' attribute to the name the layer had in the source file.

    METHOD
    Image.Layer.xoffset,
    Image.Layer.yoffset,
    Image.Layer.set_offset

    SYNTAX
    object set_offset(int x, int y)
    int xoffset()
    int yoffset()

    DESCRIPTION
    Set/query layer offset.

    METHOD
    Image.Layer.tiled,
    Image.Layer.set_tiled

    SYNTAX
    object set_tiled(int yes)
    int tiled()

    DESCRIPTION
    Set/query tiled flag. If set, the image and alpha channel will be tiled rather then framed by the fill values.

    METHOD
    Image.Layer.ysize,
    Image.Layer.xsize

    SYNTAX
    int xsize()
    int ysize()

    DESCRIPTION
    Query layer offset. This is the same as layer image/alpha image size.

    12.4 Image.Font

    CLASS
    Image.Font

    DESCRIPTION

    NOTE
    Short technical documentation on a font file: This object adds the text-drawing and -creation capabilities of the Image module.

    For simple usage, see write and load.

    other methods: baseline, height, set_xspacing_scale, set_yspacing_scale, text_extents

    struct file_head
    {
    unsigned INT32 cookie;   - 0x464f4e54
    unsigned INT32 version;  - 1
    unsigned INT32 chars;    - number of chars
    unsigned INT32 height;   - height of font
    unsigned INT32 baseline; - font baseline
    unsigned INT32 o[1];     - position of char_head's
    } *fh;
    struct char_head
    {
    unsigned INT32 width;    - width of this character
    unsigned INT32 spacing;  - spacing to next character
    unsigned char data[1];   - pixmap data (1byte/pixel)
    } *ch;
    
    

    version 2:

    On-disk syntax (everything in N.B.O), int is 4 bytes, a byte is 8 bits:

    pos 0 int cookie = 'FONT'; or 0x464f4e54 4 int version = 2; 1 was the old version without the last four chars 8 int numchars; Always 256 in this version of the dump program 12 int height; in (whole) pixels 16 int baseline; in (whole) pixels 20 char direction; 1==right to left, 0 is left to right 21 char format; Font format 22 char colortablep; Colortable format 23 char kerningtablep; Kerning table format

    24 int offsets[numchars]; pointers into the data, realative to &cookie. [colortable] [kerningtable]

    At each offset:

    0 int width; in pixels 4 int spacing; in 1/1000:th of a pixels 8 char data[]; Enough data to plot width * font->height pixels Please note that if width is 0, there is no data.

    Font formats: id type 0 Raw 8bit data 1 RLE encoded data, char length, char data, 70% more compact than raw data 2 ZLib compressed data 60% more compact than RLE

    Colortable types: 0 No colortable (the data is an alpha channel) 1 24bit RGB with alpha (index->color, 256*4 bytes, rgba) 2 8bit Greyscale with alpha (index->color, 256*2 bytes)

    Kerningtable types: 0 No kerning table 1 numchars*numchars entries, each a signed char with the kerning value 2 numchar entries, each with a list of kerning pairs, like this: int len len * (short char, short value) **!

    SEE ALSO
    Image and Image.Image

    METHOD
    Image.Font.baseline

    SYNTAX
    int baseline()

    RETURNS
    font baseline (pixels from top)

    SEE A