#!/usr/bin/php
<?
/*
    Raydium - CQFD Corp.
    http://raydium.org/
    Released under both BSD license and Lesser GPL library license.
    See "license.txt" file.
*/

// This script generates a Wiki(ni) style documentation from comments of
// all header files of Raydium (raydium/header/*.h)

$page="http://wiki.raydium.org/wiki/RaydiumApiReference";

$intro="
======Raydium API Reference======

=====CQFD Corp.=====

This document is the most up-to-date version. **This is a work in progress**:
there's again some errors and wrong informations. Try, wait, or contribute ;)

\"\"<a href=$page#chapters>Index of chapters</a>\"\"
\"\"<a href=$page#index>Index of all Raydium functions</a>\"\"

----
This document is autogenerated, any change will be lost,
use RaydiumApiReferenceComments for any need.
{DATE}, for Raydium **{VERSION}**
----
";


function cleanName($str)
{
$res="";
$allowed="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";

$str=explode('(',$str);
$str=$str[0];

for($i=0;$i<strlen($str);$i++)
    if(strpos($allowed,$str[$i])!==false)
        $res.=$str[$i];

return $res;
}

function getTagLine($tag,$lines,$from=0)
{
for($i=$from;$i<count($lines);$i++)
    {
    $l=$lines[$i];
    if(substr(trim($l),0,strlen($tag))==$tag)
        return $i;
    }
return -1;
}

function getVersion()
{
$file="../common.h";
$f=file($file);
$maj=getTagLine("#define RAYDIUM_MAJOR",$f);
$min=getTagLine("#define RAYDIUM_MINOR",$f);

$maj=str_replace("\t"," ",trim($f[$maj]));
$min=str_replace("\t"," ",trim($f[$min]));

$maj=explode(" ",$maj);
$min=explode(" ",$min);
$maj=$maj[count($maj)-1];
$min=$min[count($min)-1];

return sprintf("%d.%03d",$maj,$min);
}

function getMain($filename,$offset)
{
$f=file($filename);
$l=getTagLine("/*=",$f);
if($l==-1)
    return -1;
$res=trim($f[$l+$offset]);
return $res;
}

function getPriority($filename)
{
$res=trim(getMain($filename,2));
if(is_numeric($res))
    return $res;
else
    return -1;
}

function getTitle($filename)
{
return trim(getMain($filename,1));
}



function getHeaders($directory)
{
$res=array();
if (is_dir($directory))
    {
        if ($dh = opendir($directory))
        {
            while (($file = readdir($dh)) !== false)
            {
                if(substr($file,-2)==".h")
                {
                    $res[]=$file;
                }
            }
        closedir($dh);
        }
    }
    else echo "'$directory' is not a directory";
return $res;
}


$chapters=array();
function h1($str,$addchap,$file="")
{
global $chapters;
if($addchap)
    {
    echo '""'."<a name=\"$file\"></a>".'""';
    $chapters[$file]=$str;
    $i++;
    }
echo "\n=====$str:=====\n";
}

function h2($str)
{
echo "====$str:====\n";
}

function body($str)
{
echo $str."\n\n";
}

function intro($str)
{
$str=str_replace("{DATE}","Generated: ".date("Y-m-d H:i:s"),$str);
$str=str_replace("{VERSION}",getVersion(),$str);
echo $str;
}

$index=array();
function addToIndex($f)
{
global $index;

$p=strpos($f,"raydium_");
if($p!==false)
    $f=substr($f,$p);
else
    $f="unsupported - $f";

$cf=cleanName($f);
$index[$cf]=$f."|$cf";
return $cf;
}

// Main

$id="";
$files=getHeaders(".");
//var_dump($files);
if($files==-1)
    die("No header found");

unset($sorted);
for($i=0;$i<count($files);$i++)
    {
    $file=$files[$i];
    $p=getPriority($file);
    if($p==-1)
        $p=999000+$i;

    while(isset($sorted[$p]))
        $p++;
    $sorted[$p]=$file;
    }
ksort($sorted);
$sorted=array_values($sorted);
//var_dump($sorted);

// Files are sorted, now

intro($intro);

