| 87 | } |
| 88 | |
| 89 | bool BinaryPatch::loadDIF(std::string name) |
| 90 | { |
| 91 | entries.clear(); |
| 92 | |
| 93 | std::ifstream infile(name); |
| 94 | if(infile.bad()) |
| 95 | { |
| 96 | cerr << "Cannot open file: " << name << endl; |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | std::string s; |
| 101 | while(std::getline(infile, s)) |
| 102 | { |
| 103 | // Parse lines that begin with "[0-9a-f]+:" |
| 104 | size_t idx = s.find(':'); |
| 105 | if (idx == std::string::npos || idx == 0 || idx > 8) |
| 106 | continue; |
| 107 | |
| 108 | bool ok = true; |
| 109 | for (size_t i = 0; i < idx; i++) |
| 110 | if (!is_hex(s[i])) |
| 111 | ok = false; |
| 112 | if (!ok) |
| 113 | continue; |
| 114 | |
| 115 | unsigned off, oval, nval; |
| 116 | int nchar = 0; |
| 117 | int cnt = sscanf(s.c_str(), "%x: %x %x%n", &off, &oval, &nval, &nchar); |
| 118 | |
| 119 | if (cnt < 3) |
| 120 | { |
| 121 | cerr << "Could not parse: " << s << endl; |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | for (size_t i = nchar; i < s.size(); i++) |
| 126 | { |
| 127 | if (!isspace(s[i])) |
| 128 | { |
| 129 | cerr << "Garbage at end of line: " << s << endl; |
| 130 | return false; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if (oval >= 256 || nval >= 256) |
| 135 | { |
| 136 | cerr << "Invalid byte values: " << s << endl; |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | Byte bv = { off, patch_byte(oval), patch_byte(nval) }; |
| 141 | entries.push_back(bv); |
| 142 | } |
| 143 | |
| 144 | if (entries.empty()) |
| 145 | { |
| 146 | cerr << "No lines recognized." << endl; |