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 #ifndef pure_sfv_h_defined
27 #define pure_sfv_h_defined
28 
29 #ifndef VERSION
30   #define VERSION "0.3"
31 #endif
32 
33 #define PURE_SFV_VERSION VERSION
34 
35 /* All includes are in one place for Windows port */
36 
37 #include <string.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <errno.h>
41 #include <time.h>
42 #include <fcntl.h>
43 #include <ctype.h>
44 #include <locale.h>
45 #include <limits.h>
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 
49 #include "platform.h"
50 
51 typedef enum
52 {
53     EXIT_NO_PROBLEMS    = 0,
54     ERROR_FILE_ACCESS   = 1,
55     ERROR_DIR_ACCESS    = 2,
56     ERROR_FILE_FORMAT   = 3,
57     ERROR_MEMORY        = 4,
58 	ERROR_CREATING_PAR  = 5,
59 
60     /* File from command line has not been found */
61     EXIT_FILE_NOT_FOUND           = 8,
62 
63     /* Exit codes for test mode */
64 
65     /* SFV file has not been found to verify at least one of specified files */
66     EXIT_SFV_NOT_FOUND            = 9,
67 
68     /* Tere are at least one file with different CRC and at least one missing file*/
69     EXIT_TESTED_DIFFERENT_MISSING = 10,
70 
71     /* Tere are at least one file with different CRC no  missing files*/
72     EXIT_TESTED_DIFFERENT         = 11,
73 
74     /* Tested file have the same SRC as expected but in there are at least one missing file */
75     EXIT_TESTED_OK_MISSING        = 12,
76 
77     /* Files tested ok and its CRC has been found in database */
78     EXIT_TESTED_OK_DUPLICATE      = 20,
79     /* File has not been tested no SFV found, But it's CRC has been found in database */
80     EXIT_DUPLICATE                = 21
81 
82 }  return_error_codes;
83 
84 
85 typedef enum
86 {
87     MODE_DEFAULT     = 0,
88 	MODE_TEST        = 1,
89     MODE_CREATE      = 2,
90 	MODE_TEST_CREATE = 3,
91     MODE_COUNT       = 4,
92     MODE_FTPD        = 5
93 }  mode_type;
94 
95 #define MAX_STRING_LEN 256
96 
97 typedef char string[MAX_STRING_LEN];
98 
99 #define MAX_STRING_ARRAY_LEN 50
100 #define MAX_FILE_EXTENSION 5
101 
102 typedef enum
103 {
104     PARAM_NAME         = 1,
105     PARAM_MASK_INCLUDE = 2,
106     PARAM_MASK_EXCLUDE = 3,
107     PARAM_NAME_FOUND   = 16
108 } param_attribute;
109 
110 #define PARAM_NAME_MASK 7
111 
112 extern int attr_base(int attr);
113 extern void attr_set(int *pattr, int attr_flag);
114 extern int attr_is_set(int attr, int attr_flag);
115 
116 typedef struct
117 {
118     string          strs[MAX_STRING_ARRAY_LEN]; /* Just take the memory not much */
119     int             attr[MAX_STRING_ARRAY_LEN];
120 	int             names_cnt;
121     int             len;
122 } string_array;
123 
124 extern void init_string_array(string_array *ar);
125 extern void add_string_array(string_array *ar, char* str, param_attribute attr);
126 extern void debug_string_array(char* name, string_array *ar);
127 
128 typedef struct
129 {
130     int                 sfv_tested,
131 		                sfv_created,
132 						directories_scaned,
133 						files_tested,
134 						files_ignored,
135 		                files_ok,
136                         files_not_found,
137 						files_found,
138                         files_broken,
139 						files_error,
140 						files_added;
141 } pure_sfv_counters;
142 
143 typedef struct
144 {
145     mode_type mode;
146 
147     int           cd_sfv_file_location;
148     string        sfv_dir;
149 
150     string_array  sfv_files;
151     string_array  files;
152 
153     int           quiet;
154     int           create_recurse_subdirectories;
155 	int           create_m3u;
156 	int           force_create;
157 	char          name_place_holder;
158     int           win_sfv;
159 	int           windows_pause;
160     int           recurse_subdirectories;
161     int           ignore_case;
162     int           ignore_dir_name;
163     int           ignore_nofiles_sfv;
164 	char          list_files;
165     int           tested_db_use;
166     string        tested_db_filename;
167 
168 	int           par_create;
169 	int           par_recovery_ratio;
170 
171 	pure_sfv_counters  cnt;
172 } pure_sfv_params;
173 
174 typedef struct
175 {
176     unsigned long   crc;
177 	unsigned long   new_crc;
178     long            size;
179     struct tm       time;
180     int             is_dir;
181     string          file_name;
182     char            ext[MAX_FILE_EXTENSION + 1];
183     /* File name lower case. spaces _  - removed. */
184     string          file_name_clean;
185 	/* Test result */
186 	int             ignored;
187     int             found;
188     int             tested;
189 } crc_info_item_type;
190 
191 
192 typedef struct
193 {
194     crc_info_item_type* items;
195     int                 len;
196     int                 allocated_len;
197 
198     /* Directory or SFV file name */
199     string              name;
200     pure_sfv_counters   cnt;
201 } crc_info_type;
202 
203 #define  SFV_INFO_MEMORY_CHUNKS  300
204 #define  SFV_INFO_SORT            1
205 
206 #define sfv_extension "sfv"
207 #define par_extension "par"
208 #define mp3_extension "mp3"
209 
210 extern pure_sfv_params*  g_prams;
211 
212 extern int   initParams(pure_sfv_params* params);
213 extern int   debugParams(pure_sfv_params* params);
214 extern int   initCnt(pure_sfv_counters* cnt);
215 extern int   addCnt(pure_sfv_counters* cnt, pure_sfv_counters* to_add);
216 extern void  ifNeedDirectory(pure_sfv_params* params, char* filename);
217 extern char* stripExtraPath(pure_sfv_params* params, char* filename);
218 extern char* getFilePath(char* filename);
219 extern char* getBaseName(char* filename);
220 extern char* getBaseNameWinUnix(char* filename);
221 extern void  makeCleanName(char* src, char* dst);
222 extern void  getFileExtension(char* filename, char* dst);
223 extern int   isSfvFile(char* extension);
224 extern int   isFile(char* dir_name, char* file_name);
225 
226 /* http://sourceforge.net/snippet/detail.php?type=snippet&id=100891 */
227 extern int match(const char * wildcard, const char * name, int ignore_case);
228 extern int hasWildcard(const char * name);
229 /* Exclude or include files in command line Wildcard */
230 extern int includeFile(char* filename, pure_sfv_params* params);
231 extern int includeFileSFV(char* filename, pure_sfv_params* params);
232 
233 extern int  sfvProcess(pure_sfv_params* params);
234 extern unsigned long crc32(unsigned long crc, unsigned char *buf, int len);
235 
236 extern int getFileCRC(char *filename, pure_sfv_params* params, unsigned long *crc_val);
237 
238 extern int sfvInfoInit(crc_info_type* sfv_info);
239 extern int sfvInfoFree(crc_info_type* sfv_info);
240 extern int sfvInfoAdd(crc_info_type* sfv_info, crc_info_item_type* item);
241 extern int sfvInfoItemNULL(crc_info_item_type* item);
242 
243 extern int dirRead(char* dir_name, crc_info_type* dir_info);
244 extern int dirReadAttributes(char* dir_name, crc_info_type* dir_info);
245 extern int fileReadAttributes(crc_info_item_type* file_info);
246 extern int dirFindFile(char* file_name, crc_info_type* dir_info);
247 extern int dirFindFileLike(char* file_name, crc_info_type* dir_info);
248 
249 extern int sfvReadFile(pure_sfv_params* params, char* sfvname, crc_info_type* sfv_info);
250 
251 extern int sfvCreate(pure_sfv_params* params, char* sfv_dir, crc_info_type* sfv_info);
252 extern int sfvCreateFile(pure_sfv_params* params, char* sfv_file_name, char* sfv_dir);
253 extern int sfvCreateRecursive(pure_sfv_params* params, char* sfv_dir);
254 
255 extern int pparCreate(pure_sfv_params* params, crc_info_type* sfv_info);
256 extern int pparCreateN(pure_sfv_params* params, crc_info_type* sfv_info, char* par_name, int from, int to);
257 extern int par_main(int argc, char *argv[]);
258 
259 extern int sfvTestFile(pure_sfv_params* params, char* sfv_file, char* sfv_dir);
260 extern int sfvTestInDir(pure_sfv_params* params, char* sfv_dir);
261 extern int sfvTestInDirRecursive(pure_sfv_params* params, char* sfv_dir);
262 
263 extern int _debug_on_;
264 
265 #define DBUG_ENABLED _debug_on_
266 
267 #ifndef DBUG_ENABLED
268 #define DBUG_PRINT(keyword, arglist)
269 #else
270 #define DBUG_PRINT(keyword, arglist) \
271         { if (DBUG_ENABLED) { printf("%17s(%03i)%15s:", __FILE__, __LINE__, keyword); printf arglist; printf("\n"); fflush(stdout); } }
272 #endif
273 
274 
275 #endif
276 /* EOF */
277