1 /*********************************************************************
2  *
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * ===================================================================
15  * Revision History ::
16  * YYYY.MM.DD   Change ID       Developer
17  *              Description
18  * -------------------------------------------------------------------
19  * 2002.04.25                   Vlad Skarzhevskyy
20  *              Initial implementation.
21  *
22  * ===================================================================
23  *
24  ********************************************************************/
25 
26 #include "pure-sfv.h"
27 
sfvInfoInit(crc_info_type * crc_info)28 int sfvInfoInit(crc_info_type* crc_info)
29 {
30     memset(crc_info, 0, sizeof(crc_info_type));
31     crc_info->len = 0;
32     crc_info->allocated_len = 0;
33     crc_info->items = NULL;
34     initCnt(&(crc_info->cnt));
35     return 0;
36 }
37 
sfvInfoFree(crc_info_type * crc_info)38 int sfvInfoFree(crc_info_type* crc_info)
39 {
40     crc_info->len = 0;
41     crc_info->allocated_len = 0;
42     free(crc_info->items);
43     return 0;
44 }
45 
sfvInfoItemNULL(crc_info_item_type * item)46 int sfvInfoItemNULL(crc_info_item_type* item)
47 {
48     memset(item, 0, sizeof(crc_info_item_type));
49     return 0;
50 }
51 
sfvInfoAdd(crc_info_type * crc_info,crc_info_item_type * item)52 int sfvInfoAdd(crc_info_type* crc_info, crc_info_item_type* item)
53 {
54     int do_sort = 0;
55     int insert_position;
56     int idx;
57 
58     if (crc_info->items == NULL)
59     {
60         crc_info->items = (crc_info_item_type *) calloc(SFV_INFO_MEMORY_CHUNKS, sizeof(crc_info_item_type));
61         if ((crc_info->items) == NULL)
62         {
63             fprintf(stderr, "sfv: Error in memory allocation\n");
64             return ERROR_MEMORY;
65         }
66         memset(crc_info->items, 0, SFV_INFO_MEMORY_CHUNKS * sizeof(crc_info_item_type));
67         crc_info->allocated_len = SFV_INFO_MEMORY_CHUNKS;
68         crc_info->len = 0;
69     }
70     if (crc_info->allocated_len == crc_info->len + 1)
71     {
72         DBUG_PRINT("sfvInfoAdd", ("Reallocate the list of pointers"));
73         crc_info->items = (crc_info_item_type*) realloc((crc_info->items),
74                               (crc_info->allocated_len + SFV_INFO_MEMORY_CHUNKS) * sizeof(crc_info_item_type));
75         if ((crc_info->items) == NULL)
76         {
77             fprintf(stderr, "sfv: Error in memory allocation\n");
78             return ERROR_MEMORY;
79         }
80         memset(&(crc_info->items[crc_info->len + 1]), 0, SFV_INFO_MEMORY_CHUNKS * sizeof(crc_info_item_type));
81         crc_info->allocated_len = crc_info->allocated_len + SFV_INFO_MEMORY_CHUNKS;
82     }
83 
84     makeCleanName(item->file_name, item->file_name_clean);
85     getFileExtension(item->file_name, item->ext);
86 
87 #ifdef SFV_INFO_SORT
88     do_sort = 1;
89 #endif
90 
91     if (!do_sort) {
92         memcpy(&(crc_info->items[crc_info->len]), item, sizeof(crc_info_item_type));
93         crc_info->len ++;
94     } else {
95         /* Sort by name */
96         insert_position = crc_info->len;
97         for (idx = 0; idx < crc_info->len; idx ++) {
98             if (strcmp(crc_info->items[idx].file_name_clean, item->file_name_clean) > 0) {
99                 insert_position = idx;
100                 break;
101             }
102         }
103         if (insert_position == crc_info->len) {
104             memcpy(&(crc_info->items[crc_info->len]), item, sizeof(crc_info_item_type));
105         } else {
106             for (idx = crc_info->len; idx > insert_position; idx --) {
107                 memcpy(&(crc_info->items[idx]), &(crc_info->items[idx-1]), sizeof(crc_info_item_type));
108             }
109             memcpy(&(crc_info->items[insert_position]), item, sizeof(crc_info_item_type));
110         }
111         crc_info->len ++;
112     }
113     return 0;
114 }
115 
116 /* EOF */
117 
118