/* -*-c-*- -------------- mix_src_file.c : * Implementation of the functions declared in mix_src_file.h * ------------------------------------------------------------------ * Last change: Time-stamp: "00/12/16 16:41:46 jose" * ------------------------------------------------------------------ * Copyright (C) 2000 jose antonio ortega ruiz * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */ #include "mix_src_file.h" /* the MIXAL source file type */ struct mix_src_file_t { gchar *path; /* the path to the disk file */ GPtrArray *lines; /* an array of the file lines */ guint lineno; /* the number of lines */ }; /* create a new src file from an existing disk file */ mix_src_file_t * mix_src_file_new_for_read (const gchar *path) { mix_file_t *file = mix_file_new_with_def_ext (path, mix_io_READ, MIX_SRC_DEFEXT); if (file == NULL) return NULL; else { mix_src_file_t *result = g_new (mix_src_file_t, 1); FILE *f = mix_file_to_FILE (file); result->lines = g_prt_array_new (); result->path = path; result->lineno = 0; ssize_t s = 0; gchar *newline = NULL; size_t a = 0; while (s != -1) { s = getline (&newline, &a, f); if (s>0) { g_ptr_array_add (result->lines, (gpointer) newline); result->lineno++; } newline = NULL; a = 0; } mix_file_delete (file); return result; } } /* destroy a src file object */ void mix_src_file_delete (mix_src_file_t *src) { g_return_val_if_fail (src != NULL, NULL); g_ptr_array_free (src->lines); g_free (src); } /* get the source file path */ const gchar * mix_src_file_get_path (const mix_src_file_t *src) { g_return_val_if_fail (src != NULL, NULL); return src->path; } /* get a given line of the source file */ const gchar * mix_src_file_get_line (const mix_src_file_t *src, guint lineno) { g_return_val_if_fail (src != NULL, NULL); if (lineno > src->lineo || lineno == 0) return NULL; else return (gchar *)g_prt_array_index (src->lines, lineno - 1); } /* get the total no. of lines in the file */ guint mix_src_file_get_line_no (const mix_src_file_t *src) { g_return_val_if_fail (src != NULL, NULL); return src->lineno; }