| 1 |
/*
|
| 2 |
* =========================================================================
|
| 3 |
* unblock_dlg - A program to add newlines to an optional-format USGS DLG file.
|
| 4 |
* Copyright (c) 1997,2000 Fred M. Erickson
|
| 5 |
*
|
| 6 |
* This program is free software; you can redistribute it and/or modify
|
| 7 |
* it under the terms of the GNU General Public License as published by
|
| 8 |
* the Free Software Foundation; either version 2, or (at your option)
|
| 9 |
* any later version.
|
| 10 |
*
|
| 11 |
* This program is distributed in the hope that it will be useful,
|
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 14 |
* GNU General Public License for more details.
|
| 15 |
*
|
| 16 |
* You should have received a copy of the GNU General Public License
|
| 17 |
* along with this program; if not, write to the Free Software
|
| 18 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
| 19 |
* =========================================================================
|
| 20 |
*
|
| 21 |
* This program unblocks the information in an optional-format DLG file
|
| 22 |
* by adding a newline to each record.
|
| 23 |
* It sets the last byte in each record (which should always be
|
| 24 |
* a blank) to a newline.
|
| 25 |
*
|
| 26 |
* The program reads from stdin and writes to stdout.
|
| 27 |
*/
|
| 28 |
#include <stdio.h>
|
| 29 |
#include <sys/types.h>
|
| 30 |
#include "drawmap.h"
|
| 31 |
#include "dlg.h"
|
| 32 |
|
| 33 |
void
|
| 34 |
license(void)
|
| 35 |
{
|
| 36 |
fprintf(stderr, "This program is free software; you can redistribute it and/or modify\n");
|
| 37 |
fprintf(stderr, "it under the terms of the GNU General Public License as published by\n");
|
| 38 |
fprintf(stderr, "the Free Software Foundation; either version 2, or (at your option)\n");
|
| 39 |
fprintf(stderr, "any later version.\n\n");
|
| 40 |
|
| 41 |
fprintf(stderr, "This program is distributed in the hope that it will be useful,\n");
|
| 42 |
fprintf(stderr, "but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
|
| 43 |
fprintf(stderr, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
|
| 44 |
fprintf(stderr, "GNU General Public License for more details.\n\n");
|
| 45 |
|
| 46 |
fprintf(stderr, "You should have received a copy of the GNU General Public License\n");
|
| 47 |
fprintf(stderr, "along with this program; if not, write to the Free Software\n");
|
| 48 |
fprintf(stderr, "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n");
|
| 49 |
}
|
| 50 |
|
| 51 |
main(int argc, char *argv[])
|
| 52 |
{
|
| 53 |
unsigned char buf[DLG_RECORD_LENGTH];
|
| 54 |
int start_flag = 0;
|
| 55 |
int ret_val;
|
| 56 |
int i;
|
| 57 |
|
| 58 |
if ((argc == 2) && (argv[1][0] == '-') && (argv[1][1] == 'L')) {
|
| 59 |
license();
|
| 60 |
exit(0);
|
| 61 |
}
|
| 62 |
else if (argc != 1) {
|
| 63 |
fprintf(stderr, "Usage: %s < optional_format_dlg_file.opt\n", argv[0]);
|
| 64 |
exit(0);
|
| 65 |
}
|
| 66 |
|
| 67 |
while ((ret_val = read(0, buf, DLG_RECORD_LENGTH)) == DLG_RECORD_LENGTH) {
|
| 68 |
if (start_flag == 0) {
|
| 69 |
/*
|
| 70 |
* Check for newlines in the first read
|
| 71 |
* to try to prevent people from converting files
|
| 72 |
* that already have newlines in them.
|
| 73 |
*/
|
| 74 |
for (i = 0; i < DLG_RECORD_LENGTH; i++) {
|
| 75 |
if (buf[i] == '\n') {
|
| 76 |
fprintf(stderr, "This file already has newlines in it. Aborting.\n");
|
| 77 |
exit(0);
|
| 78 |
}
|
| 79 |
}
|
| 80 |
start_flag = 1;
|
| 81 |
}
|
| 82 |
// if (buf[DLG_RECORD_LENGTH - 1] != ' ') {
|
| 83 |
// /*
|
| 84 |
// * According to the standard, bytes 73-80 of each record are
|
| 85 |
// * either blank (which seems to usually be the case) or can
|
| 86 |
// * contain a record sequence number. This if-block assumes
|
| 87 |
// * that there is always a blank in byte 80, and checks for
|
| 88 |
// * record sanity based on that assumption. Of course, the
|
| 89 |
// * check will erroneously fail if there is a record sequence
|
| 90 |
// * number.
|
| 91 |
// */
|
| 92 |
// fprintf(stderr, "This file may have formatting problems. Aborting.\n");
|
| 93 |
// exit(0);
|
| 94 |
// }
|
| 95 |
buf[DLG_RECORD_LENGTH - 1] = '\n';
|
| 96 |
write(1, buf, ret_val);
|
| 97 |
}
|
| 98 |
|
| 99 |
if ((ret_val != 0) && (ret_val != DLG_RECORD_LENGTH)) {
|
| 100 |
fprintf(stderr, "read() returned %d\n", ret_val);
|
| 101 |
exit(0);
|
| 102 |
}
|
| 103 |
}
|