coan 4.2.4
dataset.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2004, 2006 Symbian Software Ltd.                        *
00003  *   All rights reserved.                                                  *
00004  *   Copyright (C) 2007-2011 Mike Kinghan, imk@strudl.org                  *
00005  *   All rights reserved.                                                  *
00006  *                                                                         *
00007  *   Contributed originally by Mike Kinghan, imk@strudl.org                *
00008  *                                                                         *
00009  *   Redistribution and use in source and binary forms, with or without    *
00010  *   modification, are permitted provided that the following conditions    *
00011  *   are met:                                                              *
00012  *                                                                         *
00013  *   Redistributions of source code must retain the above copyright        *
00014  *   notice, this list of conditions and the following disclaimer.         *
00015  *                                                                         *
00016  *   Redistributions in binary form must reproduce the above copyright     *
00017  *   notice, this list of conditions and the following disclaimer in the   *
00018  *   documentation and/or other materials provided with the distribution.  *
00019  *                                                                         *
00020  *   Neither the name of Symbian Software Ltd. nor the names of its        *
00021  *   contributors may be used to endorse or promote products derived from  *
00022  *   this software without specific prior written permission.              *
00023  *                                                                         *
00024  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   *
00025  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     *
00026  *   LIMITED TO, THE IMPLIED WARRANTIES OF  MERCHANTABILITY AND FITNESS    *
00027  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE        *
00028  *   COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,   *
00029  *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  *
00030  *   BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
00031  *   OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    *
00032  *   AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,*
00033  *   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF *
00034  *   THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH  *
00035  *   DAMAGE.                                                               *
00036  *                                                                         *
00037  **************************************************************************/
00038 
00039 #include "dataset.h"
00040 #include "platform.h"
00041 #include "report.h"
00042 
00051 STATE_DEF(dataset)
00052 {
00054     INCLUDE_PUBLIC(dataset);
00055     heap_str filter_types;
00059 }
00060 STATE_T(dataset);
00061 
00062 
00063 IMPLEMENT(dataset,USER_INITABLE);
00064 
00065 DEFINE_USER_INIT(dataset)(STATE_T(dataset) * dataset_st)
00066 {
00067     dataset_st->dataset_public_state.file_tree = file_tree_new();
00068 }
00069 
00070 DEFINE_USER_FINIS(dataset)(STATE_T(dataset) * dataset_st)
00071 {
00072     file_tree_dispose((SET_PUBLIC(dataset,file_tree)));
00073     SET_PUBLIC(dataset,file_tree) = NULL;
00074     free(SET_STATE(dataset,filter_types));
00075 }
00080 
00085 static bool
00086 filter_filename(const char *filename)
00087 {
00088     char const * filter_filetypes = GET_STATE(dataset,filter_types);
00089     char const *leafname = strrchr(filename,PATH_DELIM);
00090     if (!leafname) {
00091         leafname = filename;
00092     }
00093     if (filter_filetypes) {
00094         char const *extension = filter_filetypes;
00095         size_t ext_len;
00096         for (   ; *extension; extension += ext_len + 1) {
00097             char *posn = strrchr(leafname,'.');
00098             ext_len = strlen(extension);
00099             if (posn && posn[1]) {
00100                 if (!strcmp(++posn,extension)) {
00101                     return true;
00102                 }
00103             }
00104         }
00105     }
00106     return false;
00107 }
00108 
00120 static void
00121 build_proc(     file_tree_h ft,
00122             char const *name,
00123             file_tree_traverse_state_t context)
00124 {
00125     switch(context) {
00126     case FT_ENTERING_DIR:
00127         report(PROGRESS_SEARCHING_DIR,NULL,"Searching dir \"%s\"",name);
00128         break;
00129     case FT_AT_FILE:
00130         report(PROGRESS_ADDED_FILE,NULL,"Added file \"%s\"",name);
00131         break;
00132     case FT_LEAVING_DIR:
00133         report( PROGRESS_ADDED_DIR,
00134                 NULL,
00135                 "Added %d files from dir \"%s\"",
00136                 file_tree_count(ft,FT_COUNT_FILES,NULL),
00137                 name);
00138         break;
00139     default:
00140         assert(false);
00141     }
00142 }
00143 
00146 /* API*/
00147 
00148 void
00149 dataset_filter_filetypes(const char *optarg)
00150 {
00151     size_t len = strlen(optarg);
00152     heap_str list = SET_STATE(dataset,filter_types) = zallocate(len + 2);
00153     strcpy(list,optarg);
00154     list[len + 1] = '\0';
00155     while(--len) {
00156         if (list[len] == ',') {
00157             list[len] = '\0';
00158         }
00159     }
00160     file_tree_set_filter(GET_PUBLIC(dataset,file_tree),filter_filename);
00161 }
00162 
00163 void
00164 dataset_add(char const *path)
00165 {
00166     file_tree_add(GET_PUBLIC(dataset,file_tree),path,build_proc);
00167 }
00168 
00169 
00170 /* EOF*/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines