Reworked very long string support for better encapsulation.
[pspp-builds.git] / src / data / variable.c
index 6c1414832b7c992fe5301940a80e33aca794b29d..64e4b3f0cd9cbeb46e1315a6ee122de4834b9b51 100644 (file)
@@ -350,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;
+}
+
+