1 /*
2  *
3  *  Copyright (C) 1993-2018, OFFIS e.V.
4  *  All rights reserved.  See COPYRIGHT file for details.
5  *
6  *  This software and supporting documentation were developed by
7  *
8  *    OFFIS e.V.
9  *    R&D Division Health
10  *    Escherweg 2
11  *    D-26121 Oldenburg, Germany
12  *
13  *
14  *  Module:  dcmqrdb
15  *
16  *  Author:  Andrew Hewett
17  *
18  *  Purpose: This test program registers image files in the image database.
19  *
20  */
21 
22 #include "dcmtk/config/osconfig.h"    /* make sure OS specific configuration is included first */
23 
24 #define INCLUDE_CSTDLIB
25 #define INCLUDE_CSTDIO
26 #define INCLUDE_CSTRING
27 #include "dcmtk/ofstd/ofstdinc.h"
28 
29 #include "dcmtk/ofstd/ofconapp.h"
30 #include "dcmtk/ofstd/ofcmdln.h"
31 #include "dcmtk/dcmdata/dcdict.h"
32 #include "dcmtk/dcmdata/cmdlnarg.h"
33 #include "dcmtk/dcmdata/dcuid.h"       /* for dcmtk version name */
34 #include "dcmtk/dcmnet/dicom.h"
35 #include "dcmtk/dcmnet/diutil.h"
36 #include "dcmtk/dcmnet/dul.h"
37 #include "dcmtk/dcmnet/dcompat.h"
38 #include "dcmtk/dcmqrdb/dcmqrdbs.h"
39 #include "dcmtk/dcmqrdb/dcmqrdbi.h"
40 
41 #ifdef WITH_ZLIB
42 #include <zlib.h>        /* for zlibVersion() */
43 #endif
44 
45 #define OFFIS_CONSOLE_APPLICATION "dcmqridx"
46 
47 static OFLogger dcmqridxLogger = OFLog::getLogger("dcmtk.apps." OFFIS_CONSOLE_APPLICATION);
48 
49 static char rcsid[] = "$dcmtk: " OFFIS_CONSOLE_APPLICATION " v"
50   OFFIS_DCMTK_VERSION " " OFFIS_DCMTK_RELEASEDATE " $";
51 
52 
53 // ********************************************
54 
55 
56 #define SHORTCOL 3
57 #define LONGCOL  12
58 
59 
main(int argc,char * argv[])60 int main (int argc, char *argv[])
61 {
62     char sclass [120] ;
63     char sinst  [120] ;
64 #ifdef DEBUG
65     char fname  [120] ;
66 #endif
67     DcmQueryRetrieveDatabaseStatus status;
68 
69     const char *opt_storageArea = NULL;
70     OFBool opt_print = OFFalse;
71     OFBool opt_isNewFlag = OFTrue;
72 
73 #ifdef WITH_TCPWRAPPER
74     // this code makes sure that the linker cannot optimize away
75     // the DUL part of the network module where the external flags
76     // for libwrap are defined. Needed on OpenBSD.
77     dcmTCPWrapperDaemonName.set(NULL);
78 #endif
79 
80     OFCommandLine cmd;
81     OFConsoleApplication app(OFFIS_CONSOLE_APPLICATION, "Register a DICOM image file in an image database index file", rcsid);
82     cmd.setOptionColumns(LONGCOL, SHORTCOL);
83     cmd.setParamColumn(LONGCOL + SHORTCOL + 2);
84 
85     cmd.addParam("index-out",  "storage area for the index file (directory)");
86     cmd.addParam("dcmfile-in", "DICOM image file to be registered in the index file", OFCmdParam::PM_MultiOptional);
87 
88     cmd.addGroup("options:", LONGCOL, SHORTCOL);
89      cmd.addOption("--help",    "-h", "print this help text and exit", OFCommandLine::AF_Exclusive);
90      cmd.addOption("--version",       "print version information and exit", OFCommandLine::AF_Exclusive);
91      OFLog::addOptions(cmd);
92      cmd.addOption("--print",   "-p", "list contents of database index file");
93      cmd.addOption("--not-new", "-n", "set instance reviewed status to 'not new'");
94 
95     /* evaluate command line */
96     prepareCmdLineArgs(argc, argv, OFFIS_CONSOLE_APPLICATION);
97     if (app.parseCommandLine(cmd, argc, argv))
98     {
99         /* check exclusive options first */
100         if (cmd.hasExclusiveOption())
101         {
102             if (cmd.findOption("--version"))
103             {
104                 app.printHeader(OFTrue /*print host identifier*/);
105                 COUT << OFendl << "External libraries used:";
106 #if !defined(WITH_ZLIB) && !defined(WITH_TCPWRAPPER)
107                 COUT << " none" << OFendl;
108 #else
109                 COUT << OFendl;
110 #endif
111 #ifdef WITH_ZLIB
112                 COUT << "- ZLIB, Version " << zlibVersion() << OFendl;
113 #endif
114 #ifdef WITH_TCPWRAPPER
115                 COUT << "- LIBWRAP" << OFendl;
116 #endif
117                 return 0;
118             }
119         }
120 
121         /* command line parameters and options */
122         cmd.getParam(1, opt_storageArea);
123 
124         OFLog::configureFromCommandLine(cmd, app);
125 
126         if (cmd.findOption("--print"))
127             opt_print = OFTrue;
128 
129         if (cmd.findOption("--not-new"))
130             opt_isNewFlag = OFFalse;
131     }
132 
133     /* print resource identifier */
134     OFLOG_DEBUG(dcmqridxLogger, rcsid << OFendl);
135 
136     /* make sure data dictionary is loaded */
137     if (!dcmDataDict.isDictionaryLoaded())
138     {
139         OFLOG_WARN(dcmqridxLogger, "no data dictionary loaded, check environment variable: "
140             << DCM_DICT_ENVIRONMENT_VARIABLE);
141     }
142 
143     OFCondition cond;
144     DcmQueryRetrieveIndexDatabaseHandle hdl(opt_storageArea, DB_UpperMaxStudies, DB_UpperMaxBytesPerStudy, cond);
145     if (cond.good())
146     {
147         hdl.enableQuotaSystem(OFFalse); /* disable deletion of images */
148         int paramCount = cmd.getParamCount();
149         for (int param = 2; param <= paramCount; param++)
150         {
151             const char *opt_imageFile = NULL;
152             cmd.getParam(param, opt_imageFile);
153             if (access(opt_imageFile, R_OK) < 0)
154                 OFLOG_ERROR(dcmqridxLogger, "cannot access: " << opt_imageFile);
155             else
156             {
157                 OFLOG_INFO(dcmqridxLogger, "registering: " << opt_imageFile);
158                 if (DU_findSOPClassAndInstanceInFile(opt_imageFile, sclass, sizeof(sclass), sinst, sizeof(sinst)))
159                 {
160 #ifdef DEBUG
161                     /*** Test what filename is recommended by DB_Module **/
162                     hdl.makeNewStoreFileName (sclass, sinst, fname, sizeof(fname));
163                     OFLOG_DEBUG(dcmqridxLogger, "DB_Module recommends " << fname << " for filename");
164 #endif
165                     hdl.storeRequest(sclass, sinst, opt_imageFile, &status, opt_isNewFlag) ;
166                 } else
167                     OFLOG_ERROR(dcmqridxLogger, "cannot load dicom file: " << opt_imageFile);
168             }
169         }
170         if (opt_print)
171         {
172             COUT << "-- DB Index File --" << OFendl;
173             hdl.printIndexFile(OFconst_cast(char *, opt_storageArea));
174         }
175         return 0;
176     }
177 
178     return 1;
179 }
180