Book: replace Write* macros with functions.
[gnushogi.git] / gnushogi / book.c
index 6df98e6..0e95607 100644 (file)
@@ -576,46 +576,45 @@ static ULONG currentoffset;
 
 #define MAXOFFSET(B) ((B.booksize - 1) * sizeof_gdxdata + sizeof_gdxadmin)
 
-#define HashOffset(hashkey, B) \
-{ \
-  currentoffset = ((ULONG)hashkey % B.booksize) \
-    * sizeof_gdxdata + sizeof_gdxadmin; \
+static void HashOffset(ULONG hashkey, struct gdxadmin *B)
+{
+    currentoffset = (hashkey % B->booksize) * sizeof_gdxdata + sizeof_gdxadmin;
 }
 
 
-#define NextOffset(B) \
-{ \
-  currentoffset += sizeof_gdxdata; \
-  if (currentoffset > B.maxoffset) \
-    currentoffset = sizeof_gdxadmin; \
+static void NextOffset(struct gdxadmin *B)
+{
+    currentoffset += sizeof_gdxdata;
+    if (currentoffset > B->maxoffset)
+        currentoffset = sizeof_gdxadmin;
 }
 
 
-#define WriteAdmin() \
-{ \
-  lseek(gfd, 0, 0); \
-  write(gfd, (char *)&ADMIN, sizeof_gdxadmin); \
+static void WriteAdmin(void)
+{
+    lseek(gfd, 0, SEEK_SET);
+    write(gfd, (char *)&ADMIN, sizeof_gdxadmin);
 }
 
-#define WriteData() \
-{ \
-  if (mustwrite ) \
-  { \
-    lseek(gfd, currentoffset, 0); \
-    write(gfd, (char *)&DATA, sizeof_gdxdata); \
-    mustwrite = false; \
-  } \
+static void WriteData(int *mustwrite)
+{
+    if (*mustwrite)
+    {
+        lseek(gfd, currentoffset, SEEK_SET);
+        write(gfd, (char *)&DATA, sizeof_gdxdata);
+        *mustwrite = false;
+    }
 }
 
 static int ReadAdmin(void)
 {
-    lseek(gfd, 0, 0);
+    lseek(gfd, 0, SEEK_SET);
     return (sizeof_gdxadmin == read(gfd, (char *)&ADMIN, sizeof_gdxadmin));
 }
 
 static int ReadData(struct gdxdata *DATA)
 {
-    lseek(gfd, currentoffset, 0);
+    lseek(gfd, currentoffset, SEEK_SET);
     return (sizeof_gdxdata == read(gfd, (char *)DATA, sizeof_gdxdata));
 }
 
@@ -736,8 +735,8 @@ GetOpenings(void)
                              * exist from some other opening.
                              */
 
-                            WriteData();
-                            HashOffset(bhashkey, B);
+                            WriteData(&mustwrite);
+                            HashOffset(bhashkey, &B);
                             first = true;
 
                             while (true)
@@ -774,12 +773,12 @@ GetOpenings(void)
                                         {
                                             DATA.flags &= (~LASTMOVE);
                                             mustwrite = true;
-                                            WriteData();
+                                            WriteData(&mustwrite);
                                         }
                                     }
                                 }
 
-                                NextOffset(B);
+                                NextOffset(&B);
                                 first = false;
                             }
 
@@ -821,14 +820,14 @@ GetOpenings(void)
                 {
                     /* reset for next opening */
                     games++;
-                    WriteData();
+                    WriteData(&mustwrite);
                     RESET();
                     i = 0;
                     side = black;
                 }
             }
 
-            WriteData();
+            WriteData(&mustwrite);
             fclose(fd);
             /* write admin rec with counts */
             ADMIN.bookcount = B.bookcount;
@@ -923,7 +922,7 @@ OpeningBook(unsigned short *hint)
         }
 
         x = 0;
-        HashOffset(hashkey, B);
+        HashOffset(hashkey, &B);
 #ifdef BOOKTEST
         printf("looking for book move, bhashbd = 0x%lx bhashkey = 0x%x\n",
                (ULONG)hashbd, HashValue(hashkey));
@@ -949,7 +948,7 @@ OpeningBook(unsigned short *hint)
                     break;
             }
 
-            NextOffset(B);
+            NextOffset(&B);
         }
 
 #ifdef BOOKTEST