Panda3D
newheader.cxx
1 // Filename: newheader.cxx
2 // Created by: drose (05Jul04)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "dtoolbase.h"
16 
17 #include <stdio.h>
18 #include <time.h>
19 #include <stdlib.h>
20 
21 const char *cxx_style =
22 "// Filename: %s\n"
23 "// Created by: %s (%s)\n"
24 "//\n"
25 "////////////////////////////////////////////////////////////////////\n"
26 "//\n"
27 "// PANDA 3D SOFTWARE\n"
28 "// Copyright (c) Carnegie Mellon University. All rights reserved.\n"
29 "//\n"
30 "// All use of this software is subject to the terms of the revised BSD\n"
31 "// license. You should have received a copy of this license along\n"
32 "// with this source code in a file named \"LICENSE.\"\n"
33 "//\n"
34 "////////////////////////////////////////////////////////////////////\n"
35 "\n";
36 
37 const char *c_style =
38 "/* Filename: %s\n"
39 " * Created by: %s (%s)\n"
40 " *\n"
41 " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n"
42 " *\n"
43 " * PANDA 3D SOFTWARE\n"
44 " * Copyright (c) Carnegie Mellon University. All rights reserved.\n"
45 " *\n"
46 " * All use of this software is subject to the terms of the revised BSD\n"
47 " * license. You should have received a copy of this license along\n"
48 " * with this source code in a file named \"LICENSE.\"\n"
49 " *\n"
50 " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */\n"
51 "\n";
52 
53 struct FileDef {
54  const char *extension;
55  const char *header;
56 };
57 
58 FileDef file_def[] = {
59  { "h", cxx_style },
60  { "cxx", cxx_style },
61  { "I", cxx_style },
62  { "T", cxx_style },
63  { "c", c_style },
64  { NULL, NULL },
65 };
66 
67 void
68 generate_header(const char *header, const string &filename) {
69  const char *username = getenv("USER");
70  if (username == NULL) {
71  username = "";
72  }
73 
74  static const size_t max_date_buffer = 128;
75  char date_buffer[max_date_buffer];
76  time_t now = time(NULL);
77  strftime(date_buffer, max_date_buffer, "%d%b%y", localtime(&now));
78 
79  printf(header, filename.c_str(), username, date_buffer);
80 }
81 
82 int
83 main(int argc, char *argv[]) {
84  if (argc < 2) {
85  cerr << "Must specify the filename to generate a header for.\n";
86  exit(1);
87  }
88 
89  string filename = argv[1];
90  size_t dot = filename.rfind('.');
91  if (dot == string::npos) {
92  // No extension, no header.
93  return 0;
94  }
95 
96  string extension = filename.substr(dot + 1);
97 
98  size_t i = 0;
99  while (file_def[i].extension != NULL) {
100  if (extension == file_def[i].extension) {
101  generate_header(file_def[i].header, filename);
102  return 0;
103  }
104  i++;
105  }
106 
107  // No matching extension, no problem.
108  return 0;
109 }