Source file: /~heha/hs/gputils64-210929.zip/gpasm/file.cpp

/* Source file functions for gpasm
    Copyright 2016	Molnár Károly
*/

#include "stdhdr.h"

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

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

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

  if (!Node) 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(file_types Type, const char *Name) {
  static unsigned  file_id = 0;

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

  pnew = new file_context_t;

  pnew->name = GP_Strdup(Name);
  pnew->ft   = Type;
  pnew->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, pnew);
  return pnew;
}

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

void file_search_paths(source_context_t *Context, const char *Name) {
  for (int i = 0; i < state.num_paths; i++) {
    unsigned len= strlen(state.paths[i])
		+ strlen(PATH_SEPARATOR_STR)
		+ strlen(Name) + 1;
    char*full_name = new char[len];
    snprintf(full_name, len, "%s" PATH_SEPARATOR_STR "%s", state.paths[i], Name);
    Context->f = fopen(full_name, "rt");
    if (Context->f) {
      Context->name = full_name;
      break;
    }else delete[] full_name;
  }
}

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

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

void file_free(void) {
  gp_list_delete(&state.file_list);
}
Detected encoding: UTF-80