
#include "Stdafx.h"

extern "C" {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lkf5defs.h"
}

#include "LKFMetaData.h"
#include "LKFException.h"

using namespace System;
using namespace System::IO;
using namespace System::Runtime::InteropServices;

namespace lkfxdok {
	namespace c {

		LKFMetaData::LKFMetaData(int modelljahr, String^ path, String^ name)
		{
			char errorBuffer[4096]; /* V2025.1: buffer groesser */
			_modelljahr = modelljahr;
			_path = path;
			_name = name;

			char* cPath = (char*)Marshal::StringToHGlobalAnsi(path).ToPointer();
			_ptr = lkf_alloc_stamm(modelljahr, cPath, 4096, errorBuffer);
			Marshal::FreeHGlobal((IntPtr)cPath);
			
			if (!_ptr && errorBuffer[0]) {
				LKFException^ ex = gcnew LKFException(-1, gcnew String(errorBuffer, 0, (int)strlen(errorBuffer), System::Text::Encoding::UTF8));
				throw ex;
			}
		}

		LKFMetaData::~LKFMetaData() {
			this->!LKFMetaData();
		}

		LKFMetaData::!LKFMetaData() {
			if (_ptr != nullptr) {
				lkf_free_stamm((LKF_METADATA*)_ptr);
				_ptr = nullptr;
			}
		}

		String^ LKFMetaData::Path::get(void) {
			return _path;
		}

		String^ LKFMetaData::Name::get(void) {
			if (_name != nullptr) {
				return _name;
			}
			return _path;
		}

		int LKFMetaData::Modelljahr::get(void) {
			return _modelljahr;
		}

		

	}
}