summaryrefslogtreecommitdiffhomepage
path: root/mixlib/mix_src_file.c
blob: 0569aa67481c296652259da03df080a2794b64b2 (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
/* -*-c-*- -------------- mix_src_file.c :
 * Implementation of the functions declared in mix_src_file.h
 * ------------------------------------------------------------------
 *  Last change: Time-stamp: "00/12/17 12:56:50 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 */
};
  

/* 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
    {
      enum {BUFFER_SIZE = 256};
      static gchar BUFFER[BUFFER_SIZE];
      
      mix_src_file_t *result = g_new (mix_src_file_t, 1);
      FILE  *f = mix_file_to_FILE (file);
      result->lines = g_ptr_array_new ();
      result->path = g_strdup (path);
      result->lineno = 0;
      
      while (fgets (BUFFER, BUFFER_SIZE, f) == BUFFER)
	{
	  g_ptr_array_add (result->lines, (gpointer) g_strdup (BUFFER));
	  result->lineno++;
	}
      
      mix_file_delete (file);
      return result;
    }
}

/* destroy a src file object */
void
mix_src_file_delete (mix_src_file_t *src)
{
  g_return_if_fail (src != NULL);
  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 (lineno > src->lineno || lineno == 0)
    return NULL;
  else
    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);
  return src->lineno;
}