|
| 1 | +/* Utility functions for writing output validators for the Kattis |
| 2 | + * problem format. |
| 3 | + * |
| 4 | + * The primary functions and variables available are the following. |
| 5 | + * In many cases, the only functions needed are "init_io", |
| 6 | + * "wrong_answer", and "accept". |
| 7 | + * |
| 8 | + * - init_io(argc, argv): |
| 9 | + * initialization |
| 10 | + * |
| 11 | + * - judge_in, judge_ans, author_out: |
| 12 | + * std::istream objects for judge input file, judge answer |
| 13 | + * file, and submission output file. |
| 14 | + * |
| 15 | + * - accept(): |
| 16 | + * exit and give Accepted! |
| 17 | + * |
| 18 | + * - accept_with_score(double score): |
| 19 | + * exit with Accepted and give a score (for scoring problems) |
| 20 | + * |
| 21 | + * - judge_message(std::string msg, ...): |
| 22 | + * printf-style function for emitting a judge message (a |
| 23 | + * message that gets displayed to a privileged user with access |
| 24 | + * to secret data etc). |
| 25 | + * |
| 26 | + * - wrong_answer(std::string msg, ...): |
| 27 | + * printf-style function for exitting and giving Wrong Answer, |
| 28 | + * and emitting a judge message (which would typically explain |
| 29 | + * the cause of the Wrong Answer) |
| 30 | + * |
| 31 | + * - judge_error(std::string msg, ...): |
| 32 | + * printf-style function for exitting and giving Judge Error, |
| 33 | + * and emitting a judge message (which would typically explain |
| 34 | + * the cause of the Judge Error) |
| 35 | + * |
| 36 | + * - author_message(std::string msg, ...): |
| 37 | + * printf-style function for emitting an author message (a |
| 38 | + * message that gets displayed to the author of the |
| 39 | + * submission). (Use with caution, and be careful not to let |
| 40 | + * it leak information!) |
| 41 | + * |
| 42 | + */ |
| 43 | +#pragma once |
| 44 | + |
| 45 | +#include <sys/stat.h> |
| 46 | +#include <cassert> |
| 47 | +#include <cstdarg> |
| 48 | +#include <cstdlib> |
| 49 | +#include <iostream> |
| 50 | +#include <fstream> |
| 51 | +#include <sstream> |
| 52 | + |
| 53 | +typedef void (*feedback_function)(const std::string &, ...); |
| 54 | + |
| 55 | +const int EXITCODE_AC = 42; |
| 56 | +const int EXITCODE_WA = 43; |
| 57 | +const std::string FILENAME_AUTHOR_MESSAGE = "authormessage.txt"; |
| 58 | +const std::string FILENAME_JUDGE_MESSAGE = "judgemessage.txt"; |
| 59 | +const std::string FILENAME_JUDGE_ERROR = "judgeerror.txt"; |
| 60 | +const std::string FILENAME_SCORE = "score.txt"; |
| 61 | + |
| 62 | +#define USAGE "%s: judge_in judge_ans feedback_dir < author_out\n" |
| 63 | + |
| 64 | +std::ifstream judge_in, judge_ans; |
| 65 | +std::istream author_out(std::cin.rdbuf()); |
| 66 | + |
| 67 | +char *feedbackdir = NULL; |
| 68 | + |
| 69 | +void vreport_feedback(const std::string &category, |
| 70 | + const std::string &msg, |
| 71 | + va_list pvar) { |
| 72 | + std::ostringstream fname; |
| 73 | + if (feedbackdir) |
| 74 | + fname << feedbackdir << '/'; |
| 75 | + fname << category; |
| 76 | + FILE *f = fopen(fname.str().c_str(), "a"); |
| 77 | + assert(f); |
| 78 | + vfprintf(f, msg.c_str(), pvar); |
| 79 | + fclose(f); |
| 80 | +} |
| 81 | + |
| 82 | +void report_feedback(const std::string &category, const std::string &msg, ...) { |
| 83 | + va_list pvar; |
| 84 | + va_start(pvar, msg); |
| 85 | + vreport_feedback(category, msg, pvar); |
| 86 | +} |
| 87 | + |
| 88 | +void author_message(const std::string &msg, ...) { |
| 89 | + va_list pvar; |
| 90 | + va_start(pvar, msg); |
| 91 | + vreport_feedback(FILENAME_AUTHOR_MESSAGE, msg, pvar); |
| 92 | +} |
| 93 | + |
| 94 | +void judge_message(const std::string &msg, ...) { |
| 95 | + va_list pvar; |
| 96 | + va_start(pvar, msg); |
| 97 | + vreport_feedback(FILENAME_JUDGE_MESSAGE, msg, pvar); |
| 98 | +} |
| 99 | + |
| 100 | +void wrong_answer(const std::string &msg, ...) { |
| 101 | + va_list pvar; |
| 102 | + va_start(pvar, msg); |
| 103 | + vreport_feedback(FILENAME_JUDGE_MESSAGE, msg, pvar); |
| 104 | + exit(EXITCODE_WA); |
| 105 | +} |
| 106 | + |
| 107 | +void judge_error(const std::string &msg, ...) { |
| 108 | + va_list pvar; |
| 109 | + va_start(pvar, msg); |
| 110 | + vreport_feedback(FILENAME_JUDGE_ERROR, msg, pvar); |
| 111 | + assert(0); |
| 112 | +} |
| 113 | + |
| 114 | +void accept() { |
| 115 | + exit(EXITCODE_AC); |
| 116 | +} |
| 117 | + |
| 118 | +void accept_with_score(double scorevalue) { |
| 119 | + report_feedback(FILENAME_SCORE, "%.9le", scorevalue); |
| 120 | + exit(EXITCODE_AC); |
| 121 | +} |
| 122 | + |
| 123 | + |
| 124 | +bool is_directory(const char *path) { |
| 125 | + struct stat entry; |
| 126 | + return stat(path, &entry) == 0 && S_ISDIR(entry.st_mode); |
| 127 | +} |
| 128 | + |
| 129 | +void init_io(int argc, char **argv) { |
| 130 | + if(argc < 4) { |
| 131 | + fprintf(stderr, USAGE, argv[0]); |
| 132 | + judge_error("Usage: %s judgein judgeans feedbackdir [opts] < userout", argv[0]); |
| 133 | + } |
| 134 | + |
| 135 | + // Set up feedbackdir first, as that allows us to produce feedback |
| 136 | + // files for errors in the other parameters. |
| 137 | + if (!is_directory(argv[3])) { |
| 138 | + judge_error("%s: %s is not a directory\n", argv[0], argv[3]); |
| 139 | + } |
| 140 | + feedbackdir = argv[3]; |
| 141 | + |
| 142 | + judge_in.open(argv[1], std::ios_base::in); |
| 143 | + if (judge_in.fail()) { |
| 144 | + judge_error("%s: failed to open %s\n", argv[0], argv[1]); |
| 145 | + } |
| 146 | + |
| 147 | + judge_ans.open(argv[2], std::ios_base::in); |
| 148 | + if (judge_ans.fail()) { |
| 149 | + judge_error("%s: failed to open %s\n", argv[0], argv[2]); |
| 150 | + } |
| 151 | + |
| 152 | + author_out.rdbuf(std::cin.rdbuf()); |
| 153 | +} |
0 commit comments