/*************************************************************************\ * Copyright (c) 2002 The University of Chicago, as Operator of Argonne * National Laboratory. * Copyright (c) 2002 The Regents of the University of California, as * Operator of Los Alamos National Laboratory. * This file is distributed subject to a Software License Agreement found * in the file LICENSE that is included with this distribution. \*************************************************************************/ /* file: find_files.c Hairong Shang $Log: find_files.c,v $ Revision 1.3 2008/08/05 19:38:02 soliday Added a backslash to an error message so that it would not see it as a trigraph sequence. Revision 1.2 2005/03/03 17:22:19 soliday Updated to work on WIN32 Revision 1.1 2005/02/02 16:09:03 soliday Moved find_files.c from mdblib to mdbcommon Revision 1.4 2005/01/04 22:31:02 shang removed the printf statements Revision 1.3 2005/01/04 22:04:00 shang it seems that the conditional value can not be used in calculation equation. Revision 1.2 2004/12/22 19:16:55 shang initialized startHour and endHour Revision 1.1 2004/12/21 20:12:30 shang routines for finding files between dates */ #include "mdb.h" #include "SDDS.h" short make_four_digit_year (short year) { if (year>100) return year; if (year<90) year +=2000; else year += 1900; return year; } /*Returns 0/1 for nonleap/leap year Returns -1 on error */ long is_leap_year (short year) { if (year<0) return -1; year=make_four_digit_year(year); return (year%4==0 && year%100!=0 || year%400==0); } #if !defined(_WIN32) /*increaseOrder=1, increase; 0, descrease order */ char **ls_dir (char *path, char *matchstr, long tailsOnly, long *files) { DIR *dirp; struct dirent *dp; char buf[2056]; char **filename; char *tmpPath; long files0=0; filename=NULL; dirp=NULL; dp=NULL; if (!path) { /*work with current path */ SDDS_CopyString(&tmpPath,"."); } else { if (!fexists(path)) { fprintf(stderr, "path %s does not exist!\n",path); exit(1); } SDDS_CopyString(&tmpPath, path); } if (dirp=opendir(tmpPath)) { while (dp=readdir(dirp)) { if (strcmp(dp->d_name,".") && strcmp(dp->d_name,"..")) { if (!matchstr || (matchstr && wild_match(dp->d_name,matchstr))) { if (tailsOnly) sprintf(buf,"%s",dp->d_name); else sprintf(buf,"%s/%s",tmpPath,dp->d_name); filename=SDDS_Realloc(filename,sizeof(*filename)*(files0+1)); SDDS_CopyString(&filename[files0],buf); files0++; } } } closedir(dirp); } if (files0) { /*sort file by time*/ } free(tmpPath); *files=files0; return filename; } char **find_files_between_dates(char *directory, char *rootname, char *suffix, short startYear, short startMonth, short startDay, short startJDay, short endYear, short endMonth, short endDay, short endJDay, char *filter, char **extensionList, long extensions, long tailsOnly, long *files, long increaseOrder) { short iyear, iday, toYearEnd, jDayLimit, year0, month0, day0, iday0, istoday, listall; long yearLists, fileLists, newLists, i, index, tmpLists, j, *sortedIndex; char **fileList, **yearList, **newList, **tmpList; char buffer[2056], tmpstr[2056], format[2056]; double startTime, endTime, startHour, endHour, time0, hour0; time_t longtime; sortedIndex=NULL; fileLists=yearLists=newLists=tmpLists=0; fileList=yearList=newList=tmpList=NULL; if (!directory || !rootname || !filter ) { fprintf(stderr,"directory or rootname or filter for searching files is not given!\n"); exit(1); } startTime=endTime=0; startHour=endHour=0; time(&longtime); TimeEpochToBreakdown(&year0, &iday0, &month0, &day0, &hour0, longtime); if (endYear==year0) { if (endJDay==iday0) istoday=1; else if (endMonth==month0 && endDay==day0) istoday=1; else istoday=0; } else istoday=0; if (strcmp(filter,"-??\??")==0) { if (tailsOnly) sprintf(format,"%s%s-%s",rootname,"%d","%02d%02d%s"); else sprintf(format,"%s/%s%s-%s",directory, rootname,"%d","%02d%02d%s"); } else if (strcmp(filter,"-??\?-??\??")==0) { if (tailsOnly) sprintf(format,"%s%s-%s-%s",rootname,"%d","%03d","%02d%02d%s"); else sprintf(format,"%s/%s%s-%s-%s",directory,rootname,"%d","%03d","%02d%02d%s"); } else SDDS_Bomb("invalid filter given, should be -???? or -??\?-????"); if (!startJDay) { TimeBreakdownToEpoch(startYear, 0, startMonth, startDay, startHour, &startTime); TimeEpochToBreakdown(&startYear, &startJDay, &startMonth, &startDay, &startHour,startTime); } if (!endJDay) { TimeBreakdownToEpoch(endYear, 0, endMonth, endDay, endHour, &endTime); TimeEpochToBreakdown(&endYear, &endJDay, &endMonth, &endDay, &endHour,endTime); } if (extensions) { tmpList=(char**)malloc(sizeof(tmpList)*extensions); for (i=0;i=0) { newList=SDDS_Realloc(newList, sizeof(*newList)*(newLists+1)); SDDS_CopyString(&newList[newLists], buffer); newLists++; } } } /*end of for iday loop */ if (newLists) { fileList=SDDS_Realloc(fileList,sizeof(*fileList)*(fileLists+newLists)); for (j=0;j