public int Count(Guid idVariable, CaseDataType dataType, string databaseName)
        {
            string fileName = Path.Combine(
                ConfigurationManager.AppSettings["CaseDataPath"],
                databaseName,
                idVariable + ".kdf"
                );

            if (!File.Exists(fileName))
            {
                return(0);
            }

            byte[] buffer = File.ReadAllBytes(fileName);

            if (dataType == CaseDataType.Categorical)
            {
                return(buffer.Length / 32);
            }
            else if (dataType == CaseDataType.Numeric)
            {
                return(buffer.Length / 24);
            }

            int fieldLength = 0;

            if (dataType == CaseDataType.Text)
            {
                fieldLength = BitConverter.ToInt32(new byte[]
                {
                    buffer[0],
                    buffer[1],
                    buffer[2],
                    buffer[3]
                }, 0);

                return(buffer.Length / (16 + fieldLength));
            }

            return(0);
        }
        public List <object[]> GetValues2(
            CaseDataType dataType,
            string fileName,
            Dictionary <Guid, object> categoryFilter = null
            )
        {
            if (!File.Exists(fileName))
            {
                return(new List <object[]>());
            }

            byte[] buffer = File.ReadAllBytes(fileName);

            int i           = 0;
            int fieldLength = 0;

            if (dataType == CaseDataType.Text)
            {
                fieldLength = BitConverter.ToInt32(new byte[]
                {
                    buffer[i + 0],
                    buffer[i + 1],
                    buffer[i + 2],
                    buffer[i + 3]
                }, 0);

                i += 4;
            }

            List <object[]> result = new List <object[]>();

            while (true)
            {
                if (i == buffer.Length)
                {
                    break;
                }

                /*
                 * long idRespondent = BitConverter.ToInt64(new byte[]
                 *  {
                 *      buffer[i+0],
                 *      buffer[i+1],
                 *      buffer[i+2],
                 *      buffer[i+3],
                 *      buffer[i+4],
                 *      buffer[i+5],
                 *      buffer[i+6],
                 *      buffer[i+7]
                 *  }, 0);
                 *
                 * i += 8;
                 */
                Guid idRespondent = new Guid(
                    BitConverter.ToUInt32(buffer, i + 0),
                    BitConverter.ToUInt16(buffer, i + 4),
                    BitConverter.ToUInt16(buffer, i + 6),
                    buffer[i + 8],
                    buffer[i + 9],
                    buffer[i + 10],
                    buffer[i + 11],
                    buffer[i + 12],
                    buffer[i + 13],
                    buffer[i + 14],
                    buffer[i + 15]
                    );
                i += 16;

                object value;

                if (dataType == CaseDataType.Numeric)
                {
                    value = BitConverter.ToDouble(new byte[]
                    {
                        buffer[i + 0],
                        buffer[i + 1],
                        buffer[i + 2],
                        buffer[i + 3],
                        buffer[i + 4],
                        buffer[i + 5],
                        buffer[i + 6],
                        buffer[i + 7]
                    }, 0);

                    i += 8;
                }
                else if (dataType == CaseDataType.Text)
                {
                    byte[] stringBuffer = new byte[fieldLength];

                    for (int c = 0; c < fieldLength; c++)
                    {
                        stringBuffer[c] = buffer[i + c];
                    }

                    value = System.Text.Encoding.UTF8.GetString(stringBuffer).Replace("\0", "").Trim();
                    i    += fieldLength;
                }
                else
                {
                    Guid idCategory = new Guid(
                        BitConverter.ToUInt32(buffer, i + 0),
                        BitConverter.ToUInt16(buffer, i + 4),
                        BitConverter.ToUInt16(buffer, i + 6),
                        buffer[i + 8],
                        buffer[i + 9],
                        buffer[i + 10],
                        buffer[i + 11],
                        buffer[i + 12],
                        buffer[i + 13],
                        buffer[i + 14],
                        buffer[i + 15]
                        );
                    i += 16;


                    if (categoryFilter != null && categoryFilter.ContainsKey(idCategory) == false)
                    {
                        continue;
                    }

                    value = idCategory;
                }

                result.Add(new object[] { idRespondent, value });
            }

            return(result);
        }
        public Dictionary <Guid, List <object> > GetValues(
            string fileName,
            CaseDataType dataType,
            Dictionary <Guid, List <object> > result,
            Dictionary <Guid, object> categoryFilter = null
            )
        {
            if (!File.Exists(fileName))
            {
                return(new Dictionary <Guid, List <object> >());
            }

            byte[] buffer = File.ReadAllBytes(fileName);

            int fieldLength = 0;
            int i           = 0;

            if (dataType == CaseDataType.Text)
            {
                fieldLength = BitConverter.ToInt32(new byte[]
                {
                    buffer[i + 0],
                    buffer[i + 1],
                    buffer[i + 2],
                    buffer[i + 3]
                }, 0);

                i += 4;
            }

            while (true)
            {
                if (i >= buffer.Length)
                {
                    break;
                }

                Guid idRespondent = new Guid(new byte[]
                {
                    buffer[i + 0],
                    buffer[i + 1],
                    buffer[i + 2],
                    buffer[i + 3],
                    buffer[i + 4],
                    buffer[i + 5],
                    buffer[i + 6],
                    buffer[i + 7],
                    buffer[i + 8],
                    buffer[i + 9],
                    buffer[i + 10],
                    buffer[i + 11],
                    buffer[i + 12],
                    buffer[i + 13],
                    buffer[i + 14],
                    buffer[i + 15]
                });
                i += 16;

                object value;

                if (dataType == CaseDataType.Numeric)
                {
                    value = BitConverter.ToDouble(new byte[]
                    {
                        buffer[i + 0],
                        buffer[i + 1],
                        buffer[i + 2],
                        buffer[i + 3],
                        buffer[i + 4],
                        buffer[i + 5],
                        buffer[i + 6],
                        buffer[i + 7]
                    }, 0);

                    i += 8;
                }
                else if (dataType == CaseDataType.Text)
                {
                    byte[] stringBuffer = new byte[fieldLength];

                    for (int c = 0; c < fieldLength; c++)
                    {
                        stringBuffer[c] = buffer[i + c];
                    }

                    value = System.Text.Encoding.UTF8.GetString(stringBuffer).Replace("\0", "").Trim();
                    i    += fieldLength;
                }
                else
                {
                    Guid idCategory = new Guid(new byte[]
                    {
                        buffer[i + 0],
                        buffer[i + 1],
                        buffer[i + 2],
                        buffer[i + 3],
                        buffer[i + 4],
                        buffer[i + 5],
                        buffer[i + 6],
                        buffer[i + 7],
                        buffer[i + 8],
                        buffer[i + 9],
                        buffer[i + 10],
                        buffer[i + 11],
                        buffer[i + 12],
                        buffer[i + 13],
                        buffer[i + 14],
                        buffer[i + 15]
                    });
                    i += 16;


                    if (categoryFilter != null && categoryFilter.ContainsKey(idCategory) == false)
                    {
                        continue;
                    }

                    value = idCategory;
                }

                if (!result.ContainsKey(idRespondent))
                {
                    result.Add(idRespondent, new List <object>());
                }

                result[idRespondent].Add(value);
            }

            return(result);
        }