Ejemplo n.º 1
0
static int rpmfd_init(rpmfdObject *s, PyObject *args, PyObject *kwds)
{
    char *kwlist[] = { "obj", "mode", "flags", NULL };
    const char *mode = "r";
    const char *flags = "ufdio";
    char *rpmio_mode = NULL;
    PyObject *fo = NULL;
    FD_t fd = NULL;
    int fdno;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|ss", kwlist, 
				     &fo, &mode, &flags))
	return -1;

    rpmio_mode = rstrscat(NULL, mode, ".", flags, NULL);

    if (PyBytes_Check(fo)) {
	fd = openPath(PyBytes_AsString(fo), rpmio_mode);
    } else if (PyUnicode_Check(fo)) {
	PyObject *enc = NULL;
	int rc;
#if PY_MAJOR_VERSION >= 3
	rc = PyUnicode_FSConverter(fo, &enc);
#else
	rc = utf8FromPyObject(fo, &enc);
#endif
	if (rc) {
	    fd = openPath(PyBytes_AsString(enc), rpmio_mode);
	    Py_DECREF(enc);
	}
    } else if (rpmfdObject_Check(fo)) {
	rpmfdObject *fdo = (rpmfdObject *)fo;
	fd = openFd(fdDup(Fileno(fdo->fd)), rpmio_mode);
    } else if ((fdno = PyObject_AsFileDescriptor(fo)) >= 0) {
	fd = openFd(fdDup(fdno), rpmio_mode);
    } else {
	PyErr_SetString(PyExc_TypeError, "path or file object expected");
    }

    if (fd != NULL) {
	Fclose(s->fd); /* in case __init__ was called again */
	free(s->mode);
	free(s->flags);
	s->fd = fd;
	s->mode = rstrdup(mode);
	s->flags = rstrdup(flags);
    } else {
	PyErr_SetString(PyExc_IOError, Fstrerror(fd));
    }

    free(rpmio_mode);
    return (fd == NULL) ? -1 : 0;
}
Ejemplo n.º 2
0
 /// \return false if !closed()
 void open(const boost::filesystem::path &path)
 {
     try {
         if (!closed()) {
             BOOST_THROW_EXCEPTION(OpenedError());
         }
         path_ = path;
         stream_ = openFd(path, errno_);
         checkError();
     } catch (std::exception &) {
         BOOST_THROW_EXCEPTION(OpenError() <<
                               enable_nested_current());
     }
 }
Ejemplo n.º 3
0
int GpIoPin::setValue(uint value){
#ifdef __ARMEL__
	if( _isOut && _currentValue != value ){
		if( 0 > _ioFd ) openFd();
		if( 0 > _ioFd ) return _ioFd;

		if( value ){
			write(_ioFd, "1", 2);
		}else{
			write(_ioFd, "0", 2);
		}

		_currentValue = value;
	}
#endif

	return 0;
}
Ejemplo n.º 4
0
int GpIoPin::getValue(uint* value){
#ifdef __ARMEL__
	if( !_isOut ){
		if( 0 > _ioFd ) openFd();
		if( 0 > _ioFd ) return _ioFd;

		char ch;

		read(_ioFd, &ch, 1);

		if( ch != '0' ){
			*value = 1;
		}else{
			*value = 0;
		}

		_currentValue = *value;
	}
#endif

	return 0;
}