Panda3D
build_patch.cxx
1 // Filename: build_patch.cxx
2 // Created by:
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 "pandabase.h"
16 #include "pystub.h"
17 #include "panda_getopt.h"
18 #include "preprocess_argv.h"
19 #include "patchfile.h"
20 #include "filename.h"
21 
22 void
23 usage() {
24  cerr << "Usage: build_patch [opts] <old_file> <new_file>" << endl;
25 }
26 
27 void
28 help() {
29  usage();
30  cerr << "\n"
31  "This program generates a patch file that describes the differences\n"
32  "between any two source files. The patch file can later be used to\n"
33  "construct <new_file>, given <old_file>. Arbitrary file types, including\n"
34  "binary files, are supported.\n\n"
35 
36  "The patching algorithm can get very slow for very large files. As an\n"
37  "optimization, if the input files are both Panda Multifiles, the patcher\n"
38  "will by default patch them on a per-subfile basis, which has the potential\n"
39  "to be much faster. The -c option will forbid this and force the patcher\n"
40  "to work on the full file.\n\n"
41 
42  "Options:\n\n"
43 
44  " -o output_name\n"
45  " Specify the filename of the patch file to generate.\n\n"
46 
47  " -c\n"
48  " Always generate patches against the complete file, even if the\n"
49  " input files appear to be multifiles.\n\n"
50 
51  " -f footprint_length\n"
52  " Specify the footprint length for the patching algorithm.\n\n";
53 }
54 
55 int
56 main(int argc, char **argv) {
57  // A call to pystub() to force libpystub.so to be linked in.
58  pystub();
59 
60  Filename patch_file;
61  bool complete_file = false;
62  int footprint_length = 0;
63 
64  // extern char *optarg;
65  extern int optind;
66  static const char *optflags = "o:cf:h";
67  preprocess_argv(argc, argv);
68  int flag = getopt(argc, argv, optflags);
69  Filename rel_path;
70  while (flag != EOF) {
71  switch (flag) {
72  case 'o':
73  patch_file = optarg;
74  break;
75 
76  case 'c':
77  complete_file = true;
78  break;
79 
80  case 'f':
81  footprint_length = atoi(optarg);
82  break;
83 
84  case 'h':
85  help();
86  return 1;
87  case '?':
88  usage();
89  return 1;
90  default:
91  cerr << "Unhandled switch: " << flag << endl;
92  break;
93  }
94  flag = getopt(argc, argv, optflags);
95  }
96  argc -= (optind - 1);
97  argv += (optind - 1);
98 
99  if (argc < 3) {
100  usage();
101  return 1;
102  }
103 
104  Filename src_file = Filename::from_os_specific(argv[1]);
105  src_file.set_binary();
106 
107  Filename dest_file = Filename::from_os_specific(argv[2]);
108  dest_file.set_binary();
109 
110  if (patch_file.empty()) {
111  patch_file = dest_file.get_fullpath() + ".pch";
112  }
113  Patchfile pfile;
114 
115  pfile.set_allow_multifile(!complete_file);
116  if (footprint_length != 0) {
117  cerr << "Footprint length is " << footprint_length << "\n";
118  pfile.set_footprint_length(footprint_length);
119  }
120 
121  cerr << "Building patch file to convert " << src_file << " to "
122  << dest_file << endl;
123  if (pfile.build(src_file, dest_file, patch_file) == false) {
124  cerr << "build patch failed" << endl;
125  return 1;
126  }
127 
128  return 0;
129 }
string get_fullpath() const
Returns the entire filename: directory, basename, extension.
Definition: filename.I:398
void set_binary()
Indicates that the filename represents a binary file.
Definition: filename.I:494
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
static Filename from_os_specific(const string &os_specific, Type type=T_general)
This named constructor returns a Panda-style filename (that is, using forward slashes, and no drive letter) based on the supplied filename string that describes a filename in the local system conventions (for instance, on Windows, it may use backslashes or begin with a drive letter and a colon).
Definition: filename.cxx:332