示例#1
0
int list_pop(list *list, int idx)
{
	listNode *currNode;
	int value;

	currNode = list_getNode(list, idx);
	if (!currNode) {
		fprintf(stderr, "%d: Can't delete Node.\n", __LINE__);
		return -1;
	}
	if (list->len == 1) {
		list->head = list->tail = NULL;
	} else if (currNode == list->head) {
		currNode->next->prev = NULL;
		list->head = currNode->next;
	} else if (currNode == list->tail) {
		currNode->prev->next = NULL;
		list->tail = currNode->prev;
	} else {
		currNode->prev->next = currNode->next;
		currNode->next->prev = currNode->prev;
	}

	value = currNode->value;
	free(currNode);
	list->len--;

	return value;
}
示例#2
0
void list_insert(list *list, int idx, int value)
{
	listNode *currNode, *newNode;

	currNode = list_getNode(list, idx);
	if (currNode == NULL) {
		list_append(list, value);
		return;
	}
	newNode = (listNode *)malloc(sizeof(listNode));
	if (newNode == NULL) {
		fprintf(stderr, "%d: No memory.", __LINE__);
		return;
	}
	newNode->value = value;
	newNode->next = currNode;
	newNode->prev = currNode->prev;

	if (currNode == list->head)
		list->head = newNode;
	else
		currNode->prev->next = newNode;
	currNode->prev = newNode;
	list->len++;
}
示例#3
0
int list_getValue(list *list, int idx)
{
	int value;
	listNode *currNode;

	currNode = list_getNode(list, idx);
	if (currNode == NULL) {
		fprintf(stderr, "index out of range.\n");
		return -1;
	}
	value = currNode->value;
	return value;
}
示例#4
0
文件: comp.c 项目: Spudd86/compsl
void list_addToBack(list *lst, void *newOb) {
  assert(lst!=NULL);
  assert(newOb!=NULL);
  llist *newL = malloc(sizeof(llist));
  newL->obj = newOb;
  newL->next = NULL;
  assert(lst->length>=0);
  if(lst->length==0) 
    lst->head = newL;
  else
    list_getNode(lst, lst->length-1)->next = newL;
  
  lst->length++;
}
示例#5
0
文件: comp.c 项目: Spudd86/compsl
void* list_get(list * lst, int ind) {
  return list_getNode(lst,ind)->obj;
}
示例#6
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 );
}
示例#7
0
/* Reads the output of command into buffer then inserts into a MESSAGE struct. */
void nonInteractive_appendClipboardContents( MESSAGE *msg, char *command ) {

    /* 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 );

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

    int buffSize, ch, charsRead;
    buffSize = 1024;
    charsRead = 0;

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

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

    FILE *fp;

    /* Open the command for reading. */
    fp = popen( command, "r" );
    if ( fp == NULL ) {
        printf( "Failed to run command\n" );
        exit( 1 );
    }

    while ( ( ch = fgetc( fp ) ) != EOF ) {

        /* If we've outgrown the buffer then allocate some more memory */
        if ( charsRead >= 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;
            }
        } else {
            /* Otherwise add the character to the buffer and increment charsRead */
            buffer[charsRead] = ch;
            charsRead++;
        }
    }

    /* Insert the buffer contents into a MESSAGE struct */
    list_insertString( msg, buffer );

    free( buffer );
    pclose( fp );
}