/* TCHELP.C - Static Library Implementation */
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "TCHELP.H"

int find_str(const char* src, const char* key, char* out) {
    char s[64];
    char* p1;
    char* p2;
    int len;

    wsprintf(s, "%s=\"", key);
    p1 = strstr(src, s);
    if (p1) {
        p1 += strlen(s);
        p2 = strchr(p1, '"');
        if (p2) {
            len = p2 - p1;
            if (len > BUF_S) len = BUF_S;
            strncpy(out, p1, len);
            out[len] = 0;
            return 1;
        }
    }
    return 0;
}

void TC_InitData(HelpData* d) {
    memset(d, 0, sizeof(HelpData));
    strcpy(d->auth, "Unknown");
    strcpy(d->ver, "1.0");
}

int TC_LoadXML(const char* fname, HelpData* d) {
    FILE* f;
    char line[512];
    int c_lang = -1;
    int c_sec = 0;
    char* txt;

    TC_InitData(d);
    
    f = fopen(fname, "r");
    if (!f) return 0;

    while (fgets(line, 511, f)) {
        if (strstr(line, "<tchelp")) {
            find_str(line, "author", d->auth);
            find_str(line, "version", d->ver);
        }
        if (strstr(line, "<lang")) {
            char k[10];
            if (find_str(line, "name", k) || find_str(line, "lang", k)) {
                if (d->lang_cnt < MAX_L) {
                    c_lang = d->lang_cnt;
                    strcpy(d->lang[c_lang].key, k);
                    d->lang_cnt++;
                }
            }
        }
        if (strstr(line, "<description>")) {
             char* start = strchr(line, '>');
             if (start) {
                 char* end = strchr(start+1, '<');
                 if (end) { *end = 0; if (c_lang >= 0) strcpy(d->lang[c_lang].desc, start+1); }
             }
        }
        if (c_lang >= 0 && strstr(line, "<section")) {
            c_sec = d->lang[c_lang].sec_cnt;
            if (c_sec < MAX_S) {
                find_str(line, "name", d->lang[c_lang].sec[c_sec].name);
                if (fgets(line, 511, f)) {
                    txt = line;
                    while (*txt == ' ' || *txt == '\t') txt++; 
                    txt[strcspn(txt, "\r\n")] = 0;
                    if (strstr(txt, "</section>")) *strstr(txt, "</section>") = 0;
                    strcpy(d->lang[c_lang].sec[c_sec].text, txt);
                    d->lang[c_lang].sec_cnt++;
                }
            }
        }
        if (strstr(line, "<link")) {
            if (d->link_cnt < MAX_LINKS) {
                find_str(line, "page", d->links[d->link_cnt]);
                d->link_cnt++;
            }
        }
    }
    fclose(f);
    return 1;
}

int TC_SaveXML(const char* fname, HelpData* d) {
    FILE* f;
    int i, j;
    f = fopen(fname, "w");
    if (!f) return 0;

    fprintf(f, "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
    fprintf(f, "<tchelp version=\"%s\" author=\"%s\">\n", d->ver, d->auth);

    for(i=0; i<d->lang_cnt; i++) {
        fprintf(f, "  <lang name=\"%s\">\n", d->lang[i].key);
        fprintf(f, "    <description>%s</description>\n", d->lang[i].desc);
        for(j=0; j<d->lang[i].sec_cnt; j++) {
            fprintf(f, "    <section name=\"%s\">\n", d->lang[i].sec[j].name);
            fprintf(f, "      %s\n", d->lang[i].sec[j].text);
            fprintf(f, "    </section>\n");
        }
        fprintf(f, "  </lang>\n");
    }
    for(i=0; i<d->link_cnt; i++) {
        fprintf(f, "  <link page=\"%s\" />\n", d->links[i]);
    }
    fprintf(f, "</tchelp>\n");
    fclose(f);
    return 1;
}