lin-club  

r2llib-0.21 and biditext-mulix-0.07 "crawling earthworms" release

mulix
Sun, 05 Aug 2001 03:50:03 -0700

hello, clubbers

new r2llib, featuring guy's requested new and improved
void r2llib_query_state(r2llib_t rtl,
                        R2L_STATE* r2l_state,
                        BIDITEXT_BASE_STATE* bt_state);

and a new biditext, now using this function and achieving the same speed
as the old style biditext!

* please take a few minutes to look at the diff and spot any
embarassing bugs i may have made. thank you in advance. *

http://www.pointer.co.il/~mulix/r2l/r2llib-0.21.tar.gz
http://www.pointer.co.il/~mulix/r2l/biditext-mulix-0.07.tar.gz

r2llib Changelog:

0.21:
        biditext_base_state.c: get/set_biditext_base_state() uses
                r2l_query/set_state_internal()
        r2l_state.c: get/set_r2l_state() uses
                r2l_query/set_state_internal()
        file_ops.c: new function, create_file_with_size() creates a file
                with a certain initial size
        query_state.[hc]: new function, r2l_query_state_internal() and
                r2l_set_state_internal()
        r2llib.[hc]: new function, r2l_query_state()

biditext Changelog:

0.07:
        common.c: changed to use new & efficient r2l_query_state()

diff -ur r2llib-0.20/Changelog r2llib-0.21/Changelog
--- r2llib-0.20/Changelog       Fri Aug  3 23:51:34 2001
+++ r2llib-0.21/Changelog       Sun Aug  5 12:04:40 2001
@@ -1,3 +1,13 @@
+0.21:
+       biditext_base_state.c: get/set_biditext_base_state() uses
+               r2l_query/set_state_internal()
+       r2l_state.c: get/set_r2l_state() uses
+               r2l_query/set_state_internal()
+       file_ops.c: new function, create_file_with_size() creates a file
+               with a certain initial size
+       query_state.[hc]: new function, r2l_query_state_internal() and
+               r2l_set_state_internal()
+       r2llib.[hc]: new function, r2l_query_state()
 0.20:
        changed include guards from _FOO to FOO (emil)
        r2llib.[hc]: changed function names to r2l_xxx_xxx(emil)
diff -ur r2llib-0.20/Makefile r2llib-0.21/Makefile
--- r2llib-0.20/Makefile        Fri Aug  3 23:51:34 2001
+++ r2llib-0.21/Makefile        Sun Aug  5 12:04:40 2001
@@ -1,5 +1,5 @@
 PACKAGE                = r2llib
-VERSION        = 0.20
+VERSION        = 0.21
 CC             = gcc
 DEBUG          = -g
 PREFIX         = /usr/local
@@ -25,8 +25,8 @@
 BIN            = r2llib-test
 LIB_NAME       = r2l
 LIB            = lib$(LIB_NAME).a
-OBJS           = r2llib.o r2l_state.o biditext_base_state.o parse_argv.o 
r2llib_data.o file_ops.o
-DEPS           = r2llib.h r2l_state.h biditext_base_state.h parse_argv.h 
r2llib_data.h file_ops.h
+OBJS           = r2llib.o r2l_state.o biditext_base_state.o parse_argv.o 
+r2llib_data.o file_ops.o query_state.o
+DEPS           = r2llib.h r2l_state.h biditext_base_state.h parse_argv.h 
+r2llib_data.h file_ops.h query_state.h
 EXT_HEADERS    = r2llib.h
 BIN_OBJS       = main.c
 CONFIG_BIN     = $(PACKAGE)-config
@@ -38,7 +38,8 @@
  Doxyfile Makefile $(PACKAGE).spec.in $(PACKAGE).spec $(CONFIG_SRC)\
  biditext_base_state.c biditext_base_state.h file_ops.c file_ops.h \
  main.c parse_argv.c parse_argv.h r2l_state.c r2l_state.h \
- r2llib.c r2llib.h r2llib_data.c r2llib_data.h
+ r2llib.c r2llib.h r2llib_data.c r2llib_data.h query_state.h \
+ query_state.c
 # what about 'rev' ?
 TAR_BALL_DIR=$(PACKAGE)-$(VERSION)
 TAR_BALL=$(TAR_BALL_DIR).tar.gz
diff -ur r2llib-0.20/biditext_base_state.c r2llib-0.21/biditext_base_state.c
--- r2llib-0.20/biditext_base_state.c   Fri Aug  3 23:51:34 2001
+++ r2llib-0.21/biditext_base_state.c   Sun Aug  5 12:04:40 2001
@@ -5,63 +5,36 @@
 #include "r2llib.h"
 #include "biditext_base_state.h"
 #include "file_ops.h"
+#include "query_state.h"

 BIDITEXT_BASE_STATE
 set_biditext_base_state(const char* file_name,
                        BIDITEXT_BASE_STATE requested_state)
 {
-     BIDITEXT_BASE_STATE state;
-     int fd;
+     BIDITEXT_BASE_STATE bt = BT_BASE_ERROR;
+     R2L_STATE st = R2L_ERROR;

      if (!file_name || (requested_state == BT_BASE_ERROR))
          return BT_BASE_ERROR;

-     state = get_biditext_base_state(file_name);
-     if (state == requested_state)
-         return state;
-
-     fd = create_file(file_name);
-     if (fd == -1)
-         return BT_BASE_ERROR;
-
-     switch (requested_state){
-     case(BT_BASE_RTL):
-         if (write(fd, "aa", 2) != 2)
-              requested_state = BT_BASE_ERROR;
-         break;
-     case(BT_BASE_LTR):
-         if (write(fd, "a", 1) != 1)
-              requested_state = BT_BASE_ERROR;
-         break;
-     default:
-         assert(0*requested_state);
-         /* fallthrough */
-     case (BT_BASE_NEUTRAL):
-         if (truncate(file_name, 0) != 0)
-              requested_state = BT_BASE_ERROR;
-         break;
-     }
-
-     close(fd);
-
+     r2l_query_state_internal(file_name, &st, &bt);
+     if (bt == requested_state)
+         return requested_state;
+
+     /* we need to set the requested biditext state, while maintaining the
+       r2l state we had */
+     r2l_set_state_internal(file_name, &st, &requested_state);
      return requested_state;
 }

 BIDITEXT_BASE_STATE
 get_biditext_base_state(const char* file_name)
 {
-     int sz;
+     BIDITEXT_BASE_STATE bt = BT_BASE_ERROR;

      if (!file_name)
          return BT_BASE_ERROR;

-     sz = file_size(file_name);
-     if (sz <= 0)
-         return BT_BASE_NEUTRAL;
-
-     /* (sz % 2) == (sz & 0x0001) */
-     if (sz & 0x0001)
-         return BT_BASE_LTR;
-
-     return BT_BASE_RTL;
+     r2l_query_state_internal(file_name, NULL, &bt);
+     return bt;
 }
diff -ur r2llib-0.20/file_ops.c r2llib-0.21/file_ops.c
--- r2llib-0.20/file_ops.c      Fri Aug  3 23:51:34 2001
+++ r2llib-0.21/file_ops.c      Sun Aug  5 12:04:40 2001
@@ -96,6 +96,35 @@
      return buf.st_size;
 }

+int
+create_file_with_size(const char* file_name, unsigned int size)
+{
+     int rc;
+     int fd;
+     static char buf[16];
+
+     /* sanitize size */
+     size = ((size > 16) ? 16 : size);
+
+     fd = create_file(file_name);
+
+     if ((size == 0) || (fd == -1)){
+         return fd;
+     }
+
+     memset(buf, 'a', size);
+
+     rc = write(fd, buf, size);
+
+     if (rc < size){
+         close(fd);
+         delete_file(file_name);
+         return -1;
+     }
+
+     return fd;
+}
+

 #if 0 /* not used right now */
 /* writes 'token' (a null terminated string) to the file
diff -ur r2llib-0.20/file_ops.h r2llib-0.21/file_ops.h
--- r2llib-0.20/file_ops.h      Fri Aug  3 23:51:34 2001
+++ r2llib-0.21/file_ops.h      Sun Aug  5 12:04:40 2001
@@ -35,6 +35,15 @@
 int
 file_size(const char* file_name);

+/**
+ * Create a file with size 'size'
+ * @param file_name the name of the file to create
+ * @return the file descriptor or -1 on error
+ */
+int
+create_file_with_size(const char* file_name, unsigned int size);
+
+
 #if 0 /* not used right now */
 /* writes 'token' (a null terminated string) to the file
    return 1 on succes, -1 on errors */
