Converting C to C#, handling lack of local static vars

Rhodan

Active member
Joined
Apr 7, 2015
Messages
41
Programming Experience
10+
I can't find a c# version of libastro so I guess I have to convert it myself. It doesn't look all that hard though it does use static locals a lot which c# doesn't support. I figure I'll have to use class static vars and rename them so different methods don't mess with other methods using the same local var name.

So my question is, is that a reasonable approach or does anyone know a better way? An example of the C code is below.

C code:
static void
aaha_aux (
double lt,
double x, double y,
double *p, double *q)
{
    static double last_lt = -3434, slt, clt;
    double cap, B;

    if (lt != last_lt) {
        slt = sin(lt);
        clt = cos(lt);
        last_lt = lt;
    }

    solve_sphere (-x, PI/2-y, slt, clt, &cap, &B);
    *p = B;
    *q = PI/2 - acos(cap);
}
 
Seems reasonable if you are just doing a simplistic mechanical porting of the code.

With the specific code above, though, it seems like the use of the static variable is an attempt to shave off a few milliseconds of time by avoiding recomputing the sine and cosine of lt. (Furthermore it seems to violate on the key rules about dealing with floating point numbers -- do not do equality comparisons.) This also brings into question thread safety. What happens when two different threads come in with different values of lt?
 
Also, is this the same libastro that is in PyEphem? If so, be aware of that the C library code is owned by Elwood Downey, and he explicitly says from his download page:
XEphem source code may not be used, in whole or in part, to build other computer programs under any circumstances without prior agreement.
PyEphem's author, Brandon Rhodes, obtained permission for use in PyEphem.
 
You could also just compile the C source if you have Visual C++ installed in VS, and then create a .NET wrapper that P/Invokes to the original library and does type conversions/marshalling.

VC++ includes a C compiler, which you can invoke in the Visual Studio Command Line with "cl.exe"...

C#:
cl /W4 file1.c file2.c file3.c /link /out:lib1.dll


Then in C#, P/Invoke to the C library:

C#:
[DllImport("lib1.dll")]
public static extern void aaha_aux(double lt, double x, double y, ref double p, ref double q);

Please note that you will have to double check the type mappings, I *think* this is right, but not sure about the two ref parameters... You can figure it out! :)

This is your ticket:

Redistribution rights: You may redistribute XEphem source code only in its entirety and without modification. You may redistribute binaries only if they were built from the original source code, or from source code with very minor changes made for the purpose of porting and not for the purposes of changing functionality.

If you manage to port and build XEphem in its entirety from the original source in C in Windows, contact the original author, share with him your build script, and ask if you have his blessing to use the port. However looking at the breadth of XEphem, the entire visualization layer will have to be ported from X11/Motif to something like GDI+ or DirectDraw, or GTK#, and it may be a monumental task.
 
Last edited:
Back
Top Bottom