Index: /trunk/ippTools/src/dettool.c
===================================================================
--- /trunk/ippTools/src/dettool.c	(revision 7173)
+++ /trunk/ippTools/src/dettool.c	(revision 7174)
@@ -14,4 +14,5 @@
 static bool addstacMode(pxConfig *config);
 static bool stacMode(pxConfig *config);
+static psArray *validDetInputClassIds(pxConfig *config, const char *det_id);
 static bool stacframeMode(pxConfig *config);
 static bool addmasterMode(pxConfig *config);
@@ -479,4 +480,71 @@
     }
 
+    psArray *valid_class_ids = validDetInputClassIds(config, det_id);
+    if (!valid_class_ids) {
+        psError(PS_ERR_UNKNOWN, false, "no detInputExp imfile class_ids found");
+        return false; 
+    }
+    
+    // check class_ids for validity
+    for (long i = 0; i < psArrayLength(stackedImfiles); i++) {
+        bool valid = false;
+        for (long j = 0; j < psArrayLength(valid_class_ids); j++) {
+            if (strcmp(((detStackedImfileRow *)stackedImfiles->data[i])->class_id,
+                   (char *)valid_class_ids->data[j]) == 0) {
+                valid = true;
+                if (!psArrayRemove(valid_class_ids, valid_class_ids->data[j])) {
+                    psError(PS_ERR_UNKNOWN, false, "psArrayRemove() failed");
+                    psFree(stackedImfiles);
+                    psFree(valid_class_ids);
+                    return false;
+                }
+                j--; // must update loop index
+            }
+        }
+        if (!valid) {
+            psError(PS_ERR_UNKNOWN, true,
+        "class_id %s does not corespond to on contained in an detInputExp",
+            ((detStackedImfileRow *)stackedImfiles->data[i])->class_id);
+            psFree(stackedImfiles);
+            psFree(valid_class_ids);
+            return false;
+        }
+    }
+
+    // check for residual (unmatched) input imfile class_ids
+    if (psArrayLength(valid_class_ids)) {
+        psError(PS_ERR_UNKNOWN, true, "det_id frame is missing %d class_ids",
+            psArrayLength(valid_class_ids));
+        psFree(valid_class_ids);
+        return false;
+    }
+    psFree(valid_class_ids);
+
+    // print detStackedImfile if all class_ids are matched
+    psMetadata *output = psMetadataAlloc();
+    for (long i = 0; i < psArrayLength(stackedImfiles); i++) {
+        psMetadata *md = detStackedImfileMetadataFromObject(
+                stackedImfiles->data[i]);
+        psMetadataAddMetadata(
+            output, PS_LIST_TAIL, "detStackedImfile", PS_META_DUPLICATE_OK,
+            NULL, md
+        );
+    }
+
+    psString str = psMetadataConfigFormat(output); 
+    psFree(output);
+    fprintf(stdout, "%s\n", str);
+    psFree(str);
+  
+    return true;
+}
+
+static psArray *validDetInputClassIds(pxConfig *config, const char *det_id)
+{
+    PS_ASSERT_PTR_NON_NULL(config, NULL);
+    // det_id is input as a string because the fact that it is an integer
+    // is just a database impliementation detail.
+    PS_ASSERT_PTR_NON_NULL(det_id, NULL);
+
     psArray *inputExps = NULL;
     {
@@ -486,6 +554,5 @@
             psError(PS_ERR_UNKNOWN, false, "failed to add item det_id");
             psFree(where);
-            psFree(stackedImfiles);
-            return false;
+            return NULL;
         }
         inputExps = detInputExpSelectRowObjects(config->dbh, where, 0);
@@ -505,5 +572,5 @@
                 psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id");
                 psFree(where);
-                return false;
+                return NULL;
             }
         }
@@ -536,57 +603,6 @@
     }
     psFree(rawImfiles);
-    
-    // check class_ids for validity
-    for (long i = 0; i < psArrayLength(stackedImfiles); i++) {
-        bool valid = false;
-        for (long j = 0; j < psArrayLength(valid_class_ids); j++) {
-            if (strcmp(((detStackedImfileRow *)stackedImfiles->data[i])->class_id,
-                   (char *)valid_class_ids->data[j]) == 0) {
-                valid = true;
-                if (!psArrayRemove(valid_class_ids, valid_class_ids->data[j])) {
-                    psError(PS_ERR_UNKNOWN, false, "psArrayRemove() failed");
-                    psFree(stackedImfiles);
-                    psFree(valid_class_ids);
-                    return false;
-                }
-                j--; // must update loop index
-            }
-        }
-        if (!valid) {
-            psError(PS_ERR_UNKNOWN, true,
-        "class_id %s does not corespond to on contained in an detInputExp",
-            ((detStackedImfileRow *)stackedImfiles->data[i])->class_id);
-            psFree(stackedImfiles);
-            psFree(valid_class_ids);
-            return false;
-        }
-    }
-
-    // check for residual (unmatched) input imfile class_ids
-    if (psArrayLength(valid_class_ids)) {
-        psError(PS_ERR_UNKNOWN, true, "det_id frame is missing %d class_ids",
-            psArrayLength(valid_class_ids));
-        psFree(valid_class_ids);
-        return false;
-    }
-    psFree(valid_class_ids);
-
-    // print detStackedImfile if all class_ids are matched
-    psMetadata *output = psMetadataAlloc();
-    for (long i = 0; i < psArrayLength(stackedImfiles); i++) {
-        psMetadata *md = detStackedImfileMetadataFromObject(
-                stackedImfiles->data[i]);
-        psMetadataAddMetadata(
-            output, PS_LIST_TAIL, "detStackedImfile", PS_META_DUPLICATE_OK,
-            NULL, md
-        );
-    }
-
-    psString str = psMetadataConfigFormat(output); 
-    psFree(output);
-    fprintf(stdout, "%s\n", str);
-    psFree(str);
-  
-    return true;
+
+    return valid_class_ids;
 }
 
@@ -685,4 +701,14 @@
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
-    return true;
-}
+
+    // find all detMasterIImfile for det_id
+    // XXX det_id is requried as a simplification
+    psArray *masterImfiles =
+        detMasterImfileSelectRowObjects(config->dbh, config->where, 0);
+    if (!masterImfiles) {
+        psError(PS_ERR_UNKNOWN, false, "no detMasterImfile rows found");
+        return NULL;
+    }
+
+    return true;
+}