for($i=0;$i<count($sorted);$i++)
    {
    $file=$sorted[$i];
    $title=getTitle($file);

    if($title==-1)
        {
        h1(($i+1)." no documentation for $file",true,$file);
        continue;
        }

    h1(($i+1)." $title",true,$file);

    $f=file($file);
    $last=0;
    $n=0;
    while(($l=getTagLine("/**",$f,$last))!=-1)
        {
        $title=trim($f[$l-1]);
        if($title=="")
            $title="// unknown item";

        // types:
        // 1 - Comment (//)
        // 2 - Macro (#)
        // 3 - Code (...)
        $type=3;
        if($title[0]=="/")
            {
            $type=1;
            $com_type=$title[1];
            $title=trim(substr($title,2));
            if($com_type=='*')
                {
                $title=str_replace( "*/", "", $title);
                $title=trim($title);
                }
            }

        if($title[0]=="#")
            {
            $type=2;
            $pos=strpos($title,")");
            if($pos)
                {
                $title=substr($title,0,$pos+1);
                }
            $title=trim(str_replace("#define ","",$title))." (macro)";

            }

        if($type==3)
            {
            if(substr($title,0,7)=="extern ")
                $title=trim(substr($title,7));
            if(substr($title,0,9)=="__rayapi ")
                $title=trim(substr($title,9));
            if($title[strlen($title)-1]==";")
                $title=substr($title,0,-1);
            $title=trim(str_replace("**","* *",$title));
            }

        if($type==2 || $type==3)
            $id=addToIndex($title);

        h2('""'."<a name=\"$id\"></a>".'""'.($i+1).".".($n+1)." $title");

        $last=$l+1;
        $end=getTagLine("**/",$f,$last);
        if($end==-1)
            die("expected '**/' (started line $l)");

        unset($body);
/*      for($j=$l+1;$j<$end;$j++)
            {
            $lj=trim($f[$j]);
            if($lj=="")
                $lj="\n\n";
            else
                $lj.=" ";
            $body[]=$lj;
            }
        $str=implode("",$body);*/
        for($j=$l+1;$j<$end;$j++)
            {
            $lj=trim($f[$j]);
            $body[]=$lj;
            }
        $str=@implode("\n",$body);
        body($str);

        $last=$end+1;
        $n++;
        }
    }


h1('""<a name=chapters></a>""Chapters',false);
foreach($chapters as $key => $val)
    {
    echo('====""'."<a href=\"$page#$key\">$val</a>".'""====')."\n";
    }

sort($index);
h1('""<a name=index></a>""Index',false);
for($i=0;$i<count($index);$i++)
    {
    $j=explode("|",$index[$i]);
    $k=$j[0];
    $l=$j[1];
    echo '""'."<a href=\"$page#$l\"><tt>$k</tt></a>".'""'."\n";
    }

\
ZSTR_AFFECT(c)\
RETURN_LONG(res);\
}

// void f(char *, ...) - (printf style)
#define PHP_v_svaria(fname)\
ZEND_FUNCTION(fname)\
{\
char *a;\
long s_len;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "s", &a, &s_len) == FAILURE)  return;\
fname("%s",a);\
}

// int f(char *, int, float, float, char *)
#define PHP_i_siffs(fname)\
ZEND_FUNCTION(fname)\
{\
long s_len1;\
long s_len2;\
char *a;\
long b;\
double c,d;\
char *e;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "sldds", &a, &s_len1, &b, &c, &d, &e, &s_len2) == FAILURE)  return;\
RETURN_LONG(fname(a,b,c,d,e));\
}

// int f(char *, int, float, float, char *, int)
#define PHP_i_siffsi(fname)\
ZEND_FUNCTION(fname)\
{\
long s_len1;\
long s_len2;\
char *a;\
long b;\
double c,d;\
char *e;\
long f;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "slddsl", &a, &s_len1, &b, &c, &d, &e, &s_len2, &f) == FAILURE)  return;\
RETURN_LONG(fname(a,b,c,d,e,f));\
}

// int f(char *, int, float, float, char *, float, float, float)
#define PHP_i_siffsfff(fname)\
ZEND_FUNCTION(fname)\
{\
long s_len1;\
long s_len2;\
char *a;\
long b;\
double c,d;\
char *e;\
double f,g,h;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "slddsddd", &a, &s_len1, &b, &c, &d, &e, &s_len2, &f, &g, &h) == FAILURE)  return;\
RETURN_LONG(fname(a,b,c,d,e,f,g,h));\
}


