// Automatically generated C++ file on Tue Aug 5 01:52:59 2025 // // To build with Digital Mars C++ Compiler: // // dmc -mn -WD filewrite.cpp kernel32.lib #include #include #include extern "C" __declspec(dllexport) void (*Display)( const char *format, ...) = 0; // works like printf() extern "C" __declspec(dllexport) void (*EXIT)(const char *format, ...) = 0; // print message like printf() but exit(0) afterward extern "C" __declspec(dllexport) const double *DegreesC = 0; // pointer to current circuit temperature extern "C" __declspec(dllexport) const int *StepNumber = 0; // pointer to current step number extern "C" __declspec(dllexport) const int *NumberSteps = 0; // pointer to estimated number of steps extern "C" __declspec(dllexport) const char *const *InstanceName = 0; // pointer to address of instance name extern "C" __declspec(dllexport) const char *QUX = 0; // path to QUX.exe extern "C" __declspec(dllexport) const bool *ForKeeps = 0; // pointer to whether being evaluated non-hypothetically extern "C" __declspec(dllexport) const bool *HoldICs = 0; // pointer to whether instance initial conditions are being held extern "C" __declspec(dllexport) const void *GUI_HWND = 0; // pointer to Window handle of QUX.exe extern "C" __declspec(dllexport) const double *CKTtime = 0; extern "C" __declspec(dllexport) const double *CKTdelta = 0; extern "C" __declspec(dllexport) const int *IntegrationOrder = 0; extern "C" __declspec(dllexport) const int *CKTnoncon = 0; extern "C" __declspec(dllexport) const char *(*BinaryFormat)( unsigned int data) = 0; // BinaryFormat(0x1C) returns "0b00011100" extern "C" __declspec(dllexport) void (*bzero)(void *ptr, unsigned int count) = 0; union uData { bool b; char c; unsigned char uc; short s; unsigned short us; int i; unsigned int ui; float f; double d; long long int i64; unsigned long long int ui64; char *str; unsigned char *bytes; }; // int DllMain() must exist and return 1 for a process to load the .DLL // See https://docs.microsoft.com/en-us/windows/win32/dlls/dllmain for more // information. int __stdcall DllMain(void *module, unsigned int reason, void *reserved) { return 1; } // #undef pin names lest they collide with names in any header file(s) you might // include. #undef param1 struct sFILEWRITE { // declare the structure here // const char *filefullname; char filefullname[1024]; bool init; }; extern "C" __declspec(dllexport) void filewrite( struct sFILEWRITE **opaque, double t, union uData *data) { double param1 = data[0].d; // input const char *fname = data[1].str; // input parameter const char *fext = data[2].str; // input parameter const char *paramlabel = data[3].str; // input parameter if (!*opaque) { *opaque = (struct sFILEWRITE *)malloc(sizeof(struct sFILEWRITE)); bzero(*opaque, sizeof(struct sFILEWRITE)); } struct sFILEWRITE *inst = *opaque; // Implement module evaluation code here: // Initialization (execute one time in every .step) if (*ForKeeps & !inst->init) { // Build filename step-by-step: // // strBuf space was reserved on stack; the stack space is reserved for // strBuf only until the closing "}" is reached. subsequent code (and // additional DLL instances in schematic) would overwrite this storage. // moving buffer into per-instance data and building string there. // // changed strBuf to a pointer to the per-instance buffer for convenience // (allows continuing to use strBuf name in the existing code) //` // char strBuf[1024]; // Create a temporary buffer char *strBuf = inst->filefullname; strcpy(strBuf, fname); // Copy base filename strcat(strBuf, "_"); // Add underscore separator strcat(strBuf, paramlabel); // Append parameter label strcat(strBuf, "="); // Add equals sign char temp[32]; // Temporary buffer for number conversion gcvt(param1, 8, temp); // Convert double to string (8 significant digits) strcat(strBuf, temp); // Append converted number strcat(strBuf, fext); // Add file extension // no longer needed... // inst->filefullname = strBuf; // Assign filename pointer to instance // Display the generated filename Display("Filename : %s\n", inst->filefullname); // Attempt to create and write to file FILE *fptr = fopen(inst->filefullname, "w"); if (fptr) { fprintf(fptr, "This file name is %s\n", inst->filefullname); fclose(fptr); } inst->init = true; // Mark as initialized } /* * note: at this point, the stack space reserved for strBuf[1024] would be * "unreserved" and reused/overwritten by any following code... */ } extern "C" __declspec(dllexport) double MaxExtStepSize( struct sFILEWRITE *inst, double t) { return 1e308; // implement a good choice of max timestep size that depends // on struct sFILEWRITE } extern "C" __declspec(dllexport) void Trunc(struct sFILEWRITE *inst, double t, union uData *data, double *timestep) { // limit the timestep to a tolerance if the circuit // causes a change in struct sFILEWRITE const double ttol = 1e-9; // 1ns default tolerance if (*timestep > ttol) { struct sFILEWRITE tmp = *inst; filewrite(&(&tmp), t, data); // if(tmp != *inst) // implement a meaningful way to detect if the state has // changed // *timestep = ttol; } } extern "C" __declspec(dllexport) void Destroy(struct sFILEWRITE *inst) { free(inst); }