Advertisement
Omnikron13

libdata

Oct 11th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.78 KB | None | 0 0
  1. /*
  2.   Copyright (c) 2008, 2009, 2010 Joseph Sabey
  3.  
  4.   Permission is hereby granted, free of charge, to any person
  5.   obtaining a copy of this software and associated documentation
  6.   files (the "Software"), to deal in the Software without
  7.   restriction, including without limitation the rights to use,
  8.   copy, modify, merge, publish, distribute, sublicense, and/or sell
  9.   copies of the Software, and to permit persons to whom the
  10.   Software is furnished to do so, subject to the following
  11.   conditions:
  12.  
  13.   The above copyright notice and this permission notice shall be
  14.   included in all copies or substantial portions of the Software.
  15.  
  16.   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17.   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18.   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19.   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20.   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21.   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22.   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23.   OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25.  
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include "libdata.h"
  30.  
  31. DP data_add(DP *first, char *type)
  32. {
  33.     if(type == NULL || strlen(type) < 1)
  34.         return NULL;
  35.     DP *current = first;
  36.     while(*current != NULL)
  37.     {
  38.         current = &((*current)->next);
  39.     }
  40.     *current = malloc(sizeof(struct data));
  41.     if(*current == NULL)
  42.         return NULL;
  43.     (*current)->type = malloc(strlen(type)+1);
  44.     if((*current)->type == NULL)
  45.         return NULL;
  46.     strcpy((*current)->type, type);
  47.     (*current)->data = NULL;
  48.     (*current)->next = NULL;
  49.     (*current)->dispose = NULL;
  50.     return *current;
  51. }
  52.  
  53. DP data_add_unique(DP *first, char *type)
  54. {
  55.     DP *current = first;
  56.     while(*current != NULL)
  57.     {
  58.         if(strcmp((*current)->type, type) == 0)
  59.             return NULL;
  60.         current = &((*current)->next);
  61.     }
  62.     *current = malloc(sizeof(struct data));
  63.     if(*current == NULL)
  64.         return NULL;
  65.     (*current)->type = malloc(strlen(type)+1);
  66.     if((*current)->type == NULL)
  67.         return NULL;
  68.     strcpy((*current)->type, type);
  69.     (*current)->data = NULL;
  70.     (*current)->next = NULL;
  71.     (*current)->dispose = NULL;
  72.     return *current;
  73. }
  74.  
  75. DP data_add_string(DP *first, char *type, char *data)
  76. {
  77.     DP new = NULL;
  78.     new = data_add(first, type);
  79.     if(new == NULL)
  80.         return NULL;
  81.     new->data = malloc(strlen(data)+1);
  82.     if(new->data == NULL)
  83.         return NULL;
  84.     strcpy((char*)new->data, data);
  85.     return new;
  86. }
  87.  
  88. DP data_add_string_unique(DP *first, char *type, char *data)
  89. {
  90.     DP new = NULL;
  91.     new = data_add_unique(first, type);
  92.     if(new == NULL)
  93.         return NULL;
  94.     new->data = malloc(strlen(data)+1);
  95.     if(new->data == NULL)
  96.         return NULL;
  97.     strcpy(new->data, data);
  98.     return new;
  99. }
  100.  
  101. DP data_add_int(DP *first, char *type, int data)
  102. {
  103.     DP new = NULL;
  104.     new = data_add(first, type);
  105.     if(new == NULL)
  106.         return NULL;
  107.     new->data = malloc(sizeof(int));
  108.     if(new->data == NULL)
  109.         return NULL;
  110.     *((int*)new->data) = data;
  111.     return new;
  112. }
  113.  
  114. DP data_add_int_unique(DP *first, char *type, int data)
  115. {
  116.     DP new = NULL;
  117.     new = data_add_unique(first, type);
  118.     if(new == NULL)
  119.         return NULL;
  120.     new->data = malloc(sizeof(int));
  121.     if(new->data == NULL)
  122.         return NULL;
  123.     *((int*)new->data) = data;
  124.     return new;
  125. }
  126.  
  127. int data_splice(DP *source, DP *target, int offset)
  128. {
  129.     DP buffer = NULL;
  130.     buffer = *source;
  131.     DP current = buffer;
  132.     int x = 0;
  133.     while(x < offset)
  134.     {
  135.         current = current->next;
  136.         x++;
  137.     }
  138.     *source = current->next;
  139.     current->next = *target;
  140.     *target = buffer;
  141.     return 0;
  142. }
  143.  
  144. int data_splice_type(DP *source, DP *target, char *type)
  145. {
  146.     DP *buffer = NULL;
  147.     while(1)
  148.     {
  149.         buffer = data_get_type(source, type, 0);
  150.         if(buffer == NULL)
  151.             return 0;
  152.         data_splice(buffer, target, 0);
  153.     }
  154. }
  155.  
  156. int data_count_type(DP first, char *type)
  157. {
  158.     int count = 0;
  159.     DP current = first;
  160.     while(current != NULL)
  161.     {
  162.         if(strcmp(current->type, type) == 0)
  163.             count++;
  164.         current = current->next;
  165.     }
  166.     return count;
  167. }
  168.  
  169. int data_count_all(DP first)
  170. {
  171.     int count = 0;
  172.     DP current = first;
  173.     while(current != NULL)
  174.     {
  175.         count++;
  176.         current = current->next;
  177.     }
  178.     return count;
  179. }
  180.  
  181. DP *data_get_type(DP *first, char *type, int offset)
  182. {
  183.     int count = 0;
  184.     DP *current = first;
  185.     while(*current != NULL)
  186.     {
  187.         if(strcmp((*current)->type, type) == 0)
  188.             if(count == offset)
  189.             {
  190.                 return current;
  191.             }
  192.             else
  193.             {
  194.                 count++;
  195.             }
  196.         current = &((*current)->next);
  197.     }
  198.     return NULL;
  199. }
  200.  
  201. int data_sort_insertion(DP *first, int(*compare)(DP first, DP second))
  202. {
  203.     if(first == NULL)
  204.         return 1;
  205.     if(*first == NULL)
  206.         return 2;
  207.     if(compare == NULL)
  208.         return 3;
  209.     int position = -1;
  210.     while(1)
  211.     {
  212.         if(position == -1)
  213.         {
  214.             DP *current = first;
  215.             DP *next = &((*first)->next);
  216.             while(1)
  217.             {
  218.                 int swap = compare(*current, *next);
  219.                 if(swap > 0)
  220.                     return 4;
  221.                 if(swap == -1)
  222.                 {
  223.                     DP buffer = *current;
  224.                     *current = (*current)->next;
  225.                     buffer->next = (buffer->next)->next;
  226.                     (*current)->next = buffer;
  227.                     break;
  228.                 }
  229.                 current = next;
  230.                 next = &((*next)->next);
  231.                 if(*next == NULL)
  232.                     return 0;
  233.                 position++;
  234.             }
  235.         }
  236.         else
  237.         {
  238.             while(position > -1)
  239.             {
  240.                 DP *current = first;
  241.                 DP *next = &((*first)->next);
  242.                 int x = 0;
  243.                 while(x < position)
  244.                 {
  245.                     current = next;
  246.                     next = &((*next)->next);
  247.                     x++;
  248.                 }
  249.                 if(compare(*current, *next) == -1)
  250.                 {
  251.                     DP buffer = *current;
  252.                     *current = (*current)->next;
  253.                     buffer->next = (buffer->next)->next;
  254.                     (*current)->next = buffer;
  255.                     position--;
  256.                 }
  257.                 else
  258.                     position = -1;
  259.             }
  260.         }
  261.     }
  262.     return 0;
  263. }
  264.  
  265. int data_compare_data_int_low(DP first, DP second)
  266. {
  267.     if(first == NULL || second == NULL)
  268.         return 1;
  269.     if(first->data == NULL || second->data == NULL)
  270.         return 2;
  271.     if((*((int*)first->data)) <= (*((int*)second->data)))
  272.         return 0;
  273.     else
  274.         return -1;
  275. }
  276.  
  277. int data_remove(DP *first, char *type, int offset)
  278. {
  279.     if(type == NULL || strlen(type) < 1)
  280.         return 1;
  281.     if(offset < 0)
  282.         return 2;
  283.     int count = 0;
  284.     DP *current = first;
  285.     DP *previous = current;
  286.     while(*current != NULL)
  287.     {
  288.         if(strcmp((*current)->type, type) == 0)
  289.         {
  290.             if(count == offset)
  291.             {
  292.                 DP next = (*current)->next;
  293.                 (*current)->dispose(*current);
  294.                 free(*current);
  295.                 (*previous)->next = next;
  296.                 return 0;
  297.             }
  298.             count++;
  299.         }
  300.         previous = current;
  301.         current = &((*current)->next);
  302.     }
  303.     return 3;
  304. }
  305.  
  306. int data_dispose(DP data)
  307. {
  308.     //if(data != NULL)
  309.     //  printf("Disposing: %s | ", (char*)data->type);
  310.     if(data == NULL)
  311.         return 0;
  312.     if(data->next != NULL)
  313.         data_dispose(data->next);
  314.     free(data->type);
  315.     if(data->dispose != NULL)
  316.         data->dispose(data);
  317.     else
  318.         free(data->data);
  319.     free(data);
  320.     return 0;
  321. }
  322.  
  323. int data_dispose_data(DP data)
  324. {
  325.     data_dispose((DP)data->data);
  326.     return 0;
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement