Ejemplo n.º 1
0
std::string join(tstring const& p1, tstring const& p2, tstring const& p3, tstring const& p4)
{
    std::string result;

    tstring s1 = remove_duplicate_slashes_at_ends(p1);
    tstring s2 = remove_duplicate_slashes_at_ends(p2);
    tstring s3 = remove_duplicate_slashes_at_ends(p3);
    tstring s4 = remove_duplicate_slashes_at_ends(p4);

    // calculate length
    {
        size_t result_size = 0;
	bool    need_sep_flag = false;
	calc_join_length(s1, result_size, need_sep_flag);
	calc_join_length(s2, result_size, need_sep_flag);
	calc_join_length(s3, result_size, need_sep_flag);
	calc_join_length(s4, result_size, need_sep_flag);
	result.reserve(result_size+1);
    }
    {
        bool separator_flag = false;
        join_append(result, s1, separator_flag);
        join_append(result, s2, separator_flag);
        join_append(result, s3, separator_flag);
        join_append(result, s4, separator_flag);
    }
    return result;
}
Ejemplo n.º 2
0
static PyObject *
csv_writerow(WriterObj *self, PyObject *seq)
{
    DialectObj *dialect = self->dialect;
    int len, i;

    if (!PySequence_Check(seq))
        return PyErr_Format(error_obj, "sequence expected");

    len = PySequence_Length(seq);
    if (len < 0)
        return NULL;

    /* Join all fields in internal buffer.
     */
    join_reset(self);
    for (i = 0; i < len; i++) {
        PyObject *field;
        int append_ok;
        int quoted;

        field = PySequence_GetItem(seq, i);
        if (field == NULL)
            return NULL;

        switch (dialect->quoting) {
        case QUOTE_NONNUMERIC:
            quoted = !PyNumber_Check(field);
            break;
        case QUOTE_ALL:
            quoted = 1;
            break;
        default:
            quoted = 0;
            break;
        }

        if (PyString_Check(field)) {
            append_ok = join_append(self,
                                    PyString_AS_STRING(field),
                                    &quoted, len == 1);
            Py_DECREF(field);
        }
        else if (field == Py_None) {
            append_ok = join_append(self, "", &quoted, len == 1);
            Py_DECREF(field);
        }
        else {
            PyObject *str;

            if (PyFloat_Check(field)) {
                str = PyObject_Repr(field);
            } else {
                str = PyObject_Str(field);
            }
            Py_DECREF(field);
            if (str == NULL)
                return NULL;

            append_ok = join_append(self, PyString_AS_STRING(str),
                                    &quoted, len == 1);
            Py_DECREF(str);
        }
        if (!append_ok)
            return NULL;
    }

    /* Add line terminator.
     */
    if (!join_append_lineterminator(self))
        return 0;

    return PyObject_CallFunction(self->writeline,
                                 "(s#)", self->rec, self->rec_len);
}
Ejemplo n.º 3
0
static PyObject *
csv_writerow(WriterObj *self, PyObject *seq)
{
    DialectObj *dialect = self->dialect;
    Py_ssize_t len, i;
    PyObject *line, *result;

    if (!PySequence_Check(seq))
        return PyErr_Format(error_obj, "sequence expected");

    len = PySequence_Length(seq);
    if (len < 0)
        return NULL;

    /* Join all fields in internal buffer.
     */
    join_reset(self);
    for (i = 0; i < len; i++) {
        PyObject *field;
        int append_ok;
        int quoted;

        field = PySequence_GetItem(seq, i);
        if (field == NULL)
            return NULL;

        switch (dialect->quoting) {
        case QUOTE_NONNUMERIC:
            quoted = !PyNumber_Check(field);
            break;
        case QUOTE_ALL:
            quoted = 1;
            break;
        default:
            quoted = 0;
            break;
        }

        if (PyUnicode_Check(field)) {
            append_ok = join_append(self, field, &quoted, len == 1);
            Py_DECREF(field);
        }
        else if (field == Py_None) {
            append_ok = join_append(self, NULL, &quoted, len == 1);
            Py_DECREF(field);
        }
        else {
            PyObject *str;

            str = PyObject_Str(field);
            Py_DECREF(field);
            if (str == NULL)
                return NULL;
            append_ok = join_append(self, str, &quoted, len == 1);
            Py_DECREF(str);
        }
        if (!append_ok)
            return NULL;
    }

    /* Add line terminator.
     */
    if (!join_append_lineterminator(self))
        return 0;

    line = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
                                     (void *) self->rec, self->rec_len);
    if (line == NULL)
        return NULL;
    result = PyObject_CallFunctionObjArgs(self->writeline, line, NULL);
    Py_DECREF(line);
    return result;
}
Ejemplo n.º 4
0
Archivo: _csv.c Proyecto: 1st1/cpython
static PyObject *
csv_writerow(WriterObj *self, PyObject *seq)
{
    DialectObj *dialect = self->dialect;
    PyObject *iter, *field, *line, *result;

    iter = PyObject_GetIter(seq);
    if (iter == NULL)
        return PyErr_Format(_csvstate_global->error_obj,
                            "iterable expected, not %.200s",
                            seq->ob_type->tp_name);

    /* Join all fields in internal buffer.
     */
    join_reset(self);
    while ((field = PyIter_Next(iter))) {
        int append_ok;
        int quoted;

        switch (dialect->quoting) {
        case QUOTE_NONNUMERIC:
            quoted = !PyNumber_Check(field);
            break;
        case QUOTE_ALL:
            quoted = 1;
            break;
        default:
            quoted = 0;
            break;
        }

        if (PyUnicode_Check(field)) {
            append_ok = join_append(self, field, quoted);
            Py_DECREF(field);
        }
        else if (field == Py_None) {
            append_ok = join_append(self, NULL, quoted);
            Py_DECREF(field);
        }
        else {
            PyObject *str;

            str = PyObject_Str(field);
            Py_DECREF(field);
            if (str == NULL) {
                Py_DECREF(iter);
                return NULL;
            }
            append_ok = join_append(self, str, quoted);
            Py_DECREF(str);
        }
        if (!append_ok) {
            Py_DECREF(iter);
            return NULL;
        }
    }
    Py_DECREF(iter);
    if (PyErr_Occurred())
        return NULL;

    if (self->num_fields > 0 && self->rec_len == 0) {
        if (dialect->quoting == QUOTE_NONE) {
            PyErr_Format(_csvstate_global->error_obj,
                "single empty field record must be quoted");
            return NULL;
        }
        self->num_fields--;
        if (!join_append(self, NULL, 1))
            return NULL;
    }

    /* Add line terminator.
     */
    if (!join_append_lineterminator(self))
        return NULL;

    line = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
                                     (void *) self->rec, self->rec_len);
    if (line == NULL)
        return NULL;
    result = PyObject_CallFunctionObjArgs(self->writeline, line, NULL);
    Py_DECREF(line);
    return result;
}