home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Languages / python / PyObjC-0.47-MIHS / pyobjc-0.47-src / Modules / nsdefaults.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-03  |  12.5 KB  |  559 lines

  1. /* Copyright (c) 1995 by Lele Gaifax.  All Rights Reserved
  2.  *
  3.  * $Source: /LocalDeveloper/cvs/net/psa/pyobjc/Modules/nsdefaults.c,v $
  4.  * $Revision: 1.3 $
  5.  * $Date: 1996/10/03 18:32:48 $
  6.  *
  7.  */
  8.  
  9.  
  10. #include "Python.h"
  11. #include "modsupport.h"        /* For getargs() etc. */
  12. // #include <appkit/Application.h>    /* For NXHomeDirectory() */
  13. extern const char *NXHomeDirectory(void); /* let's use gcc! */
  14.  
  15. #include <defaults/defaults.h>
  16. #include <db/db.h>
  17.  
  18. static PyObject * NsdefaultsError;
  19.  
  20. /* Given a Python dictionary, allocate a NXDefaultVector with the same
  21.    number of elements plus one for the ending null ''sentinel'' and
  22.    initialize it with the content of the dictionary.
  23.  
  24.    Elements of the array point to the string in the dictionary, so
  25.    it's not a good idea use it after VALUES has been DECREFed.
  26.  
  27.    Both keys and values must be string, otherwise NULL is returned.
  28.  
  29.    Returns the new NXDefaultVector. */
  30.  
  31. static struct _NXDefault *
  32. build_vector_from_dict (PyObject * values)
  33. {
  34.   struct _NXDefault * vector;
  35.   int pos;
  36.   int idx;
  37.   PyObject * key;
  38.   PyObject * value;
  39.  
  40.   vector = PyMem_NEW(struct _NXDefault, PyDict_Size (values) + 1);
  41.   idx = pos = 0;
  42.   while (PyDict_Next (values, &pos, &key, &value))
  43.     {
  44.       char * n = PyString_AsString (key);
  45.       char * v = PyString_AsString (value);
  46.  
  47.       if (!n || !v)
  48.     {
  49.       PyMem_DEL (vector);
  50.       PyErr_SetString (NsdefaultsError, "Keys and values must be string");
  51.       return NULL;
  52.     }
  53.       
  54.       vector[idx].name = n;
  55.       vector[idx].value = v;
  56.  
  57.       idx++;
  58.     }
  59.   vector[idx].name = vector[idx].value = 0;
  60.   return vector;
  61. }
  62.  
  63. static char register_defaults_doc[] =
  64. "Register default parameters in the cache.\n\
  65. \n\
  66. Arguments:\n\
  67.     OWNER    the name of the owner of these parameters\n\
  68.     PARAMS    a dictionary containing the parameters with their\n\
  69.         default values\n\
  70. \n\
  71. Returns:\n\
  72.   A boolean value, TRUE on succeding operation, FALSE if something goes\n\
  73.   wrong.";
  74.   
  75. static PyObject *
  76. register_defaults (PyObject * self, PyObject * args)
  77. {
  78.   const char * owner;
  79.   PyObject * values;
  80.  
  81.   if (PyArg_ParseTuple (args, "sO!;a string and a dictionary", &owner, &PyDict_Type, &values))
  82.     {
  83.       struct _NXDefault * vector = build_vector_from_dict (values);
  84.       int status;
  85.  
  86.       if (!vector)
  87.     return NULL;
  88.       
  89.       if (! (status = NXRegisterDefaults (owner, vector)))
  90.     PyErr_SetString (NsdefaultsError, "could't open the default database");
  91.       
  92.       PyMem_DEL(vector);
  93.  
  94.       if (! status)
  95.     {
  96.       Py_INCREF (Py_False);
  97.       return Py_False;
  98.     }
  99.       else
  100.     {
  101.       Py_INCREF (Py_True);
  102.       return Py_True;
  103.     }
  104.     }
  105.   else
  106.     return NULL;
  107. }
  108.  
  109. static char get_default_value_doc[] =
  110. "Look in the cache for a parameter and return its value if found.\n\
  111. \n\
  112. Arguments:\n\
  113.     OWNER    the name of the owner of the parameter\n\
  114.     PARAM    the name of the paramenter\n\
  115. \n\
  116. Returns:\n\
  117.   A string representing the value of the parameter if it is found, None\n\
  118.   otherwise.";
  119.  
  120. static PyObject *
  121. get_default_value (PyObject * self, PyObject * args)
  122. {
  123.   char * owner;
  124.   char * name;
  125.  
  126.   if (PyArg_ParseTuple (args, "ss;two strings", &owner, &name))
  127.     {
  128.       const char * value = NXGetDefaultValue (owner, name);
  129.  
  130.       if (value)
  131.     {
  132.       return Py_BuildValue ("s", value);
  133.     }
  134.       else
  135.     {
  136.       Py_INCREF(Py_None);
  137.       return Py_None;
  138.     }
  139.     }
  140.   else
  141.     return NULL;
  142. }
  143.   
  144. static char read_default_doc[] =
  145. "Look in the Default Database for a parameter and return its value if found.\n\
  146. \n\
  147. Arguments:\n\
  148.     OWNER    the name of the owner of the parameter\n\
  149.     PARAM    the name of the paramenter\n\
  150. \n\
  151. Returns:\n\
  152.   A string representing the value of the parameter if it is found, None\n\
  153.   otherwise.";
  154.  
  155. static PyObject *
  156. read_default (PyObject * self, PyObject * args)
  157. {
  158.   char * owner;
  159.   char * name;
  160.  
  161.   if (PyArg_ParseTuple (args, "ss;two strings", &owner, &name))
  162.     {
  163.       const char * value = NXReadDefault (owner, name);
  164.  
  165.       if (value)
  166.     {
  167.       return Py_BuildValue ("s", value);
  168.     }
  169.       else
  170.     {
  171.       Py_INCREF(Py_None);
  172.       return Py_None;
  173.     }
  174.     }
  175.   else
  176.     return NULL;
  177. }
  178.  
  179. static char remove_default_doc[] =
  180. "Remove from the Default Database a parameter.\n\
  181. \n\
  182. Arguments:\n\
  183.     OWNER    the name of the owner of the parameter\n\
  184.     PARAM    the name of the parameter\n\
  185. \n\
  186. Returns:\n\
  187.   A boolean value, TRUE on succeding operation, FALSE if something goes\n\
  188.   wrong.";
  189.  
  190. static PyObject *
  191. remove_default (PyObject * self, PyObject * args)
  192. {
  193.   char * owner;
  194.   char * name;
  195.  
  196.   if (PyArg_ParseTuple (args, "ss;two strings", &owner, &name))
  197.     {
  198.       int status = NXRemoveDefault (owner, name);
  199.  
  200.       if (status == 0)
  201.     {
  202.       PyErr_SetString (NsdefaultsError, "couldn't remove the value");
  203.       Py_INCREF (Py_False);
  204.       return Py_False;
  205.     }
  206.       else
  207.     {
  208.       Py_INCREF (Py_True);
  209.       return Py_True;
  210.     }
  211.     }
  212.   else
  213.     return NULL;
  214. }
  215.  
  216. static char set_default_doc[] =
  217. "Store a default parameters with its value in the cache.\n\
  218. \n\
  219. Arguments:\n\
  220.     OWNER    the name of the owner of this parameter\n\
  221.     PARAM    the name of the parameter\n\
  222.     VALUE    its value\n\
  223. \n\
  224. Returns:\n\
  225.   A boolean value, TRUE on succeding operation, FALSE if something goes\n\
  226.   wrong.";
  227.  
  228. static PyObject *
  229. set_default (PyObject * self, PyObject * args)
  230. {
  231.   char * owner;
  232.   char * name;
  233.   char * value;
  234.  
  235.   if (PyArg_ParseTuple (args, "sss;three strings", &owner, &name, &value))
  236.     {
  237.       int status = NXSetDefault (owner, name, value);
  238.  
  239.       if (status == 0)
  240.     {
  241.       PyErr_SetString (NsdefaultsError, "couldn't set the value");
  242.       Py_INCREF (Py_False);
  243.       return Py_False;
  244.     }
  245.       else
  246.     {
  247.       Py_INCREF (Py_True);
  248.       return Py_True;
  249.     }
  250.    }
  251.   else
  252.     return NULL;
  253. }
  254.  
  255. static char update_default_doc[] =
  256. "Update a default parameter in the cache from the Default Database.\n\
  257. \n\
  258. Arguments:\n\
  259.     OWNER    the name of the owner of this parameter\n\
  260.     PARAM    the name of the parameter\n\
  261. \n\
  262. Returns:\n\
  263.   A string representing the previous value of the parameter if found,\n\
  264.   None otherwise.";
  265.  
  266. static PyObject *
  267. update_default (PyObject * self, PyObject * args)
  268. {
  269.   char * owner;
  270.   char * name;
  271.  
  272.   if (PyArg_ParseTuple (args, "ss;two strings", &owner, &name))
  273.     {
  274.       const char * value = NXUpdateDefault (owner, name);
  275.  
  276.       if (value)
  277.     {
  278.       return Py_BuildValue ("s", value);
  279.     }
  280.       else
  281.     {
  282.       Py_INCREF(Py_None);
  283.       return Py_None;
  284.     }
  285.     }
  286.   else
  287.     return NULL;
  288. }
  289.  
  290. static char update_defaults_doc[] =
  291. "Update a the cache re-reading the entire Default Database.\n\
  292. \n\
  293. Arguments:\n\
  294.     None\n\
  295. \n\
  296. Returns:\n\
  297.   None";
  298.  
  299. static PyObject *
  300. update_defaults (PyObject * self, PyObject * args)
  301. {
  302.   NXUpdateDefaults ();
  303.  
  304.   Py_INCREF(Py_None);
  305.   return Py_None;
  306. }
  307.  
  308. static char write_default_doc[] =
  309. "Store a default parameters with its value in the Default Database.\n\
  310. \n\
  311. Arguments:\n\
  312.     OWNER    the name of the owner of this parameter\n\
  313.     PARAM    the name of the parameter\n\
  314.     VALUE    its value\n\
  315. \n\
  316. Returns:\n\
  317.   A boolean value, TRUE on succeding operation, FALSE if something goes\n\
  318.   wrong.";
  319.  
  320. static PyObject *
  321. write_default (PyObject * self, PyObject * args)
  322. {
  323.   char * owner;
  324.   char * name;
  325.   char * value;
  326.  
  327.   if (PyArg_ParseTuple (args, "sss;three strings", &owner, &name, &value))
  328.     {
  329.       int status = NXWriteDefault (owner, name, value);
  330.  
  331.       if (status == 0)
  332.     {
  333.       PyErr_SetString (NsdefaultsError, "couldn't write the value");
  334.       Py_INCREF (Py_False);
  335.       return Py_False;
  336.     }
  337.       else
  338.     {
  339.       Py_INCREF (Py_True);
  340.       return Py_True;
  341.     }
  342.     }
  343.   else
  344.     return NULL;
  345. }
  346.  
  347. static char write_defaults_doc[] =
  348. "Write a set of parameters and their values into the Default Database.\n\
  349. \n\
  350. Arguments:\n\
  351.     OWNER    the name of the owner of these parameters\n\
  352.     PARAMS    a dictionary containing the parameters with their\n\
  353.         default values\n\
  354. \n\
  355. Returns:\n\
  356.   An integer value representing the number of succesfully written parameters";
  357.   
  358. static PyObject *
  359. write_defaults (PyObject * self, PyObject * args)
  360. {
  361.   const char * owner;
  362.   PyObject * values;
  363.  
  364.   if (PyArg_ParseTuple (args, "sO!;a string and a dictionary", &owner, &PyDict_Type, &values))
  365.     {
  366.       struct _NXDefault * vector = build_vector_from_dict (values);
  367.       int written;
  368.  
  369.       if (!vector)
  370.     return NULL;
  371.  
  372.       written = NXWriteDefaults (owner, vector);
  373.       
  374.       PyMem_DEL(vector);
  375.  
  376.       return Py_BuildValue ("i", written);
  377.     }
  378.   else
  379.     return NULL;
  380. }
  381.  
  382. static char set_defaults_user_doc[] =
  383. "Set the name of the user whose database you want to use by subsequent calls to these functions.\n\
  384. \n\
  385. Arguments:\n\
  386.     USER    the name of the user\n\
  387. \n\
  388. Returns:\n\
  389.   A string containing the name of the previous user.";
  390.  
  391. static PyObject *
  392. set_defaults_user (PyObject * self, PyObject * args)
  393. {
  394.   char * user;
  395.  
  396.   if (PyArg_ParseTuple (args, "s;a string", &user))
  397.     {
  398.       const char * value = NXSetDefaultsUser (user);
  399.  
  400.       if (value)
  401.     {
  402.       return Py_BuildValue ("s", value);
  403.     }
  404.       else
  405.     {
  406.       Py_INCREF(Py_None);
  407.       return Py_None;
  408.     }
  409.     }
  410.   else
  411.     return NULL;
  412. }
  413.  
  414. static char defaults_doc[] =
  415. "Build a dictionary corresponding to the current Defaults database content.\n\
  416. Does not honour NXSetDefaultsUser() setting, always reads .NeXT/.NeXTdefaults\n\
  417. in the current user's home directory.\n\
  418. \n\
  419. Arguments:\n\
  420.     OWNER    optional name of the owner. If not given, fetch all entries.\n\
  421. \n\
  422. Returns:\n\
  423.   A dictionary containing name and value of each default of OWNER if given,\n\
  424.   otherwise a dictionary of dictionaries, one for each owner";
  425.  
  426. static PyObject *
  427. defaults (PyObject * self, PyObject * args)
  428. {
  429.   char *ownern = NULL;
  430.  
  431.   if (PyArg_ParseTuple (args, "|s;a string", &ownern))
  432.     {
  433. #define DEFDBNAME "/.NeXT/.NeXTdefaults"
  434.       const char *userhome = NXHomeDirectory();
  435.       char dbname[strlen (userhome) + sizeof DEFDBNAME];
  436.       Database *db;
  437.  
  438.       strcpy (dbname, userhome);
  439.       strcat (dbname, DEFDBNAME);
  440.  
  441.       if ((db = dbOpen (dbname)))
  442.     {
  443.       Data data;
  444.       char key[1024], content[1024];
  445.       PyObject *dict;
  446.       
  447.       data.k.s = key;
  448.       data.c.s = content;
  449.  
  450.       dict = PyDict_New();
  451.       if (!dict)
  452.         return NULL;
  453.  
  454.       for (dbFirst (db, &data); dbGet (db, &data); dbNext (db, &data))
  455.         {
  456.           char *o = key;
  457.           register char *def = key;
  458.  
  459.           key[data.k.n] = '\0';
  460.           content[data.c.n] = '\0';
  461.  
  462.           while (*def != '\377')
  463.         def++;
  464.  
  465.           *def = 0;
  466.           def++;
  467.           
  468.           if (!ownern || !strcmp (o, ownern))
  469.         {
  470.           PyObject *pydef = PyString_FromString (def);
  471.           PyObject *pyval = PyString_FromString (content);
  472.  
  473.           if (!pydef || !pyval)
  474.             {
  475.               Py_XDECREF(pydef);
  476.               Py_XDECREF(pyval);
  477.               Py_DECREF(dict);
  478.               
  479.               return NULL;
  480.             }
  481.           
  482.           if (ownern)
  483.             {
  484.               PyDict_SetItem (dict, pydef, pyval);
  485.             }
  486.           else
  487.             {
  488.               PyObject *ownerd = PyDict_GetItemString (dict, o);
  489.  
  490.               if (!ownerd)
  491.             {
  492.               PyErr_Clear();
  493.               ownerd = PyDict_New();
  494.               PyDict_SetItemString (dict, o, ownerd);
  495.               Py_DECREF(ownerd);
  496.             }
  497.  
  498.               PyDict_SetItem (ownerd, pydef, pyval);
  499.             }
  500.           
  501.           Py_DECREF(pydef);
  502.           Py_DECREF(pyval);
  503.         }
  504.         }
  505.       
  506.       return dict;
  507.     }
  508.       else
  509.     {
  510. #define ERRMSG "Can't open `%s'"
  511.       char errmsg[strlen (dbname) + sizeof ERRMSG];
  512.  
  513.       sprintf (errmsg, ERRMSG, dbname);
  514.       PyErr_SetString (NsdefaultsError, errmsg);
  515. #undef ERRMSG
  516.     }
  517.     }
  518.  
  519.   return NULL;
  520. }
  521.  
  522. /* List of functions defined in the module */
  523.  
  524. static PyMethodDef nsdefaults_methods[] = {
  525.   { "registerDefaults",    (PyCFunction) register_defaults,    METH_VARARGS, register_defaults_doc },
  526.   { "getDefaultValue",     (PyCFunction) get_default_value,    METH_VARARGS, get_default_value_doc },
  527.   { "readDefault",     (PyCFunction) read_default,        METH_VARARGS, read_default_doc },
  528.   { "removeDefault",     (PyCFunction) remove_default,        METH_VARARGS, remove_default_doc },
  529.   { "setDefault",     (PyCFunction) set_default,        METH_VARARGS, set_default_doc },
  530.   { "updateDefault",     (PyCFunction) update_default,        METH_VARARGS, update_default_doc },
  531.   { "updateDefaults",     (PyCFunction) update_defaults,        METH_VARARGS, update_defaults_doc },
  532.   { "writeDefault",     (PyCFunction) write_default,        METH_VARARGS, write_default_doc },
  533.   { "writeDefaults",     (PyCFunction) write_defaults,        METH_VARARGS, write_defaults_doc },
  534.   { "setDefaultsUser",     (PyCFunction) set_defaults_user,    METH_VARARGS, set_defaults_user_doc },
  535.   { "defaults",        (PyCFunction) defaults,            METH_VARARGS, defaults_doc },
  536.   { 0, 0, 0, 0 }        /* sentinel */
  537. };
  538.  
  539. static char nsdefaults_doc[] =
  540. "Wrapper around the NXDefault facility on NeXTSTEP";
  541.  
  542. void
  543. initnsdefaults()
  544. {
  545.   PyObject *m, *d;
  546.  
  547.   /* Create the module and add the functions */
  548.   m = Py_InitModule4 ("nsdefaults", nsdefaults_methods, nsdefaults_doc, NULL, PYTHON_API_VERSION);
  549.   
  550.   /* Add some symbolic constants to the module */
  551.   d = PyModule_GetDict(m);
  552.   NsdefaultsError = PyString_FromString ("nsdefaults.error");
  553.   PyDict_SetItemString (d, "error", NsdefaultsError);
  554.   
  555.   /* Check for errors */
  556.   if (PyErr_Occurred())
  557.     Py_FatalError ("can't initialize module nsdefaults");
  558. }
  559.