File: file.c

package info (click to toggle)
gputils 1.5.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 159,960 kB
  • sloc: pascal: 1,459,440; ansic: 319,705; sh: 4,323; makefile: 2,134; lex: 1,755; yacc: 1,595
file content (115 lines) | stat: -rw-r--r-- 2,870 bytes parent folder | download | duplicates (2)
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
115
/* Source file functions for gpasm

    Copyright (C) 2016 Molnar Karoly

This file is part of gputils.

gputils 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, or (at your option)
any later version.

gputils 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 gputils; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

#include "stdhdr.h"

#include "libgputils.h"
#include "gpasm.h"

/*------------------------------------------------------------------------------------------------*/

void
file_delete_node(void *Node)
{
  file_context_t *n;

  if (Node == NULL) {
    return;
  }

  n = (file_context_t *)Node;
  free(n->name);
  free(n);
}

/*------------------------------------------------------------------------------------------------*/

/* file_add: Add a file of type 'type' to the file_context stack. */

file_context_t *
file_add(unsigned int Type, const char *Name)
{
  static unsigned int  file_id = 0;

  file_context_t      *new;

  /* First check to make sure this file is not already in the list. */
  new = state.file_list.last;
  while (new != NULL) {
    if (strcmp(new->name, Name) == 0) {
      return new;
    }
    new = new->prev;
  }

  new = gp_list_node_new(sizeof(*new));

  new->name = GP_Strdup(Name);
  new->ft   = Type;
  new->id   = file_id++;

  if (state.file_list.first == NULL) {
    gp_list_set_delete_node_func(&state.file_list, file_delete_node);
  }

  gp_list_node_append(&state.file_list, new);
  return new;
}

/*------------------------------------------------------------------------------------------------*/

void
file_search_paths(source_context_t *Context, const char *Name)
{
  char *full_name;
  int   i;
  int   len;

  full_name = NULL;
  for (i = 0; i < state.num_paths; i++) {
    len = snprintf(NULL, 0, "%s" PATH_SEPARATOR_STR "%s", state.paths[i], Name);
    assert(len > 0);

    ++len;
    full_name = GP_Realloc(full_name, (size_t)len);
    snprintf(full_name, (size_t)len, "%s" PATH_SEPARATOR_STR "%s", state.paths[i], Name);

    Context->f = fopen(full_name, "rt");
    if (Context->f != NULL) {
      Context->name = GP_Strdup(full_name);
      break;
    }
  }

  if (full_name != NULL) {
    free(full_name);
  }
}

/*------------------------------------------------------------------------------------------------*/

/* file_free: free memory allocated to the file_context stack */

void
file_free(void)
{
  gp_list_delete(&state.file_list);
}