Sindbad~EG File Manager

Current Path : /home/beeson/ThreeBody/ThreeBodyProblem/
Upload File :
Current File : //home/beeson/ThreeBody/ThreeBodyProblem/SendJavascript.c

// Michael Beeson, for the ThreeBody project
// original date 3.19.25
// 3.24.25  made it also send x0,y0,p0,q0
// 3.24.25  made it send colors as rgb(r,g,b)

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "ThreeBody.h"
#include "ThreeBodyDoc.h"
#include "sendJavascript.h"
#include "svgGraph3.h"

int sendJavaScript(char *outbuffer, int outbuffersize, PDOCDATA3 pDocData) {
    int offset = 0;
    int n;
    body *bodies = pDocData->bodies;
    int nbodies = pDocData->nbodies;
    double tmax = pDocData->tmax;
    char *integrationMethod = pDocData->integrationMethod;
    // Write the opening <script> tag
    n = snprintf(outbuffer + offset, outbuffersize - offset, "<script>\n");
    if (n < 0 || n >= outbuffersize - offset) return offset;
    offset += n;
    
    // in the PHP we treat tmax as an integer
    n = snprintf(outbuffer + offset, outbuffersize - offset, "var tmax = %d;", (int)(tmax+0.1));
    if (n < 0 || n >= outbuffersize - offset) return offset;
    offset += n;
 
     n = snprintf(outbuffer + offset, outbuffersize - offset, "var integrationMethod = \"%s\";",
                  integrationMethod);
    if (n < 0 || n >= outbuffersize - offset) return offset;
    offset += n;

    // Write the bodies
    n = snprintf(outbuffer + offset, outbuffersize - offset, "var bodies = [\n");
    if (n < 0 || n >= outbuffersize - offset) return offset;
    offset += n;

    // Loop over each body and write an object literal for it.
    for (int i = 0; i < nbodies; i++) {
        // For all bodies except the last, add a trailing comma.
        const char *comma = (i < nbodies - 1) ? "," : "";
        char colorbuf[32];
        svg_colorstring(colorbuf,bodies[i].color);
        n = snprintf(outbuffer + offset, outbuffersize - offset,
             "  { mass: %g, bodynumber: %d, color: \"%s\", r: %g, x0: %g, y0: %g, p0: %g, q0: %g }%s\n",
             bodies[i].mass,
             bodies[i].bodynumber,
             colorbuf,
             bodies[i].r,
             bodies[i].x0,
             bodies[i].y0,
             bodies[i].p0,
             bodies[i].q0,
             comma);
       
        if (n < 0 || n >= outbuffersize - offset) break; // truncation or error
        offset += n;
    }
    
    // also send tmax and integrationMethod

    // Write the closing bracket.
    n = snprintf(outbuffer + offset, outbuffersize - offset, "];\n");
    if (n < 0 || n >= outbuffersize - offset) return offset;
    offset += n;

    // Write the closing </script> tag
    n = snprintf(outbuffer + offset, outbuffersize - offset, "</script>\n");
    if (n < 0 || n >= outbuffersize - offset) return offset;
    offset += n;

    return offset;  // return the total number of bytes written
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists