Reworked very long string support for better encapsulation.
[pspp-builds.git] / src / data / variable.c
index 3c170c842c943fddd2039d8932543fb1849eebee..64e4b3f0cd9cbeb46e1315a6ee122de4834b9b51 100644 (file)
@@ -131,6 +131,7 @@ hash_value(const union value  *v, int width)
 bool
 var_is_valid_name (const char *name, bool issue_error) 
 {
+  bool plausible;
   size_t length, i;
   
   assert (name != NULL);
@@ -139,7 +140,7 @@ var_is_valid_name (const char *name, bool issue_error)
      CHARACTERS */
   length = strlen (name);
 
-  bool plausible = var_is_plausible_name(name, issue_error);
+  plausible = var_is_plausible_name(name, issue_error);
 
   if ( ! plausible ) 
     return false;
@@ -349,3 +350,36 @@ dict_class_to_name (enum dict_class dict_class)
       abort ();
     }
 }
+
+/* Return the number of bytes used when writing case_data for a variable 
+   of WIDTH */
+int
+width_to_bytes(int width)
+{
+  const int chunks = width / EFFECTIVE_LONG_STRING_LENGTH ;
+  const int remainder = width - (chunks * EFFECTIVE_LONG_STRING_LENGTH) ;
+  int bytes, mod8;
+
+  assert (width >= 0);
+
+  if ( width == 0 ) 
+    return MAX_SHORT_STRING ;
+
+  if ( width <= MAX_LONG_STRING) 
+    return MAX_SHORT_STRING * DIV_RND_UP(width, MAX_SHORT_STRING);
+
+
+  bytes =  remainder + (chunks * (MAX_LONG_STRING + 1) );
+
+  /* Round up to the nearest 8 */
+  mod8 = bytes % MAX_SHORT_STRING;
+
+  if ( mod8 ) 
+    bytes += MAX_SHORT_STRING - mod8;
+
+  assert( bytes % MAX_SHORT_STRING == 0 );
+
+  return bytes;
+}
+
+