File: output_directory.cpp

package info (click to toggle)
meep-mpich2 1.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 25,824 kB
  • sloc: cpp: 27,370; python: 10,574; lisp: 1,213; makefile: 440; sh: 28
file content (145 lines) | stat: -rw-r--r-- 4,079 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* Copyright (C) 2005-2015 Massachusetts Institute of Technology  
%
%  This program is free software; you can redistribute it and/or modify
%  it under the terms of the GNU General Public License as published by
%  the Free Software Foundation; either version 2, or (at your option)
%  any later version.
%
%  This program is distributed in the hope that it will be useful,
%  but WITHOUT ANY WARRANTY; without even the implied warranty of
%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%  GNU General Public License for more details.
%
%  You should have received a copy of the GNU General Public License
%  along with this program; if not, write to the Free Software Foundation,
%  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>

#include "meep.hpp"

using namespace std;

namespace meep {

const char symlink_name[] = "latest_output";

void structure::set_output_directory(const char *name) {
  char buf[300];
  outdir = name;
  if (!quiet) master_printf("Using output directory %s/\n", name);
  if (readlink(symlink_name, buf, 300) > 0) {
    // Link already exists.
    unlink(symlink_name);
  }
  symlink(name, symlink_name);
  outdir = name;
}

void fields::set_output_directory(const char *name) {
  delete[] outdir;
  outdir = new char[strlen(name) + 1]; strcpy(outdir, name);
  for (int i=0;i<num_chunks;i++) chunks[i]->set_output_directory(outdir);
}

void fields_chunk::set_output_directory(const char *name) {
  outdir = name;
}

static void cp(const char *a, const char *b) {
  FILE *fa = fopen(a,"r");
  FILE *fb = fopen(b,"w");
  if (!fa || !fb) return;
  int ca;
  while (1) {
    ca = getc(fa);
    if (ca == EOF) break;
    putc(ca,fb);
  }
  fclose(fa);
  fclose(fb);
}

static bool is_ok_dir(const char *dirname) {
  DIR *dir;
  bool direxists = 0;
  if (am_master()) {
    direxists = (dir = opendir(dirname)) != NULL;
    if (direxists) closedir(dir);
    else mkdir(dirname, 00777);
  }
  direxists = broadcast(0, direxists);
  return !direxists;
}

FILE *create_output_file(const char *dirname, const char *fname) {
  const int buflen = 300;
  char n[buflen];
  snprintf(n, buflen, "%s/%s", dirname, fname);
  FILE *o = master_fopen(n, "w");
  if (!o) abort("Unable to create file %s!\n", n);
  return o;
}

const char *make_output_directory(const char *exename, const char *jobname) {
  const int buflen = 300;
  char basename[buflen];
  const char * const evil_suffs[] = { ".dac", ".cpp", ".cc", ".cxx", ".C" };
  char stripped_name[buflen];
  const char *bnp = exename; // stripped_name holds the actual name of the executable (dirs removed).
  const char *t;
  for (t=exename;*t;t++) {
    if (*t == '/') bnp = t+1;
  }
  
  snprintf(stripped_name, buflen, "%s", bnp);
  for (int i = 0; i < (int)(sizeof(evil_suffs) / sizeof(evil_suffs[0])); ++i) {
    int sufflen = strlen(evil_suffs[i]);
    if (strcmp(stripped_name + strlen(stripped_name) - sufflen, 
	       evil_suffs[i]) == 0
	&& strlen(stripped_name) > size_t(sufflen)) {
      stripped_name[strlen(stripped_name) - sufflen] = (char)0;
      break;
    }
  }

  char sourcename[buflen]; // Holds the "example.cpp" filename.
  snprintf(sourcename, buflen, "%s.cpp", stripped_name);

  if (jobname != NULL) {
    snprintf(basename, buflen, "%s", jobname);
  }
  else {
    snprintf(basename, buflen, "%s", stripped_name);
  }

  static char outdirname[buflen];
  snprintf(outdirname, buflen, "%s-out", basename);
  {
    int i = 0;
    while (!is_ok_dir(outdirname)) {
      if (!quiet)
	master_printf("Output directory %s already exists!\n", outdirname);
      snprintf(outdirname, buflen, "%s-out-%d", basename, i++);
    }
  }
  char outsrcname[buflen];
  snprintf(outsrcname, buflen, "%s/%s", outdirname, sourcename);
  cp(sourcename, outsrcname);

  return outdirname;
}

void trash_output_directory(const char *dirname) {
  if (am_master()) mkdir(dirname, 00777);
}

} // namespace meep