WoMax Documentation

# OMax.oracle/OMax.oracle.c

    00001 /*---------------------------------
    00002  * OMax.oracle - OMax.oracle.c
    00003  * Created on 09/03/09 by BenCello
    00004  *---------------------------------*/
    00005
    00007
    00008 #ifndef __OMAX_ORACLE_MAX_API__
    00009 #define __OMAX_ORACLE_MAX_API__
    00010
    00011 #include <sstream>
    00012 using namespace std;
    00013
    00014 #include "Oracle_classes.hpp"
    00015
    00016 //extern "C"
    00017 //{
    00018         #include "OMax.oracle.h"                // object structure
    00019
    00021         // Prototypes //
    00023
    00024         // Standard Max5 methodes
    00025         void *OMax_oracle_new(t_symbol *s, long argc, t_atom *argv);
    00026         void OMax_oracle_free(t_OMax_oracle *x);
    00027         void OMax_oracle_assist(t_OMax_oracle *x, void *b, long io, long index, char *s);
    00028
    00029         // Input/ouput routines
    00030         void OMax_oracle_size(t_OMax_oracle *x);
    00031         void OMax_oracle_write(t_OMax_oracle *x, t_symbol *s);
    00032         void OMax_oracle_init(t_OMax_oracle *x);
    00033         void OMax_oracle_reset(t_OMax_oracle *x);
    00034
    00035         // Internal routines
    00036         void OMax_oracle_dowrite(t_OMax_oracle *x, t_symbol *s);
    00037         void OMax_oracle_writefile(t_OMax_oracle *x, char *filename, short path);
    00038
    00039         // Global class pointer variable
    00040         t_class *OMax_oracle_class;
    00041
    00043         // Functions //
    00045
    00046         int main(void)
    00047         {
    00048                 t_class *c;
    00049
    00050                 c = class_new("OMax.oracle", (method)OMax_oracle_new, (method)OMax_oracle_free, (long)sizeof(t_OMax_oracle),
    00051                                           0L /* leave NULL!! */, A_GIMME, 0);
    00052
    00053                 // assistance
    00054                 class_addmethod(c, (method)OMax_oracle_assist,"assist",A_CANT, 0);
    00055
    00056                 // input methods declarations
    00057                 class_addmethod(c, (method)OMax_oracle_size, "size", 0);
    00058                 class_addmethod(c, (method)OMax_oracle_write, "write", A_DEFSYM, 0);
    00059                 class_addmethod(c, (method)OMax_oracle_init, "init", 0);
    00060                 class_addmethod(c, (method)OMax_oracle_reset, "reset", 0);
    00061
    00062
    00063                 class_register(CLASS_BOX, c); /* CLASS_NOBOX */
    00064                 OMax_oracle_class = c;
    00065
    00066                 return 0;
    00067         }
    00068
    00070
    00071
    00074         void *OMax_oracle_new(t_symbol *s, long argc, t_atom *argv)
    00075         {
    00076
    00077                 t_OMax_oracle *x = NULL;
    00078
    00079                 if (x = (t_OMax_oracle *)object_alloc(OMax_oracle_class))
    00080                 {
    00081                         x->oracle = O_oracle();
    00082                         x->out0 = outlet_new(x, NULL);
    00083
    00084                         if (argc == 0)
    00085                                 object_error((t_object *)x,"Missing name for the Oracle");
    00086                         else
    00087                         {
    00088                                 if (argv->a_type!= A_SYM)
    00089                                         object_error((t_object *)x,"First argument must be a symbol (name of the Oracle)");
    00090                                 else
    00091                                 {
    00093                                         x->oname = atom_getsym(argv);
    00094                                         x->oname->s_thing = (t_object*)x;
    00095                                         object_post((t_object *)x,"Oracle %s declared",x->oname->s_name);
    00096                                 }
    00097                         }
    00098                         //write flags
    00099                         x->wflag = 0;
    00100                         x->readcount = 0;
    00101
    00102                         // color
    00103                         t_object *box;
    00104                         t_jrgba colorvals;
    00105                         jrgba_set(&colorvals, 0.30, 1.0, 0.15, 1.0);
    00106                         object_obex_lookup((t_object *)x, gensym("#B"), &box);
    00107                         jbox_set_color(box, &colorvals);
    00108                 }
    00109                 return (x);
    00110         }
    00111
    00114         void OMax_oracle_free(t_OMax_oracle *x)
    00115         {
    00117                 x->oracle.freestates();
    00118         }
    00119
    00123         void OMax_oracle_assist(t_OMax_oracle *x, void *b, long io, long index, char *s)
    00124         {
    00125                 switch (io)
    00126                 {
    00127             case 1: // inlets
    00128                                 switch (index)
    00129                         {
    00130                                 case 0: // leftmost
    00131                                         sprintf(s, "messages (init, reset, write, size)");
    00132                                         break;
    00133                         }
    00134                 break;
    00135             case 2: // outlets
    00136                                 switch (index)
    00137                         {
    00138                                 case 0:
    00139                                         sprintf(s, "Anything you asked");
    00140                                         break;
    00141                         }
    00142                                 break;
    00143                 }
    00144         }
    00145
    00147
    00149
    00150
    00154         void OMax_oracle_size(t_OMax_oracle *x)
    00155         {
    00156                 outlet_int(x->out0,(long)x->oracle.get_size());
    00157         }
    00158
    00162         void OMax_oracle_write(t_OMax_oracle *x, t_symbol *s)
    00163         {
    00164                 defer(x, (method)OMax_oracle_dowrite, s, 0, NULL);
    00165         }
    00166
    00170         void OMax_oracle_init(t_OMax_oracle *x)
    00171         {
    00172                 x->oracle.start();
    00173         }
    00174
    00178         void OMax_oracle_reset(t_OMax_oracle *x)
    00179         {
    00180                 x->oracle.freestates();
    00181                 outlet_int(x->out0,(long)x->oracle.get_size());
    00182         }
    00183
    00185
    00187
    00188
    00192         void OMax_oracle_dowrite(t_OMax_oracle *x, t_symbol *s)
    00193         {
    00194                 long filetype = 'TEXT';
    00195                 long outtype = 'TEXT';
    00196                 short numtypes = 1;
    00197                 char filename[512];
    00198                 short path;
    00199
    00200                 if (s == gensym(""))
    00201                 {      // if no argument supplied, ask for file
    00202                         if (saveasdialog_extended(filename, &path, &outtype, &filetype, numtypes))     // non-zero: user cancelled
    00203                                 return;
    00204                 }
    00205                 else
    00206                 {
    00207                         strcpy(filename, s->s_name);
    00208                         path = path_getdefault();
    00209                 }
    00210                 OMax_oracle_writefile(x, filename, path);
    00211         }
    00212
    00216         void OMax_oracle_writefile(t_OMax_oracle *x, char *filename, short path)
    00217         {
    00218                 char *buf;
    00219                 long err;
    00220                 long count;
    00221                 stringstream ssout;
    00222                 t_filehandle fh;
    00223                 err = path_createsysfile(filename, path, 'TEXT', &fh);
    00224                 if (err)
    00225                         return;
    00226                 ssout<<x->oracle;
    00227                 string sout = ssout.str();
    00228                 count = sout.size();
    00229                 buf = (char *)sout.c_str();
    00230                 err = sysfile_write(fh, &count, buf);
    00231                 sysfile_close(fh);
    00232         }
    00233
    00235 //}
    00236
    00237 #endif