summaryrefslogtreecommitdiffstats
path: root/courgette/courgette_minimal_tool.cc
blob: 972eac472baeee5287844a20eca4a4d131cac554 (plain)
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
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// 'courgette_minimal_tool' is not meant to be a serious command-line tool.  It
// has the minimum logic to apply a Courgette patch to a file.  The main purpose
// is to monitor the code size of the patcher.

#include <string>

#include "courgette/third_party/bsdiff.h"
#include "courgette/courgette.h"
#include "courgette/streams.h"

void PrintHelp() {
  fprintf(stderr,
    "Usage:\n"
    "  courgette_minimal_tool <old-file-input> <patch-file-input>"
    " <new-file-output>\n"
    "\n");
}

void UsageProblem(const char* message) {
  fprintf(stderr, "%s\n", message);
  PrintHelp();
  exit(1);
}

void Problem(const char* message) {
  fprintf(stderr, "%s\n", message);
  exit(1);
}

#if defined(OS_WIN)
int wmain(int argc, const wchar_t* argv[]) {
#else
int main(int argc, const char* argv[]) {
#endif
  if (argc != 4)
    UsageProblem("bad args");

  courgette::Status status =
      courgette::ApplyEnsemblePatch(argv[1], argv[2], argv[3]);

  if (status != courgette::C_OK) {
    if (status == courgette::C_READ_OPEN_ERROR) Problem("Can't open file.");
    if (status == courgette::C_WRITE_OPEN_ERROR) Problem("Can't open file.");
    if (status == courgette::C_READ_ERROR) Problem("Can't read from file.");
    if (status == courgette::C_WRITE_ERROR) Problem("Can't write to file.");
    Problem("patch failed.");
  }

  return 0;
}