source: TI05-delivery/trunk/src/python_ext/util.c @ 1289

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/trunk/src/python_ext/util.c@1289
Revision 1289, 2.7 KB checked in by spascoe, 13 years ago (diff)

Updated python code to reflect the renaming of the delivery package to pybbftp.
Some C tidying to reduce compiler warnings.

Line 
1/**
2 * NDG python embedded bbftp daemon module.
3 *
4 * @author Stephen Pascoe
5 *
6 * Copyright (C) 2006 CCLRC & NERC
7 *
8 * This software may be distributed under the terms of the Q Public Licence, version 1.0 or later.
9 *
10 */
11
12#include <stdlib.h>
13#include <string.h>
14#include <Python.h>
15
16#include "util.h"
17
18/**
19 * Routines for constructing a argc/argv style argument list.
20 */
21
22/**
23 * Initialise a structure.
24 * @return TRUE on success or FALSE on a memory error.
25 */
26int ndg_argv_init(struct ndg_argv *argv_s, int argc) {
27  argv_s->argc = argc;
28
29  if ((argv_s->argv = (char **)malloc(argc * sizeof(char *))) == NULL) {
30    return FALSE;
31  }
32
33  argv_s->next = 0;
34
35  return TRUE;
36}
37
38/**
39 * Free a structure.  \a argv_s should be cleared first with \c ndg_argv_clear().
40 *
41 * @return TRUE on success or FALSE if the structure isn't empty.
42 */
43int ndg_argv_free(struct ndg_argv *argv_s) {
44  if (argv_s->next != 0) return FALSE;
45  free(argv_s->argv);
46  return TRUE;
47}
48
49/**
50 * Clear all arguments from the structure.
51 *
52 */
53void ndg_argv_clear(struct ndg_argv *argv_s) {
54  char *arg;
55  while (argv_s->next > 0) {
56    argv_s->next--;
57    arg = argv_s->argv[argv_s->next];
58    free(arg);
59  }
60}
61
62/**
63 * Add an argument.
64 *
65 * @return TRUE on success or FALSE on an error.
66 */
67int ndg_argv_add(struct ndg_argv *argv_s, char *arg) {
68
69  if (argv_s->argc <= argv_s->next) return FALSE;
70  if ((argv_s->argv[argv_s->next] = (char *)malloc(strlen(arg) + 1)) == NULL) {
71    return FALSE;
72  }
73  strcpy(argv_s->argv[argv_s->next], arg);
74  argv_s->next++;
75  return TRUE;
76}
77
78
79/**
80 * Copy the python exception message to a bbftp logmessage.
81 *
82 * If the python error indicator is set it will be cleared.
83 * If the python error indicator is not set the empty string will be copied to \a logmessage.
84 * The exception message is truncated if it exceeds \c NDG_MAX_LOGMESSAGE.
85 *
86 * @param logmessage a pointer to a buffer of length \c NDG_MAX_LOGMESSAGE
87 *     for storing an error message when returning -1.
88 * @param prefix a string to be prefixed to the logmessage.
89 */
90void ndg_pyerr_to_logmessage(char *logmessage, char *prefix) {
91  PyObject *ptype, *pvalue, *ptraceback, *obj;
92  char *exname, *exvalue;
93
94  PyErr_Fetch(&ptype, &pvalue, &ptraceback);
95 
96  if ((obj = PyObject_GetAttrString(ptype, "__name__")) == NULL) goto error;
97  exname = PyString_AsString(obj); Py_DECREF(obj);
98  if (exname == NULL) goto error;
99
100  if ((obj = PyObject_Str(pvalue)) == NULL) goto error;
101  exvalue = PyString_AsString(obj); Py_DECREF(obj);
102  if (exvalue == NULL) goto error;
103
104  snprintf(logmessage, NDG_MAX_LOGMESSAGE, "%s: %s: %s", prefix, exname, exvalue);
105  return;
106 
107 error:
108  sprintf("ndg_pyerr_to_logmessage: internal python error", logmessage);
109  return;
110}
Note: See TracBrowser for help on using the repository browser.