Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

string logPath = @"c:\..."
INhaxLogger Logger = NhaxLogger.CreateLogger(logPath, true, 10 * 1024 * 1024);
NhaxLogger.AddLogger(Logger, NhaxLogLevel.Debug);

Sample

View file
nameNhaxFileSplit.zip

Code Block
using Himsa.Noah.NhaxLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Remoting.Messaging;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace NhaxFileSplit
{
    internal class NhaxFileSplit
    {
        private static INhaxLogger Logger;

        private static string ExeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        private static Guid CreatedByGuid = Guid.Parse("08d794ca-012f-4785-9f0d-d0aa42564f1f");
        

        static bool InitLogger()
        {
            // initialise a logger used by Nhax lib
            int logMaxSizeBytes = 10 * 1024 * 1024;

            if (ExeDir == null) {
                Console.WriteLine("Error: failed to get path to executing assembly");
                return false;
            }

            string logPath = Path.Combine(ExeDir, "NhaxFileSplit.log");
            Logger = NhaxLogger.CreateLogger(logPath, true, logMaxSizeBytes);
            NhaxLogger.AddLogger(Logger, NhaxLogLevel.Debug);
            return true;
        }

        static string ToBase64EncodedString(string s)
        {
            if (s == null) return null;
            return Convert.ToBase64String(Encoding.UTF8.GetBytes(s));
        }

        static string FromBase64EncodedString(string s)
        {
            if (s == null) return null;
            byte[] ba = Convert.FromBase64String(s);
            return System.Text.Encoding.UTF8.GetString(ba);
        }


        static bool ValidateResultFailed( ValidateResult vr)
        {
            if (!vr.Validated) return true;
            Console.WriteLine("Error: ValiedateResult {0}", vr.ToString());
            return false;
        }

        static void NhaxFileCallback(string pathToFileCreated)
        {
            Console.WriteLine("Nhax file created: {0}", pathToFileCreated);
        }

        static bool ReadWriteNhaxFile(string inputFile)
        {
            int skip = 0, 
                take = 3;                // Number of patients read in each iteration 
            List<NhaxUser> users;
            List<NhaxPatient> patients;
            int maxFileSize = 1;         // One patient per Nhax file (size og written a Nhax file will always be greater than 1)

            string privateDataBase64Encoded = ToBase64EncodedString("Test saving private data");

            using (NhaxWriter writer = new NhaxWriter( ExeDir, "test", null, NhaxFileCallback, true, true, maxFileSize)) {
                using (NhaxReader reader = new NhaxReader()) {
                    if (ValidateResultFailed(reader.Initialize(inputFile, null))) return false;
                    NhaxMiscData miscData = reader.ReadMiscData();
                    if (ValidateResultFailed(reader.ReadUsers(out users))) return false;

                    // Users and misc data must be written before patients is written to Nhax file
                    if (ValidateResultFailed(writer.WriteMiscData("NhaxFileSplit", CreatedByGuid, privateDataBase64Encoded))) return false;
                    if (ValidateResultFailed(writer.WriteUsers(users))) return false;

                    int noOfPatients = reader.NoOfPatients();
                    while (skip < noOfPatients) {
                        if (skip + take > noOfPatients)
                            take = Math.Min(take, noOfPatients - skip);
                        if (ValidateResultFailed(reader.ReadPatients(skip, take, out patients))) return false;
                        foreach (var patient in patients) {
                            if (ValidateResultFailed(writer.WritePatient(patient))) return false;
                        }
                        skip += take;
                    }
                }
            }

            return true;
        }


        static void Main(string[] args)
        {
            // Program that split an Nhax file up in a number of Nhax files each containing one patient
            string inputFile = Path.Combine(ExeDir, "Input.nhax");
            if (args.Length > 0) inputFile = args[0];
            try {
                if (!InitLogger()) return;
                if (!ReadWriteNhaxFile(inputFile)) return;
            }
            catch (Exception e) {
                Console.WriteLine(e);
            }
        }
    }
}