diff options
Diffstat (limited to 'mixlib')
| -rw-r--r-- | mixlib/mix_src_file.c | 109 | ||||
| -rw-r--r-- | mixlib/mix_src_file.h | 57 | 
2 files changed, 166 insertions, 0 deletions
| diff --git a/mixlib/mix_src_file.c b/mixlib/mix_src_file.c new file mode 100644 index 0000000..b8c4356 --- /dev/null +++ b/mixlib/mix_src_file.c @@ -0,0 +1,109 @@ +/* -*-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 <jaortega@acm.org> + *   + * 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; +} + diff --git a/mixlib/mix_src_file.h b/mixlib/mix_src_file.h new file mode 100644 index 0000000..8f46111 --- /dev/null +++ b/mixlib/mix_src_file.h @@ -0,0 +1,57 @@ +/* -*-c-*- ---------------- mix_src_file.h : + * Declaration of mix_src_file_t, a type representing a MIXAL source + * file. + * ------------------------------------------------------------------ + *  Last change: Time-stamp: <00/12/16 15:06:57 jose> + * ------------------------------------------------------------------ + * Copyright (C) 2000 jose antonio ortega ruiz <jaortega@acm.org> + *   + * 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. + *   + */ + + +#ifndef MIX_SRC_FILE_H +#define MIX_SRC_FILE_H + +#include "mix_file.h" + +/* the MIXAL source file type */ +typedef struct mix_src_file_t mix_src_file_t; + +/* create a new src file from an existing disk file */ +extern mix_src_file_t * +mix_src_file_new_for_read (const gchar *path); + +/* destroy a src file object */ +extern void +mix_src_file_delete (mix_src_file_t *src); + +/* get the source file path */ +extern const gchar * +mix_src_file_get_path (const mix_src_file_t *src); + +/* get a given line of the source file */ +extern const gchar * +mix_src_file_get_line (const mix_src_file_t *src, guint lineno); + +/* get the total no. of lines in the file */ +extern guint +mix_src_file_get_line_no (const mix_src_file_t *src); + + + +#endif /* MIX_SRC_FILE_H */ + | 