// int f(char *, int, float, float, int, int, char *)
#define PHP_i_siffiis(fname)\
ZEND_FUNCTION(fname)\
{\
long s_len1;\
long s_len2;\
char *a;\
long b;\
double c,d;\
long e,f;\
char *g;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "slddlls", &a, &s_len1, &b, &c, &d, &e, &f, &g, &s_len2) == FAILURE)  return;\
RETURN_LONG(fname(a,b,c,d,e,f,g));\
}

#define PHP_i_siffiii(fname)\
ZEND_FUNCTION(fname)\
{\
long s_len1;\
char *a;\
long b;\
double c,d;\
long e,f;\
long g;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "slddlll", &a, &s_len1, &b, &c, &d, &e, &f, &g) == FAILURE)  return;\
RETURN_LONG(fname(a,b,c,d,e,f,g));\
}

// int f(char *, int, float, float, float, int, int, char *)
#define PHP_i_sifffiis(fname)\
ZEND_FUNCTION(fname)\
{\
long s_len1;\
long s_len2;\
char *a;\
long b;\
double c,d,e;\
long g,h;\
char *i;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "sldddlls", &a, &s_len1, &b, &c, &d, &e, &g, &h, &i, &s_len2) == FAILURE)  return;\
RETURN_LONG(fname(a,b,c,d,e,g,h,i));\
}


// int f(char *, int, float, float, float, float, int, int, char *)
#define PHP_i_siffffiis(fname)\
ZEND_FUNCTION(fname)\
{\
long s_len1;\
long s_len2;\
char *a;\
long b;\
double c,d,e,f;\
long g,h;\
char *i;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "slddddlls", &a, &s_len1, &b, &c, &d, &e, &f, &g, &h, &i, &s_len2) == FAILURE)  return;\
RETURN_LONG(fname(a,b,c,d,e,f,g,h,i));\
}


// void f(char *, int)
#define PHP_v_si(fname)\
ZEND_FUNCTION(fname)\
{\
char *a;\
long s_len;\
long b;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "sl", &a, &s_len, &b) == FAILURE)  return;\
fname(a,b);\
}

// int f(char *, float)
#define PHP_i_sf(fname)\
ZEND_FUNCTION(fname)\
{\
char *a;\
long s_len;\
double b;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "sd", &a, &s_len, &b) == FAILURE)  return;\
RETURN_LONG(fname(a,b));\
}


// int f(char *, float, float)
#define PHP_i_sff(fname)\
ZEND_FUNCTION(fname)\
{\
char *a;\
long s_len;\
double b,c;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "sdd", &a, &s_len, &b, &c) == FAILURE)  return;\
RETURN_LONG(fname(a,b,c));\
}

// void f(char *, float, float)
#define PHP_v_sff(fname)\
ZEND_FUNCTION(fname)\
{\
char *a;\
long s_len;\
double b,c;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "sdd", &a, &s_len, &b, &c) == FAILURE)  return;\
fname(a,b,c);\
}

// void f(char *, float, float, float)
#define PHP_v_sfff(fname)\
ZEND_FUNCTION(fname)\
{\
char *a;\
long s_len;\
double b,c,d;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "sddd", &a, &s_len, &b, &c, &d) == FAILURE)  return;\
fname(a,b,c,d);\
}

// void f(char *, float, float, float, float, float, float)
#define PHP_v_sffffff(fname)\
ZEND_FUNCTION(fname)\
{\
char *a;\
long s_len;\
double b,c,d;\
double e,f,g;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "sdddddd", &a, &s_len, &b, &c, &d, &e, &f, &g) == FAILURE)  return;\
fname(a,b,c,d,e,f,g);\
}


// void f(char *, char *, int)
#define PHP_v_ssi(fname)\
ZEND_FUNCTION(fname)\
{\
char *a;\
long s_len1;\
char *b;\
long s_len2;\
long c;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "ssl", &a, &s_len1, &b, &s_len2, &c) == FAILURE)  return;\
fname(a,b,c);\
}

