/** * ファイルを移動する。 * 移動元がファイルで、移動先がディレクトリの場合は、そのディレクトリの下にファイルを移動する。 */ function move_file($src, $trg) { if (!is_file($src) && !is_dir($src)) { log_fatal("Faild to rename: {$src} => {$trg} -- missing {$src}"); exit(1); } if (is_file($src) && is_dir($trg)) { $base_name = basename($src); $trg .= "/{$base_name}"; } if (is_dir($trg)) { rmdirs(array($trg)); } elseif (is_file($trg)) { unlink_files(array($trg)); } if (!rename($src, $trg)) { log_fatal("Faild to rename: {$src} => {$trg}"); exit(1); } }
/** * 各レベルのワークファイルを生成する。 */ function generate_work_files($src_list, $trg, $name, $col, $limit) { $rcnt = 0; $wcnt = 0; for ($i = 0; $i <= 9; ++$i) { $out = "{$trg}/{$name}{$i}.csv"; foreach ($src_list as $src) { $s = fopen($src, "rb"); $t = fopen($out, "ab"); while (!feof($s)) { $line = fgets($s); if (!$line) { continue; } ++$rcnt; if (substr($line, $col, 1) != $i) { continue; } ++$wcnt; fwrite($t, $line); } fclose($s); fclose($t); } if ($col == $limit) { if (0 == filesize($out)) { unlink_files(array($out)); } } else { $ret = generate_work_files(array($out), $trg, "{$name}{$i}", $col + 1, $limit); $rcnt += $ret[0]; $wcnt += $ret[1]; unlink_files(array($out)); } } return array($rcnt, $wcnt); }
/** * Web上のファイルをダウンロードする。 */ function download($url, $trg) { $trg = PATH_TMP . "/" . $trg; unlink_files(array($trg)); $f = fopen($url, "rb"); $t = fopen($trg, "wb"); while (!feof($f)) { fwrite($t, fread($f, BUFSIZ)); } fclose($t); fclose($f); if (!is_file($trg)) { log_info("Failed to download: {$trg}"); return false; } log_info("Downloaded: {$trg}"); return true; }
/** * HTML 詳細データを作成する。 */ function generate_html_detail($tmp, $code, $city_name, &$areas, &$firms, &$appendix) { global $yomikubun; $pref_code = substr($code, 0, 2); $file_name = substr($code, 2, 3); $floors = array(); $add1_shrink = ""; $numbers = false; $street = ""; $src = "{$tmp}/{$pref_code}/{$file_name}.csv"; $s = fopen($src, "rb"); while ($cols = fgetcsv($s, 1024, ",")) { if (0 < preg_match('/\\(/', $cols[1])) { $numbers = true; foreach ($yomikubun as $key => $val) { if (0 == preg_match($val[1], $cols[4])) { continue; } $yomi = $key; break; } } elseif ($numbers) { } else { foreach ($yomikubun as $key => $val) { if (0 == preg_match($val[1], $cols[4])) { continue; } $yomi = $key; break; } } $matches = array(); if (0 < preg_match('/(.*)(\\([^\\(\\)]*\\))$/', $cols[1], $matches)) { $numbers = false; if ($add1_shrink != "" && $matches[1] != $add1_shrink) { $add1_shrink = ""; update_html_floores($tmp, $code, $city_name, $floors, $appendix); } if (0 < preg_match("/階層不明/", $matches[2])) { $add1_shrink = $matches[1]; ++$appendix; $floors = array(); array_push($areas, array("link" => "{$file_name}-{$appendix}.html", "add1" => $matches[1], "yomi" => $yomi)); } if ($add1_shrink != "") { array_push($floors, array("zip" => $cols[0], "add1" => $matches[1], "add2" => preg_replace("/[\\(\\)]/", "", $matches[2]), "yomi" => $yomi)); } else { array_push($areas, array("zip" => $cols[0], "add1" => $matches[1], "add2" => $matches[2], "yomi" => $yomi)); } } elseif ($cols[3] != "") { array_push($firms, array("zip" => $cols[0], "add1" => $cols[1], "add2" => $cols[2], "bldg" => preg_replace("/\\ /", "", $cols[3]), "yomi" => $yomi)); } elseif ($numbers) { $street .= $cols[1]; if (0 < preg_match('/\\)/', $cols[1])) { $numbers = false; } if (!$numbers) { $matches = array(); if (0 < preg_match('/(.*)(\\([^\\(\\)]*\\))$/', $street, $matches)) { array_push($areas, array("zip" => $cols[0], "add1" => $matches[1], "add2" => $matches[2], "yomi" => $yomi)); } else { array_push($areas, array("zip" => $cols[0], "add1" => $street, "add2" => "", "yomi" => $yomi)); } $street = ""; } } else { array_push($areas, array("zip" => $cols[0], "add1" => $cols[1], "add2" => "", "yomi" => $yomi)); } } fclose($s); unlink_files(array($src)); if (0 < count($floors)) { update_html_floores($tmp, $code, $city_name, $floors, $appendix); } }