<commit_before>
// -*- mode: c++; c-basic-offset:4 -*-

// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.

// Copyright (c) 2002,2003 OPeNDAP, Inc.
// Author: <NAME> <<EMAIL>>
//         <NAME> <<EMAIL>> 
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.

// (c) COPYRIGHT URI/MIT 1994-2001
// Please read the full copyright statement in the file COPYRIGHT_URI.
//
// Authors:
//      jhrg,jimg       <NAME> <<EMAIL>>
//      reza            <NAME> <<EMAIL>>

// A few useful routines which are used in CGI programs.
//
// ReZa 9/30/94 

#include "config.h"

static char rcsid[] not_used = {"$Id$"};

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#ifndef TM_IN_SYS_TIME
#include <time.h>
#else
#include <sys/time.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>

#ifndef WIN32
#include <unistd.h>
#include <sys/wait.h>
#else
#include <io.h>
#include <fcntl.h>
#include <process.h>
// Win32 does not define this. 08/21/02 jhrg
#define F_OK 0
#endif

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>

#include "cgi_util.h"
#include "util.h"		// This supplies flush_stream for WIN32.
#include "debug.h"


#ifdef WIN32
#define FILE_DELIMITER	'\\'
#else  //  default to unix
#define FILE_DELIMITER '/'
#endif

// ...not using a const string here to avoid global objects. jhrg 12/23/05
#define CRLF "\r\n"             // Change here and in expr-test.cc. 

using namespace std;

static const int TimLen = 26;	// length of string from asctime()
static const int CLUMP_SIZE = 1024; // size of clumps to new in fmakeword()

/** This sends a formatted block of text to the client, containing
    version information about various aspects of the server.  The
    arguments allow you to enclose version information about the
    filter program and the dataset in the message.  Either argument
    (or both) may be omitted, in which case no script or dataset
    version information will be printed.

    @brief Send a version number.
    @param script_ver The version of the filter script executing this
    function. 
    @param dataset_ver The version of the dataset.
    @return TRUE for success. Always returns true.
*/
bool
do_version(const string &script_ver, const string &dataset_ver)
{
    fprintf( stdout, "HTTP/1.0 200 OK%s", CRLF ) ;
    fprintf( stdout, "XDODS-Server: %s%s", DVR, CRLF ) ;
    fprintf( stdout, "XOPeNDAP-Server: %s%s", DVR, CRLF ) ;
    fprintf( stdout, "XDAP: %s%s", DAP_PROTOCOL_VERSION, CRLF ) ;
    fprintf( stdout, "Content-Type: text/plain%s", CRLF ) ;
    fprintf( stdout, CRLF ) ;
    
    fprintf( stdout, "Core software version: %s%s", DVR, CRLF ) ;

    if (script_ver != "")
	fprintf( stdout, "Server Script Revision: %s%s", script_ver.c_str(), CRLF ) ;

    if (dataset_ver != "")
	fprintf( stdout,  "Dataset version: %s%s", dataset_ver.c_str(), CRLF ) ;

    fflush( stdout ) ;          // Not sure this is needed. jhrg 12/23/05
    
    return true;
}

#if OLD_DDS_TRANS_CODE
#ifndef WIN32
// // 02/13/98 jhrg
/** This function sends data to the client.

    This function has been superseded by the DODSFilter::send_data() mfunc.

    @deprecated Use DODSFilter's methods instead.

    @brief Send data. (deprecated)
    @param compression A Boolean value indicating whether the data is
    to be sent in compressed form or not.  A value of TRUE indicates
    that the data is to be comressed.
    @param data_stream A file pointer indicating where to print the
    data.  This is typically an XDR pointer.
    @param dds The DODS Dataset Descriptor Structure of the dataset
    whose data is to be sent.  The <tt>do_data_transfer</tt> uses the
    <tt>send</tt> method of this DDS to do the sending.
    @param dataset The (local) name of the dataset whose data is to be
    sent.
    @param constraint A constraint expression that may have been sent
    with the original data request.
    @return TRUE for success, FALSE otherwise.
*/
bool
do_data_transfer(bool compression, FILE *data_stream, DDS &dds,
		 const string &dataset, const string &constraint)
{
    if (compression) {
	int childpid;
	FILE *data_sink = compressor(data_stream, childpid);
	if (!dds.send(dataset, constraint, data_sink, true)) {
	    ErrMsgT("Could not send compressed data");
	    return false;
	}
	fclose(data_sink);
	int pid;
	while ((pid = waitpid(childpid, 0, 0)) > 0) {
	    DBG(cerr << "pid: " << pid << endl);
	}
    }
    else {
	if (!dds.send(dataset, constraint, data_stream, false)) {
	    ErrMsgT("Could not send data");
	    return false;
	}
    }

    return true;
}
#endif
#endif