// void f(char *, char *, float)
#define PHP_v_ssf(fname)\
ZEND_FUNCTION(fname)\
{\
char *a;\
long s_len1;\
char *b;\
long s_len2;\
double c;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "ssd", &a, &s_len1, &b, &s_len2, &c) == FAILURE)  return;\
fname(a,b,c);\
}


// void f(char *, char *, float, float)
#define PHP_v_ssff(fname)\
ZEND_FUNCTION(fname)\
{\
char *a;\
long s_len1;\
char *b;\
long s_len2;\
double c,d;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "ssdd", &a, &s_len1, &b, &s_len2, &c, &d) == FAILURE)  return;\
fname(a,b,c,d);\
}

// void f(char *, char *, float, float, float)
#define PHP_v_ssfff(fname)\
ZEND_FUNCTION(fname)\
{\
char *a;\
long s_len1;\
char *b;\
long s_len2;\
double c,d,e;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "ssddd", &a, &s_len1, &b, &s_len2, &c, &d, &e) == FAILURE)  return;\
fname(a,b,c,d,e);\
}

// void f(char *, char *, float, float, float, float)
#define PHP_v_ssffff(fname)\
ZEND_FUNCTION(fname)\
{\
char *a;\
long s_len1;\
char *b;\
long s_len2;\
double c,d,e,f;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "ssdddd", &a, &s_len1, &b, &s_len2, &c, &d, &e, &f) == FAILURE)  return;\
fname(a,b,c,d,e,f);\
}

// void f(char *, char *)
#define PHP_v_ss(fname)\
ZEND_FUNCTION(fname)\
{\
char *a;\
long s_len1;\
char *b;\
long s_len2;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "ss", &a, &s_len1, &b, &s_len2) == FAILURE)  return;\
fname(a,b);\
}

// void f(char *, char *)
// (2nd arg: rw string)
#define PHP_v_sS(fname)\
ZEND_FUNCTION(fname)\
{\
char *a;\
long s_len1;\
ZSTR_DECL(b)\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "sz", &a, &s_len1, &b_zend) == FAILURE)  return;\
ZSTR_INIT(b)\
fname(a,b_c);\
ZSTR_AFFECT(b)\
}

// void f(char *, float)
#define PHP_v_sf(fname)\
ZEND_FUNCTION(fname)\
{\
char *a;\
long s_len1;\
double b;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "sd", &a, &s_len1, &b) == FAILURE)  return;\
fname(a,b);\
}

// int f(char *, char *, char *, float, float, float, float, float, float)
#define PHP_i_sssffffff(fname)\
ZEND_FUNCTION(fname)\
{\
char *a;\
long s_len1;\
char *b;\
long s_len2;\
char *c;\
long s_len3;\
double d,e,f;\
double g,h,i;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "sssdddddd", &a, &s_len1, &b, &s_len2,&c, &s_len3, &d, &e ,&f, &g, &h, &i) == FAILURE)  return;\
RETURN_LONG(fname(a,b,c,d,e,f,g,h,i));\
}

// int f(char *, float, float, float, float)
#define PHP_i_sffff(fname)\
ZEND_FUNCTION(fname)\
{\
char *a;\
long s_len1;\
double b,c,d,e;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "sdddd", &a, &s_len1, &b, &c, &d, &e) == FAILURE)  return;\
RETURN_LONG(fname(a,b,c,d,e));\
}

// int f(char *, char *, float, float, float, float)
#define PHP_i_ssffff(fname)\
ZEND_FUNCTION(fname)\
{\
char *a;\
long s_len1;\
char *b;\
long s_len2;\
double c,d,e,f;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "ssdddd", &a, &s_len1, &b, &s_len2, &c, &d, &e ,&f) == FAILURE)  return;\
RETURN_LONG(fname(a,b,c,d,e,f));\
}

// int f(char *, char *, float, float, float, float)
#define PHP_i_sssffff(fname)\
ZEND_FUNCTION(fname)\
{\
char *a;\
long s_len1;\
char *b;\
long s_len2;\
char *c;\
long s_len3;\
double d,e,f;\
double g;\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\
  "sssdddd", &a, &s_len1, &b, &s_len2,&c, &s_len3, &d, &e ,&f, &g) == FAILURE)  return;\
RETURN_LONG(fname(a,b,c,d,e,f,g));\
}

#endif