view tests/agcl/parsifal/sort.syn @ 9:60b08b68c750

Switch to static inline as an expedient build fix. Should probably set this up with working C99 inline but for the moment I don't have the energy.
author David A. Holland
date Mon, 30 May 2022 23:56:45 -0400
parents 13d2b8934445
children
line wrap: on
line source

{
  class String {
    char *value;
  public:
    String();
    String(unsigned char *);
    int operator < (const String &);
  };
}

char = 33..127 - '<' - '>'
blank = 32 + '\t'
eof = 0

[
  context type = unsigned char *
]

(unsigned char *) words
 -> word                          =PCB.pointer;
 -> words, blank..., word         =PCB.pointer;

word = char...

(String) text
 -> words:p, blank?...            =String(p);

(String) address
 -> '<', blank?..., words:w, blank?..., '>'          =w;

line
 -> blank?..., text:t                                  =map[t] = t;
 -> blank?..., text, address:a                         =map[a] = String();
 -> blank?..., address:a, text                         =map[a] = String();

grammar
 -> line?..., eof

{
  #include <string.h>
  #include <stdio.h>
  #include "agmap.h"


  #define GET_CONTEXT CONTEXT = PCB.pointer

  AgMap<String, String> map;

  String::String() : value(new char[PCB.pointer - CONTEXT + 1]) {
    int length = PCB.pointer - CONTEXT;
    strncpy(value, CONTEXT, length);
    value[length] = 0;
  }

  String::String(unsigned char *p) : value(new char[p - CONTEXT + 1]) {
    int length = p - CONTEXT;
    strncpy(value, CONTEXT, length);
    value[length] = 0;
  }

  int operator < (const String &s) {
    return strcmp(value, s.value) < 0;
  }

  enum ConditionCode {
    success,
    incorrectArgumentCount,
    cannotOpenInput,
    cannotSeek,
    ftellFailure,
    insufficientMemory,
    readError,
    parseError
  };

 int main(int argc, char *argv[]) {

    /* Check for enough arguments */
    if (argc != 2) {
      printf("Usage: %s <filename>\n", argv[0]);
      return incorrectArgumentCount;
    }

    /* Open input file */
    FILE *input = fopen(argv[1],"r");
    if (input == NULL) {
      printf("Cannot open %s\n", argv[1]);
      return cannotOpenInput;
    }

    /* find out how big the file is */
    if (fseek(input, SEEK_SET, SEEK_END)) {
      printf("Seek error on %s\n", argv[1]);
      return cannotSeek;
    }
    long fileLength = ftell(input);
    if (fileLength < 0 ) {    // -1L is error return
      printf("Error getting file length (%d) of %s\n", fileLength, argv[1]);
      return ftellFailure;
    }
    /* fseek to beginning of file */
    if (fseek(input, 0, SEEK_SET)) {
      printf("Seek error on %s\n", argv[1]);
      return cannotSeek;
    }

    /* Allocate storage for input string */
    char *scriptString = new char[(size_t) fileLength + 1];
    if (scriptString == NULL) {
      printf("Insufficient memory\n");
      return insufficientMemory;
    }

    /* read file */
    size_t stringLength = fread(scriptString, 1, (unsigned)fileLength, input);
    if (stringLength == 0) {
      printf("Unable to read %s\n", argv[1]);
      delete [] scriptString;
      fclose(input);
      return readError;
    }
    scriptString[stringLength] = 0;            // Terminate string with null
    fclose(input);

    PCB.pointer = scriptString;
    sort();



    return success;
  }

}