/** This function accepts a dataset path name, and searches for a
    matching ancillary data file name with a very specific set of
    search rules, given here:

    <pre>
    directory           filename          extension
    same                same            `.'given
    given               same            `.'given
    same                given           `.'given
    given               given           `.'given
    </pre>

    Where ``same'' refers to the input dataset pathname, and ``given''
    refers to the function arguments.

    For example, If you call this function with a
    dataset name of <tt>/a/data</tt>, an extension of <tt>das</tt>, a
    directory of 
    <tt>b</tt>, and a filename of <tt>ralph</tt>, the function will
    look (in order) 
    for the following files:

    <pre>
    /a/data.das
    /b/data.das
    /a/ralph.das
    /b/ralph.das<commit_msg>cgi_util.cc: Removed the do_data_transfer() function. Use DODSFilter.<commit_after>
// -*- mode: c++; c-basic-offset:4 -*-

// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.

// Copyright (c) 2002,2003 OPeNDAP, Inc.
// Author: <NAME> <<EMAIL>>
//         <NAME> <<EMAIL>> 
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.

// (c) COPYRIGHT URI/MIT 1994-2001
// Please read the full copyright statement in the file COPYRIGHT_URI.
//
// Authors:
//      jhrg,jimg       <NAME> <<EMAIL>>
//      reza            <NAME> <<EMAIL>>

// A few useful routines which are used in CGI programs.
//
// ReZa 9/30/94 

#include "config.h"

static char rcsid[] not_used = {"$Id$"};

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#ifndef TM_IN_SYS_TIME
#include <time.h>
#else
#include <sys/time.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>

#ifndef WIN32
#include <unistd.h>
#include <sys/wait.h>
#else
#include <io.h>
#include <fcntl.h>
#include <process.h>
// Win32 does not define this. 08/21/02 jhrg
#define F_OK 0
#endif

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>

#include "cgi_util.h"
#include "util.h"		// This supplies flush_stream for WIN32.
#include "debug.h"


#ifdef WIN32
#define FILE_DELIMITER	'\\'
#else  //  default to unix
#define FILE_DELIMITER '/'
#endif

// ...not using a const string here to avoid global objects. jhrg 12/23/05
#define CRLF "\r\n"             // Change here and in expr-test.cc. 

using namespace std;

static const int TimLen = 26;	// length of string from asctime()
static const int CLUMP_SIZE = 1024; // size of clumps to new in fmakeword()

/** This sends a formatted block of text to the client, containing
    version information about various aspects of the server.  The
    arguments allow you to enclose version information about the
    filter program and the dataset in the message.  Either argument
    (or both) may be omitted, in which case no script or dataset
    version information will be printed.

    @brief Send a version number.
    @param script_ver The version of the filter script executing this
    function. 
    @param dataset_ver The version of the dataset.
    @return TRUE for success. Always returns true.
*/
bool
do_version(const string &script_ver, const string &dataset_ver)
{
    fprintf( stdout, "HTTP/1.0 200 OK%s", CRLF ) ;
    fprintf( stdout, "XDODS-Server: %s%s", DVR, CRLF ) ;
    fprintf( stdout, "XOPeNDAP-Server: %s%s", DVR, CRLF ) ;
    fprintf( stdout, "XDAP: %s%s", DAP_PROTOCOL_VERSION, CRLF ) ;
    fprintf( stdout, "Content-Type: text/plain%s", CRLF ) ;
    fprintf( stdout, CRLF ) ;
    
    fprintf( stdout, "Core software version: %s%s", DVR, CRLF ) ;

    if (script_ver != "")
	fprintf( stdout, "Server Script Revision: %s%s", script_ver.c_str(), CRLF ) ;

    if (dataset_ver != "")
	fprintf( stdout,  "Dataset version: %s%s", dataset_ver.c_str(), CRLF ) ;

    fflush( stdout ) ;          // Not sure this is needed. jhrg 12/23/05
    
    return true;
}

/** This function accepts a dataset path name, and searches for a
    matching ancillary data file name with a very specific set of
    search rules, given here:

    <pre>
    directory           filename          extension
    same                same            `.'given
    given               same            `.'given
    same                given           `.'given
    given               given           `.'given
    </pre>

    Where ``same'' refers to the input dataset pathname, and ``given''
    refers to the function arguments.

    For example, If you call this function with a
    dataset name of <tt>/a/data</tt>, an extension of <tt>das</tt>, a
    directory of 
    <tt>b</tt>, and a filename of <tt>ralph</tt>, the function will
    look (in order) 
    for the following files:

    <pre>
    /a/data.das
    /b/data.das
    /a/ralph.das
    /b/ralph.das
{'fraction_non_alphanumeric': 0.10959905014156544, 'fraction_numerical': 0.01707918531372728, 'mean_word_length': 2.812674094707521, 'pattern_counts': {'":': 0, '<': 91, '<?xml version=': 0, '>': 86, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3541021', 'n_tokens_mistral': 3647, 'n_tokens_neox': 3434, 'n_words': 1361}