Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Revision:
47:fbe956b10a91
Parent:
40:1324da35afd4
Child:
56:0bba0ef15697
--- a/SnSDUtils.cpp	Wed Oct 30 21:36:44 2013 +0000
+++ b/SnSDUtils.cpp	Wed Nov 06 00:04:30 2013 +0000
@@ -111,6 +111,46 @@
     return seq;
 }
 */
+
+DIR* SnSDUtils::OpenOrMakeAllDirs(const char* dirname) {
+#ifdef DEBUG
+    printf("OpenOrMakeAllDirs: [%s]\r\n",dirname);
+#endif
+    // try making the subdir
+    DIR* sd(0);
+    std::string dn(dirname);
+    std::size_t slash(0);
+    std::string sdr;
+    bool ok=true;
+    while (ok) {
+        slash=dn.find_first_of('/',slash+1);
+        if (slash==std::string::npos) {
+            sdr = dn; // no more slashes
+            ok=false;
+        } else {
+            sdr = dn.substr(0, slash);
+        }
+#ifdef DEBUG
+        printf("slash=%d, sdr=[%s] (%d), ok=%s\r\n",
+            (int)slash, sdr.c_str(), (int)sdr.size(),
+            ok ? "true" : "false");
+#endif        
+        if (sd!=0) {
+            // close the one from last time
+            closedir(sd);
+        }
+        // skip the /sd, as it's not a real directory
+        // should be skipped anyway, but..
+        if (sdr.compare(kSDdir)!=0) {
+#ifdef DEBUG
+            printf("calling OpenOrMakeDir [%s]\r\n",sdr.c_str());
+#endif
+            sd = OpenOrMakeDir(sdr.c_str());
+        }
+    }
+    return sd;
+}
+
 DIR* SnSDUtils::OpenOrMakeDir(const char* dirname) {
 #ifdef DEBUG
     printf("open dir %s\r\n",dirname);
@@ -145,7 +185,7 @@
     std::string rdnms ( GetSubDirFor(run, 0, rdlen, false) );
     const char* rdnm = rdnms.c_str();
     // open/make the run directory
-    FATDirHandle* rd(static_cast<FATDirHandle*>( OpenOrMakeDir(rdnm) ));
+    FATDirHandle* rd(static_cast<FATDirHandle*>( OpenOrMakeAllDirs(rdnm) ));
     struct dirent* rdent;
     uint16_t dseq(0), maxs(0);
 #ifdef DEBUG
@@ -189,7 +229,7 @@
     // don't compare seq#. don't use num of chars in case seq is >999
     const int32_t ncomp = strrchr(fn, 's') - fn;
     // open (or make) the run/seq dir
-    rd = static_cast<FATDirHandle*>( OpenOrMakeDir(rdnm) );
+    rd = static_cast<FATDirHandle*>( OpenOrMakeAllDirs(rdnm) );
     // get the new sequence number (ok if it overflows this seq "bin")
     maxs=0;
     while ( (rdent = readdir(rd))!=NULL ) {
@@ -320,7 +360,7 @@
 #endif
         if ((ld!=0) && (ld>name)) {
             std::string dn(name, ld-name);
-            DIR* d = OpenOrMakeDir(dn.c_str());
+            DIR* d = OpenOrMakeAllDirs(dn.c_str());
 #ifdef DEBUG
         printf("d=%p\r\n",d);
 #endif
@@ -412,6 +452,7 @@
                 PrintFilesInDirs(dnm.c_str());
             }
         }
+        closedir(d);
     }
 
 }