view examples/dsl/redirect.cpp @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children 607e3be6bad8
line wrap: on
line source

/*****

 AnaGram Programming Examples

 A Dos Script Language
 Redirection Class Module

 Copyright 1993 Parsifal Software. All Rights Reserved.

 This software is provided 'as-is', without any express or implied
 warranty.  In no event will the authors be held liable for any damages
 arising from the use of this software.

 Permission is granted to anyone to use this software for any purpose,
 including commercial applications, and to alter it and redistribute it
 freely, subject to the following restrictions:

 1. The origin of this software must not be misrepresented; you must not
    claim that you wrote the original software. If you use this software
    in a product, an acknowledgment in the product documentation would be
    appreciated but is not required.
 2. Altered source versions must be plainly marked as such, and must not be
    misrepresented as being the original software.
 3. This notice may not be removed or altered from any source distribution.

*****/

#include "redirect.h"
#include "assert.h"
#include "util.h"

// Constructors

redirect::redirect(int h) {
  int temp;
  handle = h;
#if defined(__MSDOS__) || defined(__WINDOWS__)
  tmpnam(name);
  temp = open(name, O_CREAT | O_RDWR | O_TEXT, S_IREAD | S_IWRITE);
#else
  /* tmpnam() is unsafe, and causes a warning from many Unix linkers */
  strcpy(name, ".dsl.XXXXXX");
  temp = mkstemp(name);
#endif
  assert(temp >= 0);
  old_handle = dup(handle);
  dup2(temp, handle);
  close(temp);
  copy_flag = 0;                            // live
}

redirect::redirect(int h, char *file_name, int append_flag) {
  int temp;
  int flag;
  handle = h;
  temp = open(file_name, O_CREAT | O_RDWR | O_TEXT, S_IREAD | S_IWRITE);
  if (append_flag) lseek(temp, 0L, SEEK_END);
  name[0] = 0;
  assert(temp >= 0);
  old_handle = dup(handle);
  flag = dup2(temp, handle);
  assert(flag == 0);
  close(temp);
  copy_flag = 0;                            // live
}

redirect::redirect(redirect &r) {
  *this = r;
  copy_flag++;                              // memorex
}


// Destructor

redirect::~redirect() {
  if (copy_flag) return;
  dup2(old_handle, handle);
  close(old_handle);
  if (name[0]) unlink(name);
}


// Save temporary file

char *save_file(redirect &s) {
  assert(s.copy_flag == 0);
  char *result = memdup(s.name, 1+strlen(s.name));
  s.name[0] = 0;
  return result;
}


// Extract content of file

char *content(redirect &s) {
  long length = filelength(s.handle);
  long where = tell(s.handle);              // Current file position
  unsigned n;
  char *result;

  assert(length < 65520L);
  n = (unsigned) (length + 1);
  result = new char[n];
  assert(result != NULL);
  lseek(s.handle,0,0);
  n = read(s.handle, result, (unsigned) length);
  lseek(s.handle,where,0);                  // Restore file
  result[n] = 0;                            // Terminate string
  return result;
}