Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Copyright (c) 2008, 2009, 2010 Joseph Sabey
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated documentation
- files (the "Software"), to deal in the Software without
- restriction, including without limitation the rights to use,
- copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following
- conditions:
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include "libdata.h"
- DP data_add(DP *first, char *type)
- {
- if(type == NULL || strlen(type) < 1)
- return NULL;
- DP *current = first;
- while(*current != NULL)
- {
- current = &((*current)->next);
- }
- *current = malloc(sizeof(struct data));
- if(*current == NULL)
- return NULL;
- (*current)->type = malloc(strlen(type)+1);
- if((*current)->type == NULL)
- return NULL;
- strcpy((*current)->type, type);
- (*current)->data = NULL;
- (*current)->next = NULL;
- (*current)->dispose = NULL;
- return *current;
- }
- DP data_add_unique(DP *first, char *type)
- {
- DP *current = first;
- while(*current != NULL)
- {
- if(strcmp((*current)->type, type) == 0)
- return NULL;
- current = &((*current)->next);
- }
- *current = malloc(sizeof(struct data));
- if(*current == NULL)
- return NULL;
- (*current)->type = malloc(strlen(type)+1);
- if((*current)->type == NULL)
- return NULL;
- strcpy((*current)->type, type);
- (*current)->data = NULL;
- (*current)->next = NULL;
- (*current)->dispose = NULL;
- return *current;
- }
- DP data_add_string(DP *first, char *type, char *data)
- {
- DP new = NULL;
- new = data_add(first, type);
- if(new == NULL)
- return NULL;
- new->data = malloc(strlen(data)+1);
- if(new->data == NULL)
- return NULL;
- strcpy((char*)new->data, data);
- return new;
- }
- DP data_add_string_unique(DP *first, char *type, char *data)
- {
- DP new = NULL;
- new = data_add_unique(first, type);
- if(new == NULL)
- return NULL;
- new->data = malloc(strlen(data)+1);
- if(new->data == NULL)
- return NULL;
- strcpy(new->data, data);
- return new;
- }
- DP data_add_int(DP *first, char *type, int data)
- {
- DP new = NULL;
- new = data_add(first, type);
- if(new == NULL)
- return NULL;
- new->data = malloc(sizeof(int));
- if(new->data == NULL)
- return NULL;
- *((int*)new->data) = data;
- return new;
- }
- DP data_add_int_unique(DP *first, char *type, int data)
- {
- DP new = NULL;
- new = data_add_unique(first, type);
- if(new == NULL)
- return NULL;
- new->data = malloc(sizeof(int));
- if(new->data == NULL)
- return NULL;
- *((int*)new->data) = data;
- return new;
- }
- int data_splice(DP *source, DP *target, int offset)
- {
- DP buffer = NULL;
- buffer = *source;
- DP current = buffer;
- int x = 0;
- while(x < offset)
- {
- current = current->next;
- x++;
- }
- *source = current->next;
- current->next = *target;
- *target = buffer;
- return 0;
- }
- int data_splice_type(DP *source, DP *target, char *type)
- {
- DP *buffer = NULL;
- while(1)
- {
- buffer = data_get_type(source, type, 0);
- if(buffer == NULL)
- return 0;
- data_splice(buffer, target, 0);
- }
- }
- int data_count_type(DP first, char *type)
- {
- int count = 0;
- DP current = first;
- while(current != NULL)
- {
- if(strcmp(current->type, type) == 0)
- count++;
- current = current->next;
- }
- return count;
- }
- int data_count_all(DP first)
- {
- int count = 0;
- DP current = first;
- while(current != NULL)
- {
- count++;
- current = current->next;
- }
- return count;
- }
- DP *data_get_type(DP *first, char *type, int offset)
- {
- int count = 0;
- DP *current = first;
- while(*current != NULL)
- {
- if(strcmp((*current)->type, type) == 0)
- if(count == offset)
- {
- return current;
- }
- else
- {
- count++;
- }
- current = &((*current)->next);
- }
- return NULL;
- }
- int data_sort_insertion(DP *first, int(*compare)(DP first, DP second))
- {
- if(first == NULL)
- return 1;
- if(*first == NULL)
- return 2;
- if(compare == NULL)
- return 3;
- int position = -1;
- while(1)
- {
- if(position == -1)
- {
- DP *current = first;
- DP *next = &((*first)->next);
- while(1)
- {
- int swap = compare(*current, *next);
- if(swap > 0)
- return 4;
- if(swap == -1)
- {
- DP buffer = *current;
- *current = (*current)->next;
- buffer->next = (buffer->next)->next;
- (*current)->next = buffer;
- break;
- }
- current = next;
- next = &((*next)->next);
- if(*next == NULL)
- return 0;
- position++;
- }
- }
- else
- {
- while(position > -1)
- {
- DP *current = first;
- DP *next = &((*first)->next);
- int x = 0;
- while(x < position)
- {
- current = next;
- next = &((*next)->next);
- x++;
- }
- if(compare(*current, *next) == -1)
- {
- DP buffer = *current;
- *current = (*current)->next;
- buffer->next = (buffer->next)->next;
- (*current)->next = buffer;
- position--;
- }
- else
- position = -1;
- }
- }
- }
- return 0;
- }
- int data_compare_data_int_low(DP first, DP second)
- {
- if(first == NULL || second == NULL)
- return 1;
- if(first->data == NULL || second->data == NULL)
- return 2;
- if((*((int*)first->data)) <= (*((int*)second->data)))
- return 0;
- else
- return -1;
- }
- int data_remove(DP *first, char *type, int offset)
- {
- if(type == NULL || strlen(type) < 1)
- return 1;
- if(offset < 0)
- return 2;
- int count = 0;
- DP *current = first;
- DP *previous = current;
- while(*current != NULL)
- {
- if(strcmp((*current)->type, type) == 0)
- {
- if(count == offset)
- {
- DP next = (*current)->next;
- (*current)->dispose(*current);
- free(*current);
- (*previous)->next = next;
- return 0;
- }
- count++;
- }
- previous = current;
- current = &((*current)->next);
- }
- return 3;
- }
- int data_dispose(DP data)
- {
- //if(data != NULL)
- // printf("Disposing: %s | ", (char*)data->type);
- if(data == NULL)
- return 0;
- if(data->next != NULL)
- data_dispose(data->next);
- free(data->type);
- if(data->dispose != NULL)
- data->dispose(data);
- else
- free(data->data);
- free(data);
- return 0;
- }
- int data_dispose_data(DP data)
- {
- data_dispose((DP)data->data);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement