yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
write_file.cc
Go to the documentation of this file.
1 /*
2  * yosys -- Yosys Open SYnthesis Suite
3  *
4  * Copyright (C) 2014 Clifford Wolf <clifford@clifford.at>
5  * Copyright (C) 2014 Johann Glaser <Johann.Glaser@gmx.at>
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  *
19  */
20 
21 #include "kernel/yosys.h"
22 
25 
26 struct WriteFileFrontend : public Frontend {
27  WriteFileFrontend() : Frontend("=write_file", "write a text to a file") { }
28  virtual void help()
29  {
30  // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
31  log("\n");
32  log(" write_file [options] output_file [input_file]\n");
33  log("\n");
34  log("Write the text fron the input file to the output file.\n");
35  log("\n");
36  log(" -a\n");
37  log(" Append to output file (instead of overwriting)\n");
38  log("\n");
39  log("\n");
40  log("Inside a script the input file can also can a here-document:\n");
41  log("\n");
42  log(" write_file hello.txt <<EOT\n");
43  log(" Hello World!\n");
44  log(" EOT\n");
45  log("\n");
46  }
47  virtual void execute(std::istream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design*)
48  {
49  bool append_mode = false;
50  std::string output_filename;
51 
52  size_t argidx;
53  for (argidx = 1; argidx < args.size(); argidx++)
54  {
55  if (args[argidx] == "-a") {
56  append_mode = true;
57  continue;
58  }
59  break;
60  }
61 
62  if (argidx < args.size() && args[argidx].rfind("-", 0) != 0)
63  output_filename = args[argidx++];
64  else
65  log_cmd_error("Missing putput filename.\n");
66 
67  extra_args(f, filename, args, argidx);
68 
69  FILE *of = fopen(output_filename.c_str(), append_mode ? "a" : "w");
70  char buffer[64 * 1024];
71  int bytes;
72 
73  while (0 < (bytes = readsome(*f, buffer, sizeof(buffer))))
74  fwrite(buffer, bytes, 1, of);
75 
76  fclose(of);
77  }
79 
virtual void execute(std::istream *&f, std::string filename, std::vector< std::string > args, RTLIL::Design *)
Definition: write_file.cc:47
virtual void help()
Definition: write_file.cc:28
#define PRIVATE_NAMESPACE_BEGIN
Definition: yosys.h:97
#define PRIVATE_NAMESPACE_END
Definition: yosys.h:98
void log_cmd_error(const char *format,...)
Definition: log.cc:211
int readsome(std::istream &f, char *s, int n)
Definition: yosys.cc:100
#define USING_YOSYS_NAMESPACE
Definition: yosys.h:102
WriteFileFrontend WriteFileFrontend
void log(const char *format,...)
Definition: log.cc:180
void extra_args(std::istream *&f, std::string &filename, std::vector< std::string > args, size_t argidx)
Definition: register.cc:302