Ejemplo n.º 1
0
/* Inserts a line before nodeNum */
void line_insertBefore( MESSAGE *msg, int nodeNum, char *str ) {
    LINE *oldLine, *newLine;
    oldLine = NULL;
    if ( ( oldLine = line_getLineNode( msg, nodeNum ) ) == NULL ) {
        fprintf( stderr, "Unable to retrieve nodeNum %d\n", nodeNum );
        return;
    } else {
        newLine = line_getLine();

        /* Add the new line */
        int strLen = strlen( str );
        newLine->text = malloc( strLen );
        memmove( newLine->text, str, strLen );
        newLine->text[strLen] = '\0';

        /* Update pointers */
        newLine->prev = oldLine->prev;
        newLine->next = oldLine;

        /* if oldLine->prev we are inserting at the beginning */
        if ( !oldLine->prev ) {
            msg->first = newLine;
        } else {
            /* Otherwise insert as normal */
            oldLine->prev->next = newLine;
            oldLine->prev = newLine;
        }

        /* Update statistics */
        newLine->lSize = strLen;
        msg->numChars += newLine->lSize + 1;
        msg->numLines++;
    }
}
Ejemplo n.º 2
0
/* Inserts a line after nodeNum */
void line_insertAfter( MESSAGE *msg, int nodeNum, char *str ) {

    LINE *oldLine = NULL;

    if ( ( oldLine = line_getLineNode( msg, nodeNum ) ) == NULL ) {
        fprintf( stderr, "Unable to retrieve nodeNum %d\n", nodeNum );
        return;
    } else {

        LINE *newLine = line_getLine();

        /* Add the new line */
        int strLen = strlen( str );
        newLine->text = malloc( strLen );
        memmove( newLine->text, str, strLen );
        newLine->text[strLen] = '\0';

        /* Update pointers */
        newLine->next = oldLine->next;
        oldLine->next = newLine;
        newLine->prev = oldLine;
        newLine->next->prev = newLine;

        /* Update statistics */
        newLine->lSize = strLen;
        msg->numChars += newLine->lSize + 1;
        msg->numLines++;
    }
}
Ejemplo n.º 3
0
/* Reads from stdin until EOF growing the buffer as needed */
void nonInteractive_appendMessage( MESSAGE *msg ) {

    /* Loop to the end of the message */
    msg = msg->root;
    for ( ; msg->next; msg = msg->next )
        ;

    /* Allocate and move to new node */
    msg->next = list_getNode( msg );
    msg = msg->next;

    /* Get and set path and time information */
    list_setPath( msg );
    list_setTime( msg );

    int ch, lineLen, totChars, numLines, buffSize;
    ch = lineLen = totChars = numLines = 0;

    char *tmp, *buffer;
    tmp = buffer = NULL;

    buffSize = 1024;
    buffer = malloc( buffSize * sizeof(char) );

    if ( !buffer ) {
        fprintf( stderr,
                "Failed to allocate memory for message buffer in nonInteractive_appendMessage\n" );
        exit( 1 );
    }

    LINE *line, *prev;
    line = prev = NULL;
    line = line_getLine();

    while ( ( ch = getchar() ) != EOF ) {

        /* If we've outgrown the buffer then allocate some more memory */
        if ( lineLen >= buffSize ) {
            buffSize *= 2;
            tmp = realloc( buffer, buffSize * sizeof(char *) );

            if ( !tmp ) {
                fprintf( stderr,
                        "Failed to allocate %d bytes in nonInteractive_appendMessage\n",
                        buffSize );
                exit( 1 );
            } else {
                buffer = tmp;
            }
        }

        if ( ch != '\n' && ch != '\0' ) {
            buffer[lineLen] = ch;
            lineLen++;
        } else {

            numLines++;
            buffer[lineLen] = 0;

            /* You pass buffer + lineLen because insertLine expects the pointer to be at the end of the string */
            insertLine( &line, buffer + lineLen, lineLen, numLines );

            if ( numLines == 1 ) {
                msg->first = line;
            }

            /* Set and update the prev pointer */
            line->prev = prev;
            prev = line;

            /* Get a new line and move to it */
            line->next = line_getLine();
            line = line->next;
            totChars += lineLen + 1;
            lineLen = 0;
        }
    }

    /* Update MESSAGE statistics for this message */
    msg->last = line->prev;
    msg->numChars = totChars;
    msg->numLines = numLines;
    msg->messageNum = msg->root->totalMessages + 1;
    msg->root->totalMessages++;
    free( buffer );
}