ADMB Documentation  11.1.2497
 All Classes Files Functions Variables Typedefs Friends Defines
makesub.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: makesub.cpp 2267 2014-08-28 23:40:42Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include <fvar.hpp>
00012 #include <adstring.hpp>
00013 //#include <fstream.h>
00014 #include <stdlib.h>
00015 #if !defined(_MSC_VER)
00016   #include <dirent.h>
00017   #include <sys/stat.h>
00018 #endif
00019 #if defined(__BORLANDC__)
00020 #  include <dir.h>
00021 #endif
00022 #if defined(_WIN32)
00023 #  include <windows.h>
00024 #else
00025 #  include <unistd.h>
00026 #endif
00027 
00028 #if !defined(_MSC_VER)
00029 #include <iostream>
00030 using namespace std;
00031 #include <memory.h>
00032 #include <fcntl.h>
00033 #include <sys/stat.h>
00034 #include <sys/types.h>
00035 #include <unistd.h>
00036 #endif
00037 
00038 #include <cassert>
00039 
00044 int ad_chdir(const char * s)
00045 {
00046 #if defined(_WIN32)
00047   return SetCurrentDirectory(s);
00048 #else
00049   return chdir(s);
00050 #endif
00051 }
00052 
00057 void ad_getcd(const adstring& _s)
00058 {
00059   adstring& s=(adstring&) (_s);
00060 #if defined(_WIN32)
00061   char tmp[101];
00062   tmp[0]='\0';
00063   GetCurrentDirectory(100,tmp);
00064   s=tmp;
00065 #else
00066   char tmp[101];
00067   tmp[0]='\0';
00068   #ifndef __SUNPRO_CC
00069   char* ret = getcwd(tmp,100);
00070   assert(ret != 0);
00071   #else
00072   getcwd(tmp,100);
00073   #endif
00074   s=adstring(tmp);
00075 #endif
00076 }
00077 
00082 int ad_mkdir(const char * s)
00083 {
00084 #if defined(_WIN32)
00085   return CreateDirectory(s,NULL);
00086 #else
00087   return mkdir(s,S_IREAD | S_IWRITE);
00088 #endif
00089 }
00090 
00095 int make_sub_directory(const char * s)
00096 {
00097   adstring currdir;
00098   adstring currdir1;
00099   ad_getcd(currdir);
00100   //const int len=100;
00101   //char dirname[len+1];
00102 #if defined(_WIN32)
00103   int ierr=SetCurrentDirectory(s);
00104 #else
00105   DIR * ierr=opendir(s);
00106 #endif
00107   if (!ierr) // NULL means failed
00108   {
00109     ad_getcd(currdir);
00110     return ad_mkdir(s);
00111   }
00112   else
00113   {
00114     ad_getcd(currdir1);
00115     ad_chdir(currdir);
00116     ad_getcd(currdir1);
00117     return 1;
00118   }
00119 }
00120