summaryrefslogtreecommitdiffhomepage
path: root/mixlib/mix_src_file.c
blob: c6e1be03e948599a8fe0a872aeebf5a3b2eca58f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* -*-c-*- -------------- mix_src_file.c :
 * Implementation of the functions declared in mix_src_file.h
 * ------------------------------------------------------------------
 *  Last change: Time-stamp: "00/12/17 22:46:27 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 <stdio.h>
#include <unistd.h>
#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 */
};
  
/* load the source file lines into memory */
static gboolean
load_file_ (mix_src_file_t *file)
{
  mix_file_t *mf = mix_file_new_with_def_ext (file->path,
					      mix_io_READ,
					      MIX_SRC_DEFEXT);
  if (mf != NULL)
    {
      enum {BUFFER_SIZE = 256};
      static gchar BUFFER[BUFFER_SIZE];
      
      FILE  *f = mix_file_to_FILE (mf);
      file->lines = g_ptr_array_new ();
      file->lineno = 0;
      
      while (fgets (BUFFER, BUFFER_SIZE, f) == BUFFER)
	{
	  g_ptr_array_add (file->lines, (gpointer) g_strdup (BUFFER));
	  file->lineno++;
	}
      
      mix_file_delete (mf);
      return TRUE;
    }
  return FALSE;
}

/* create a new src file from an existing disk file */
mix_src_file_t *
mix_src_file_new_for_read (const gchar *path)
{
  mix_src_file_t *result = g_new (mix_src_file_t, 1);
  result->lines = NULL;
  result->path = g_strdup (path);
  result->lineno = 0;
  return result;
}

/* destroy a src file object */
void
mix_src_file_delete (mix_src_file_t *src)
{
  g_return_if_fail (src != NULL);
  if (src->lines) g_ptr_array_free (src->lines, TRUE);
  g_free (src->path);
  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 (src->lines == NULL && !load_file_ ((mix_src_file_t*)src))
    return NULL;
  if (lineno > src->lineno || lineno == 0)
    return NULL;
  return (gchar *)g_ptr_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, 0);
  if (src->lines == NULL && !load_file_ ((mix_src_file_t*)src))
    return 0;
  return src->lineno;
}