public static List <ClassTemplate> GetAllClasses(string fileName, IEnumerable <ClassTemplate> classes) { var dataBuffer = new ClassBuffer(); var list = new List <ClassBuffer>(); Func <ClassBuffer> newFunc = () => new ClassBuffer(); Func <ClassBuffer, ClassTemplate> convert = x => x.ToClass(classes.SingleOrDefault(y => y.Id == x.Number)); // Yes, yes. Copied Code. How horrible. There's really no good alternative to DllImport-interfacing code though. Dynamic doesn't work, // templates don't work, etc. So. Given that the MajorMUD database format will never change in the future (it's been dead 10 years!), // I'm ok with copied code. Huzzah. byte[] posBlock = new byte[128]; char[] keyBuffer = fileName.ToCharArray(); //@"E:\PROJECTS\DEVELOPMENT\00-Current\KatanaMUD\KatanaMUD.Importer\bin\Debug\dats\wccrace2.dat".ToCharArray(); int bufferLength = 156; // System.Runtime.InteropServices.Marshal.SizeOf(dataBuffer); int status = Btrieve.BTRCALL(Btrieve.BOPEN, posBlock, ref dataBuffer, ref bufferLength, keyBuffer, (ushort)keyBuffer.Length, 0); if (status == 0) { status = Btrieve.BTRCALL(Btrieve.BGETFIRST, posBlock, ref dataBuffer, ref bufferLength, keyBuffer, (ushort)keyBuffer.Length, 0); if (status == 0) { list.Add(dataBuffer); } else { throw new InvalidOperationException(ErrorCode(status)); } // Get subsequent records while (status == 0) // BReturnCodes.END_OF_FILE or an error will occur { dataBuffer = newFunc(); status = Btrieve.BTRCALL(Btrieve.BGETNEXT, posBlock, ref dataBuffer, ref bufferLength, keyBuffer, (ushort)keyBuffer.Length, 0); if (status == 0) { list.Add(dataBuffer); } else if (status != 9) { throw new InvalidOperationException(ErrorCode(status)); } } } else { throw new InvalidOperationException(ErrorCode(status)); } return(list.Select(x => convert(x)).ToList()); }
public static extern short BTRCALL(ushort operation, [MarshalAs(UnmanagedType.LPArray, SizeConst = 128)] byte[] posBlk, [MarshalAs(UnmanagedType.Struct, SizeConst = 255)] ref ClassBuffer databuffer, ref int dataLength, [MarshalAs(UnmanagedType.LPArray, SizeConst = 255)] char[] keyBffer, ushort keyLength, ushort keyNum);