diff --git a/dmakefile b/dmakefile index e7bd9241f3116cbeee09c2003d0f5ef65fd8074e..7b8c7296a857b7ad8cf2a182bd39a1b3053e35f9 100644 --- a/dmakefile +++ b/dmakefile @@ -40,7 +40,7 @@ #BeginConfig #DramaSystem /* Indicates we are part of drama itself */ -ACMM_RELEASE=3_48$(ACMMBUILDVER) +ACMM_RELEASE=3_49$(ACMMBUILDVER) RELEASE=r$(ACMM_RELEASE) SYSTEM=dul /* System name (for release */ USERCCOPTIONS = AnsiCFull() @@ -77,7 +77,7 @@ SRC2 = xdcbuffers.c xdccommand.c xdcdits.c xdcimage.c xdcmessage.c xdcmisc.c \ xdcpval.c xdcscaling.c xdcworking.c xditscmd.c xdcload.c SRC3 = dulint.c NoEmbedded(messana.c CPlusPlusOnly(raw2sds.C) waittest.c waittest2.c) -CPlusPlusOnly(CPPOBJECTS = Obj(dcpp) Obj(dcpptask) Obj(dcppmonitor) Obj(dcpphandler) Obj(dcppcallbackbase)) +CPlusPlusOnly(CPPOBJECTS = Obj(dcpp) Obj(dcpptask) Obj(dcppmonitor) Obj(dcpphandler) Obj(dcppcallbackbase)) Obj(dulcpp) OBJECTS= Obj(dulreadfac) Obj(dultranslate) Obj(dulfindfile) \ TclOnly(CFITSIO(Obj(dulfitsread) Obj(dulfitswrite))) \ @@ -139,6 +139,8 @@ NormalProgramTarget(dparsefile, Obj(dparsefile) , Lib(dul), $(LIBS),) NormalProgramTarget(waittest, Obj(waittest) , Lib(dul), $(LIBS),) NormalProgramTarget(waittest2, Obj(waittest2) , Lib(dul), $(LIBS),) CPlusPlusOnly(NoEmbedded(CPPProgramTarget(raw2sds, Obj(raw2sds),,$(LIBS),))) + +#CPPProgramTarget(dulcpp, Obj(dulcpp),, $(LIBS),) /* * The libraries */ diff --git a/dul.h b/dul.h index ad7e2a14ea181341f3dba444aef773c5aad45f47..a1563d02675829e99ae1fc255bce721187aa1de6 100644 --- a/dul.h +++ b/dul.h @@ -244,4 +244,76 @@ typedef Dul___IntType *DulIntType; } #endif + + +#ifdef __cplusplus +/* + * C++ interface to dul features. Being added as needed + */ +#include <string> +#include <vector> +namespace DUL { + + /** A class which access a list of files + * + * We Construct the list of files using the specifcations, using + * the DulFindFile() rouine to build up the list. + * + * If the construtor is successfull, then you can access the set + * of file names. + */ + class FileList { + bool _valid; // Was constructor successfull. + std::vector <std::string> _fileNames; // Filename list. + + + public: + /** Construct a FileList. + * + * The user specifies a file name which may include environment + * variables and wildcards. If the constuction is successfull, then + * the user may access the set of files using other methods. + * + * @param fileSpec The file to search for, which may include + * wildcards and environment variables specifications. + * See DulFindFile() for details. + * @param defaultSpec The default file name, if any. + * @param allow_wildcards If true, allow wildcards. + * @param status Inherited status. If ok on return, the constructor + * was successful. + */ + FileList(const std::string& fileSpec, + const std::string& defaultSpec, + bool allow_wildcards, + StatusType *status); + + /** Return the number file names we have available. + * + * Simply returns the number of files available. + */ + unsigned NumFiles() const { + if (!_valid) return 0; + return _fileNames.size(); + } + + /** Return a file name by index. + * + * Returns a reference to the file name of the specified + * index. + * + *If the index is out of range or the object was not + * sucessfully constructed, then returns a reference to + * an empty string. + * + * @param index The index of the string of interest. Between + * 0 and (NumFiles()-1). + */ + const std::string & operator [](unsigned index) const; + }; + +} // namespace DUL + + +#endif + #endif diff --git a/dulcpp.C b/dulcpp.C new file mode 100644 index 0000000000000000000000000000000000000000..1b2ed9d0da5b255eade17da47a2c76ac608a63a9 --- /dev/null +++ b/dulcpp.C @@ -0,0 +1,147 @@ +/*+ d u l c p p . C + + * Module name: + dulcpp.C + + * Function: + Drama Utilities for finding files. C++ interface implmementation. + + * Description: + Implements the DUL::FileList class, which provides a C++ interface + to DulFindFile(). + + * Language: + C + + * Support: Tony Farrell, AAO + + *- + + * Copyright (c)Australian Austromonmical Observatory, 2011. + Not to be used for commercial purposes without AAP permission. + + * History: + 24-Mar-2011 - TJF - Original version + {@change entry@} + + + * "@(#) $Id$" + + */ + +static const char *rcsId="@(#) $Id$"; +static void *use_rcsId = (0 ? (void *)(&use_rcsId) : (void *) &rcsId); + +#include "dul.h" +#include "dul_err.h" + +#include <limits.h> // For PATH_MAX. + +//#define DULCPP_TEST +#ifdef DULCPP_TEST +#include "stdio.h" + +/* + * Test program used in the developement of these classes. + */ +int main() +{ + StatusType status = STATUS__OK; + + DUL::FileList files("XXX:*_coord_data.dat", "", true, &status); + + if (status != STATUS__OK) + { + fprintf(stderr,"Failed to read files - %lx\n", (long)(status)); + exit(1); + } + printf("Found %d files\n", files.NumFiles()); + + for (unsigned i = 0 ; i < files.NumFiles() ; ++i) + { + printf("File %d is \"%s\"\n", i+1, files[i].c_str()); + } +} +#endif + +using namespace DUL; + + +/* + * Construct a list of files usig the specification. + * + * For "fileSpec" and "defaultSpec" definitions, see DulFindFile(). + */ +FileList::FileList( + const std::string& fileSpec, + const std::string& defaultSpec, + const bool allow_wildcards, + StatusType * const status) : _valid(false) +{ + if (*status != STATUS__OK) return; + + DulFindFileContextType *context = 0; + char result[PATH_MAX]; + + /* + * Flags to DulFindFile. + */ + int flags= allow_wildcards ? 0 : DUL_M_NOWILD; + + /* + * Continue working here until we find all files which meet + * the specifiction. + */ + while (*status == STATUS__OK) + { + DulFindFile(fileSpec.c_str(), + defaultSpec.c_str(), + flags, + sizeof(result), + &context, + result, + status); + + if (*status == STATUS__OK) + { + // Found a file. + _fileNames.push_back(std::string(result)); + } + else if (*status == DUL__NOMORE) + { + // No more files to be found. + // But there is no error here,so clear status. + *status = STATUS__OK; + break; + } + } + /* + * Tidy up. + */ + StatusType ignore = STATUS__OK; + DulFindFileEnd(context, &ignore); + + /* + * If ok, set the objecte as valid. + */ + if (*status == STATUS__OK) + _valid = true; + +} + +static std::string emptyString; // Used by following method. +/* + * Return a file name by index. We must validate the index + * and that we have files to return. If not, we return an empty + * string. + * + * Otherwise we returnthe filename of interest. + */ +const std::string & FileList::operator[](unsigned index) const +{ + unsigned numFiles = _fileNames.size(); + if ((!_valid)||(numFiles == 0)||(index > (numFiles-1))) + return emptyString; + + return _fileNames[index]; +}