diff -ur r2llib-0.20/query_state.c r2llib-0.21/query_state.c
--- r2llib-0.20/query_state.c   Sun Aug  5 11:31:11 2001
+++ r2llib-0.21/query_state.c   Sun Aug  5 12:04:40 2001
@@ -0,0 +1,107 @@
+#include <assert.h>
+#include <unistd.h>
+
+#include "r2llib.h"
+#include "query_state.h"
+#include "file_ops.h"
+
+void
+r2l_query_state_internal(const char* file_name,
+                        R2L_STATE* r2l_state,
+                        BIDITEXT_BASE_STATE* bt_state)
+{
+     R2L_STATE st  = R2L_ERROR;
+     BIDITEXT_BASE_STATE bt = BT_BASE_ERROR;
+
+     /* from guy's email:
+
+       size:   r2l_mode      biditext base direction
+       ----------------------------------------------
+       0       enabled       neutral
+       1       disabled      neutral
+       2       enabled       rtl
+       3       disabled      rtl
+       4       enabled       ltr
+       5       disabled      ltr
+
+     */
+
+     int sz = file_size(file_name);
+
+     /* we handle the 'file not found' case the same way as the size 1 case */
+     if (sz == -1)
+         sz = 1;
+
+     switch(sz){
+     case(0):
+         st = R2L_ENABLED; bt = BT_BASE_NEUTRAL; break;
+     case(1):
+         st = R2L_DISABLED;  bt = BT_BASE_NEUTRAL; break;
+     case(2):
+         st = R2L_ENABLED; bt = BT_BASE_RTL; break;
+     case(3):
+         st = R2L_DISABLED;  bt = BT_BASE_RTL; break;
+     case(4):
+         st = R2L_ENABLED; bt = BT_BASE_LTR; break;
+     case(5):
+         st = R2L_DISABLED;  bt = BT_BASE_LTR; break;
+     default:
+         assert(sz * 0);
+     }
+
+     if (r2l_state)
+         *r2l_state = st;
+
+     if (bt_state)
+         *bt_state = bt;
+}
+
+
+void r2l_set_state_internal(const char* file_name,
+                           R2L_STATE* st,
+                           BIDITEXT_BASE_STATE* bt)
+{
+     unsigned int sz = 0;
+     int fd;
+
+     assert(file_name && st && bt);
+
+     switch (*st){
+     case(R2L_ENABLED): /* do nothing */
+         break;
+     case(R2L_DISABLED):
+         sz |= 0x0001; /* turn bit 1 on */
+         break;
+     case (R2L_ERROR): /* fallthrough */
+     default:
+         assert(*st * 0);
+     }
+
+     switch(*bt){
+     case(BT_BASE_NEUTRAL): /* do nothing */
+         break;
+     case (BT_BASE_RTL):
+         sz |= 0x0002; /* turn bit 2 on */
+         break;
+     case (BT_BASE_LTR):
+         sz |= 0x0004; /* turn bit 3 on */
+         break;
+     case (BT_BASE_ERROR): /* fallthrough */
+     default:
+         assert(*bt * 0);
+     }
+
+     if ((fd = create_file_with_size(file_name, sz)) == -1){
+         goto error;
+     }
+
+     if (close(fd) == -1){
+         goto error;
+     }
+
+     return;
+
+ error:
+     *st = R2L_ERROR;
+     *bt = BT_BASE_ERROR;
+}
diff -ur r2llib-0.20/query_state.h r2llib-0.21/query_state.h
--- r2llib-0.20/query_state.h   Sun Aug  5 11:31:10 2001
+++ r2llib-0.21/query_state.h   Sun Aug  5 12:04:40 2001
@@ -0,0 +1,15 @@
+#ifndef QUERY_STATE_H_
+#define QUERY_STATE_H_
+
+#include "r2llib.h"
+
+/** query the r2l state and biditext base state in one call */
+void r2l_query_state_internal(const char* file_name,
+                             R2L_STATE* st,
+                             BIDITEXT_BASE_STATE* bt);
+
+void r2l_set_state_internal(const char* file_name,
+                           R2L_STATE* st,
+                           BIDITEXT_BASE_STATE* bt);
+
+#endif /* QUERY_STATE_H_ */
diff -ur r2llib-0.20/r2l_state.c r2llib-0.21/r2l_state.c
--- r2llib-0.20/r2l_state.c     Fri Aug  3 23:51:34 2001
+++ r2llib-0.21/r2l_state.c     Sun Aug  5 12:04:40 2001
@@ -5,38 +5,26 @@

 #include "r2l_state.h"
 #include "file_ops.h"
