/* I/O support routines. */ /* Sys V/BSD difference. */ #ifdef CUBE #include #else #include #endif #include #include "gotoh_ds.h" extern int open(); extern int read(); /* Read target sequence. */ int get_target(target_file, target_buf) char *target_file; char *target_buf; { int t_fd; int t_length; t_fd = open(target_file, O_RDONLY); if (t_fd < 0) { fprintf(stderr, "%s (%d): cannot read target file %s.\n", __FILE__, __LINE__, target_file); lexit(1); } t_length = read(t_fd, target_buf, MAX_LENGTH); close(t_fd); return t_length; } /* Functions to manage a database file. This code is more complicated than you might expect. In order to reduce the number of i/o operations, the file format is such that one read gets one sequence and the length of the next sequence: [id0 len1][id len_next_seq seq]*[idn 0 seqn] The id and length fields constitue the header. Note that the first sequence is a null sequence. It's header provides the length of the first real sequence. The user supplied buffer must be big enough to hold seq data and the header.*/ static int db_fd; static int len_next_seq; extern int atoi(); int open_db(db_file) char *db_file; { char header[HEADER_LENGTH+1]; header[HEADER_LENGTH] = 0; /* Open database file. */ db_fd = open(db_file, O_RDONLY); if (db_fd < 0) { fprintf(stderr, "%s (%d): cannot read data base file %s.\n", __FILE__, __LINE__, db_file); lexit(1); } /* Read header of initial null sequence. Something's wrong if a whole header cannot be read. */ if (read(db_fd, header, HEADER_LENGTH) < HEADER_LENGTH) { fprintf(stderr, "%s (%d): corrupt header.\n", __FILE__, __LINE__); lexit(1); } len_next_seq = atoi(header + LENGTH_OFFSET); } int close_db() { close(db_fd); } int get_seq(buf) char *buf; { int seq_len; if (!len_next_seq) return 0; /* Read sequence. */ seq_len = len_next_seq; len_next_seq += HEADER_LENGTH; if (len_next_seq != read(db_fd, buf, len_next_seq)) { fprintf(stderr, "%s (%d): corrupt sequence.\n", __FILE__, __LINE__); lexit(1); } len_next_seq = atoi(buf + LENGTH_OFFSET); return seq_len; }