view tests/agcl/parsifal/sort.syn @ 6:607e3be6bad8

Adjust to the moving target called the C++ standard. Apparently nowadays it's not allowed to define an explicit copy constructor but not an assignment operator. Consequently, defining the explicit copy constructor in terms of the implicit/automatic assignment operator for general convenience no longer works. Add assignment operators. Caution: not tested with the IBM compiler, but there's no particular reason it shouldn't work.
author David A. Holland
date Mon, 30 May 2022 23:46:22 -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;
  }

}