<commit_before>/*
    one_file.c:

    Copyright (C) 1998 <NAME>

    This file is part of Csound.

    The Csound 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.

    Csound 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 Csound; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA
*/

#include "csoundCore.h"
#include <ctype.h>
#include <errno.h>
#if defined(LINUX) || defined(__MACH__) || defined(WIN32)
#  include <sys/types.h>
#  include <sys/stat.h>
#endif

#define CSD_MAX_LINE_LEN    4096

#ifdef WIN32
#  undef L_tmpnam
#  define L_tmpnam (200)
#endif

#ifndef TRUE
#  define TRUE  (1)
#  define FALSE (0)
#endif

typedef struct namelst {
  char           *name;
  struct namelst *next;
} NAMELST;

typedef struct {
    char    buffer[CSD_MAX_LINE_LEN];
    NAMELST *toremove;
    char    orcname[L_tmpnam + 4];
    char    sconame[L_tmpnam + 4];
    char    midname[L_tmpnam + 4];
    int     midiSet;
} ONE_FILE_GLOBALS;

#define ST(x)   (((ONE_FILE_GLOBALS*) csound->oneFileGlobals)->x)

CS_NOINLINE char *csoundTmpFileName(CSOUND *csound, char *buf, const char *ext)
{
    if (buf == NULL) {
      size_t  nBytes = (size_t) (L_tmpnam);
      if (ext != NULL && ext[0] != (char) 0)
        nBytes += strlen(ext);
      buf = csound->Malloc(csound, nBytes);
    }
    {
#if defined(LINUX) || defined(__MACH__)
      struct stat tmp;
      do {
#elif defined(WIN32)
      struct _stat tmp;
      do {
#endif
#ifndef WIN32
        if (tmpnam(buf) == NULL)
          csound->Die(csound, Str(" *** cannot create temporary file"));
#else
        {
          char  *s = (char*) csoundGetEnv(csound, "SFDIR");
          if (s == NULL)
            s = (char*) csoundGetEnv(csound, "HOME");
          s = _tempnam(s, "cs");
          if (s == NULL)
            csound->Die(csound, Str(" *** cannot create temporary file"));
          strcpy(buf, s);
          free(s);
        }
#endif
        if (ext != NULL && ext[0] != (char) 0) {
#ifndef __MACH__
          char  *p;
          /* remove original extension (does not work on OS X */
          /* and may be a bad idea) */
          if ((p = strrchr(buf, '.')) != NULL)
            *p = '\0';
#endif
          strcat(buf, ext);
        }
#ifdef __MACH__<commit_msg>Fixed temporary file bug on Windows (do not try to remove extension)<commit_after>/*
    one_file.c:

    Copyright (C) 1998 <NAME>

    This file is part of Csound.

    The Csound 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.

    Csound 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 Csound; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA
*/

#include "csoundCore.h"
#include <ctype.h>
#include <errno.h>
#if defined(LINUX) || defined(__MACH__) || defined(WIN32)
#  include <sys/types.h>
#  include <sys/stat.h>
#endif

#define CSD_MAX_LINE_LEN    4096

#ifdef WIN32
#  undef L_tmpnam
#  define L_tmpnam (200)
#endif

#ifndef TRUE
#  define TRUE  (1)
#  define FALSE (0)
#endif

typedef struct namelst {
  char           *name;
  struct namelst *next;
} NAMELST;

typedef struct {
    char    buffer[CSD_MAX_LINE_LEN];
    NAMELST *toremove;
    char    orcname[L_tmpnam + 4];
    char    sconame[L_tmpnam + 4];
    char    midname[L_tmpnam + 4];
    int     midiSet;
} ONE_FILE_GLOBALS;

#define ST(x)   (((ONE_FILE_GLOBALS*) csound->oneFileGlobals)->x)

CS_NOINLINE char *csoundTmpFileName(CSOUND *csound, char *buf, const char *ext)
{
    if (buf == NULL) {
      size_t  nBytes = (size_t) (L_tmpnam);
      if (ext != NULL && ext[0] != (char) 0)
        nBytes += strlen(ext);
      buf = csound->Malloc(csound, nBytes);
    }
    {
#if defined(LINUX) || defined(__MACH__)
      struct stat tmp;
      do {
#elif defined(WIN32)
      struct _stat tmp;
      do {
#endif
#ifndef WIN32
        if (tmpnam(buf) == NULL)
          csound->Die(csound, Str(" *** cannot create temporary file"));
#else
        {
          char  *s = (char*) csoundGetEnv(csound, "SFDIR");
          if (s == NULL)
            s = (char*) csoundGetEnv(csound, "HOME");
          s = _tempnam(s, "cs");
          if (s == NULL)
            csound->Die(csound, Str(" *** cannot create temporary file"));
          strcpy(buf, s);
          free(s);
        }
#endif
        if (ext != NULL && ext[0] != (char) 0) {
#if !defined(LINUX) && !defined(__MACH__) && !defined(WIN32)
          char  *p;
          /* remove original extension (does not work on OS X */
          /* and may be a bad idea) */
          if ((p = strrchr(buf, '.')) != NULL)
            *p = '\0';
#endif
          strcat(buf, ext);
        }
#ifdef __MACH__
{'fraction_non_alphanumeric': 0.1155547690674217, 'fraction_numerical': 0.016280304370907803, 'mean_word_length': 2.5325, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, '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': '2042082', 'n_tokens_mistral': 1929, 'n_tokens_neox': 1844, 'n_words': 636}