Browse Source

Add: Versioning and almost completed all group_sprint functions

Sebastian Kreisel 1 year ago
parent
commit
51e6996f2b
6 changed files with 84 additions and 22 deletions
  1. 3 0
      Makefile
  2. 1 0
      VERSION.txt
  3. 3 0
      compile.sh
  4. 48 8
      src/group/group_io.c
  5. 10 8
      src/group/group_io.h
  6. 19 6
      src/main.c

+ 3 - 0
Makefile

@@ -15,6 +15,9 @@ ifeq ($(BOUNDS_CHECK), 1)
 CC_FLAGS += -DBOUNDS_CHECK
 endif
 
+VERSION = undefined
+CC_FLAGS += -DVERSION='"$(VERSION)"'
+
 # ----------------------------------------------------------------------------
 
 INCLUDES = -I src/

+ 1 - 0
VERSION.txt

@@ -0,0 +1 @@
+1.0.1

+ 3 - 0
compile.sh

@@ -0,0 +1,3 @@
+#!/bin/bash
+version=$(cat "./VERSION.txt")
+make BOUNDS_CHECK=1 VERSION=$version

+ 48 - 8
src/group/group_io.c

@@ -6,6 +6,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <math.h>
+#include <time.h>
 
 
 static const f64 baseLog16 = 1.0 / log(16.0);
@@ -46,25 +47,64 @@ i32 group_sprintGTab(char *buf, Group *group)
   return offs;
 }
 
-i32 group_sprintHash(char *buf, char *gtabBuf)
+i32 group_sprintFileName(char *buf, Group* group, u64 hash)
 {
-  u64 hash = hash_djb2(gtabBuf);
-  return hash_sprintHash(buf, hash);
+  u32 offs = 0;
+  char commFlag = group_isCommutative(group) ? 'c' : 'n';
+  offs += sprintf(buf + offs, "%05i%c_", group_order(group), commFlag);
+  offs += hash_sprintHash(buf + offs, hash);
+  offs += sprintf(buf + offs, ".txt");
+  return offs;
 }
 
-i32 group_sprintFileName(char *buf, Group* group, char *gtabBuf)
+i32 group_sprintHeader(char *buf, GroupMetaInfo meta)
 {
-  u32 offs = sprintf(buf, "%05i_", group_order(group));
-  offs += group_sprintHash(buf + offs, gtabBuf);
-  offs += sprintf(buf + offs, ".txt");
+  i32 offs = group_sprintPreamble(buf);
+  offs += sprintf(buf + offs, "\n#\n");
+  offs += group_sprintFileMeta(buf + offs);
+  offs += sprintf(buf + offs, "\n#\n");
+  offs += group_sprintGroupMeta(buf + offs, meta);
+  offs += sprintf(buf + offs, "\n#\n#\n#");
   return offs;
 }
 
 i32 group_sprintPreamble(char *buf)
 {
-  return 0;
+  return sprintf(buf, "# This file is generated by ascogen and contains the"
+                      " multiplication table of a\n"
+                      "# finite group. For more information please visit"
+                      " https://elfeck.com/asocgen.");
+}
+
+i32 group_sprintFileMeta(char *buf)
+{
+  time_t t = time(0);
+  struct tm tm = *localtime(&t);
+  i32 offs = 0;
+  offs += sprintf(buf + offs, "# Version: %s\n", VERSION);
+  offs += sprintf(buf + offs, "# File Created: %d-%d-%d", tm.tm_year + 1900,
+                  tm.tm_mon + 1, tm.tm_mday);
+  return offs;
 }
 
+i32 group_sprintGroupMeta(char *buf, GroupMetaInfo meta)
+{
+  i32 offs = 0;
+  offs += sprintf(buf + offs, "# Group Name: %s\n", meta.name);
+  offs += sprintf(buf + offs, "# Group djb2-Hash: ");
+  offs += hash_sprintHash(buf + offs, meta.djb2Hash);
+  offs += sprintf(buf + offs, "\n");
+  offs += sprintf(buf + offs, "# Group Order: %i\n", meta.order);
+  offs += sprintf(buf + offs, "# Group Commutative: %i\n", meta.isCommutative);
+  offs += sprintf(buf + offs, "# Group minGenSet: ");
+  return offs;
+}
+
+
+// ---------------------------------------------------------------------------
+// Reading
+// ---------------------------------------------------------------------------
+
 void group_readFromFile(char *path)
 {
 }

+ 10 - 8
src/group/group_io.h

@@ -4,16 +4,14 @@
 #include "group.h"
 
 
-struct GroupMetaData {
-  // Write specific
-  char *date;
-  char *version;
-  // Group specific
+struct GroupMetaInfo {
   char *name;
+  u64 djb2Hash;
+  u16 order;
   bool isCommutative;
   Vecu16 *minGenSet;
 };
-typedef struct GroupMetaData GroupMetaData;
+typedef struct GroupMetaInfo GroupMetaInfo;
 
 /*
  * Tries to write the group to a file according to the following procedure:
@@ -45,8 +43,12 @@ bool group_writeToFile(Group *group, char *path);
 bool group_writeIndexedToFile(Group *group, char *path);
 
 i32 group_sprintGTab(char *buf, Group *group);
-i32 group_sprintHash(char *buf, char *gtabBuf);
-i32 group_sprintFileName(char *buf, Group* group, char *gtabBuf);
+i32 group_sprintFileName(char *buf, Group* group, u64 hash);
+i32 group_sprintHeader(char *buf, GroupMetaInfo meta);
+
+i32 group_sprintPreamble(char *buf);
+i32 group_sprintFileMeta(char *buf);
+i32 group_sprintGroupMeta(char *buf, GroupMetaInfo meta);
 
 Group* group_readFromFile_alloc(char *path);
 

+ 19 - 6
src/main.c

@@ -6,6 +6,7 @@
 #include <elfc_perm.h>
 #include <elfc_veci32.h>
 #include <elfc_mapu16.h>
+#include <elfc_hash.h>
 
 #include "group/group.h"
 #include "group/group_common.h"
@@ -19,6 +20,7 @@ i32 main_3();
 i32 main_2();
 i32 main_1();
 
+
 i32 main()
 {
   return main_5();
@@ -32,20 +34,31 @@ i32 main_5()
   f64 baseLog16 = 1.0 / log(16.0);
   u16 maxEleLen = floor(log((f64) n) * baseLog16) + 1;
   char *buf = malloc(n * n * maxEleLen + 100);
-  char *hashBuf = malloc(30);
   char *fnameBuf = malloc(100);
-  hashBuf[0] = '\0';
+  char *headerBuf = malloc(1000);
   buf[0] = '\0';
   fnameBuf[0] = '\0';
+  headerBuf[0] = '\0';
   group_sprintGTab(buf, c);
-  group_sprintHash(hashBuf, buf);
-  group_sprintFileName(fnameBuf, c, buf);
-  printf("%s\n", hashBuf);
+
+  u64 hash = hash_djb2(buf);
+
+  group_sprintFileName(fnameBuf, c, hash);
+
+  GroupMetaInfo cInfo;
+  cInfo.name ="S5";
+  cInfo.djb2Hash = hash;
+  cInfo.order = group_order(c);
+  cInfo.isCommutative = group_isCommutative(c);
+  cInfo.minGenSet = 0;
+  group_sprintHeader(headerBuf, cInfo);
+
   printf("%s\n", fnameBuf);
+  printf("\n%s\n", headerBuf);
 
+  free(headerBuf);
   free(fnameBuf);
   free(buf);
-  free(hashBuf);
 
   group_free(c);
   return 0;