+#include "query_state.h"


 R2L_STATE
 set_r2l_state(const char* file_name, R2L_STATE requested_state)
 {
      R2L_STATE state = R2L_ERROR;
+     BIDITEXT_BASE_STATE bt = BT_BASE_NEUTRAL;

      if (!file_name || (requested_state == R2L_ERROR))
          return R2L_ERROR;

-     state = get_r2l_state(file_name);
+     r2l_query_state_internal(file_name, &state, &bt);
      if (state == requested_state)
          return state;

-     if (requested_state == R2L_ENABLED){
-         int fd;
-         /* create_file can return the 0 fd too */
-         if ((fd = create_file(file_name)) != -1){
-              close(fd);
-              state = R2L_ENABLED;
-         } else {
-              state = R2L_ERROR;
-         }
-     } else { /* request is disable hebrew */
-         if (delete_file(file_name) != -1){
-              state = R2L_DISABLED;
-         } else {
-              state = R2L_ERROR;
-         }
-     }
-
-     return state;
+     /* we need to set the request r2l state, while maintaining the
+       biditext base state we had */
+     r2l_set_state_internal(file_name, &requested_state, &bt);
+     return requested_state;
 }

 R2L_STATE
@@ -70,20 +58,11 @@
 R2L_STATE
 get_r2l_state(const char* file_name)
 {
-     R2L_STATE state = R2L_ERROR; /* be paranoid */
-     int rc = 0;
-
+     R2L_STATE st = R2L_ERROR;
+
      if (!file_name)
          return R2L_ERROR;

-     rc = file_exists(file_name);
-     if (rc == 1){
-         state = R2L_ENABLED;
-     } else if (rc == 0){
-         state = R2L_DISABLED;
-     } else {
-         state = R2L_ERROR;
-     }
-
-     return state;
+     r2l_query_state_internal(file_name, &st, NULL);
+     return st;
 }
diff -ur r2llib-0.20/r2llib.c r2llib-0.21/r2llib.c
--- r2llib-0.20/r2llib.c        Fri Aug  3 23:51:34 2001
+++ r2llib-0.21/r2llib.c        Sun Aug  5 12:04:40 2001
@@ -9,6 +9,7 @@
 #include "parse_argv.h"
 #include "r2l_state.h"
 #include "biditext_base_state.h"
+#include "query_state.h"

 #define FILE_NAME_ENV_VARIABLE "BIDITEXT_FILENAME"
 #define DEFAULT_FILE_NAME ".rev"
@@ -182,3 +183,18 @@

      return create_r2llib_data(repr);
 }
+
+void
+r2l_query_state(r2llib_t rtl, R2L_STATE* r2l_state,
+               BIDITEXT_BASE_STATE* bt_state)
+{
+     const char* file_name;
+
+     assert(rtl);
+
+     file_name = r2llib_data_get_file_name(rtl);
+     if (file_name){
+         r2l_query_state_internal(file_name, r2l_state, bt_state);
+     }
+}
+
diff -ur r2llib-0.20/r2llib.h r2llib-0.21/r2llib.h
--- r2llib-0.20/r2llib.h        Fri Aug  3 23:51:34 2001
+++ r2llib-0.21/r2llib.h        Sun Aug  5 12:04:40 2001
@@ -44,6 +44,13 @@
 /** perform cleanup */
 void r2l_destroy(r2llib_t rtl);

+/** @return in r2l_state the current r2l_state and in bt_state
+ *   the current biditext base state. Passing NULL for either is
+ *   allowed.
+ */
+void r2l_query_state(r2llib_t rtl, R2L_STATE* r2l_state,
+                    BIDITEXT_BASE_STATE* bt);
+
 /** @return 1 on success, -1 on failure */
 int r2l_enable(r2llib_t rtl);

diff -ur r2llib-0.20/r2llib.spec r2llib-0.21/r2llib.spec
--- r2llib-0.20/r2llib.spec     Fri Aug  3 23:51:34 2001
+++ r2llib-0.21/r2llib.spec     Sun Aug  5 12:04:40 2001
@@ -1,5 +1,5 @@
 %define name r2llib
-%define version 0.20
+%define version 0.21
 %define release 2

 Summary: A library for checking and toggling biditext's language


-- 
mulix
http://www.advogato.com/person/mulix

linux/reboot.h: #define LINUX_REBOOT_MAGIC1 0xfee1dead