Пример #1
0
PathName
DviDoc::GetDocDir ()
{
  PathName result = GetPathName();
  result.RemoveFileSpec ();
  return (result);
}
Пример #2
0
void
File::Move (/*[in]*/ const PathName &	source,
	    /*[in]*/ const PathName &	dest)
{
  struct stat sourceStat;
  if (stat(source.Get(), &sourceStat) != 0)
    {
      FATAL_CRT_ERROR ("stat", source.Get());
    }

  PathName destDir (dest);
  destDir.MakeAbsolute ();
  destDir.RemoveFileSpec ();
  struct stat destStat;
  if (stat(destDir.Get(), &destStat) != 0)
    {
      FATAL_CRT_ERROR ("stat", destDir.Get());
    }
  
  bool sameDevice = (sourceStat.st_dev == destStat.st_dev);

  if (sameDevice)
    {
      SessionImpl::theSession->trace_files->WriteFormattedLine
	("core",
	 T_("renaming %s to %s"),
	 Q_(source),
	 Q_(dest));
      if (rename(source.Get(), dest.Get()) != 0)
	{
	  FATAL_CRT_ERROR ("rename", source.Get());
	}
    }
  else
    {
      Copy (source, dest);
      try
	{
	  Delete (source);
	}
      catch (const MiKTeXException &)
	{
	  try
	    {
	      if (Exists(source))
		{
		  Delete (dest);
		}
	    }
	  catch (const MiKTeXException &)
	    {
	    }
	  throw;
	}
    }
}
Пример #3
0
FILE *
File::Open (/*[in]*/ const PathName &	path,
	    /*[in]*/ FileMode		mode,
	    /*[in]*/ FileAccess		access,
	    /*[in]*/ bool		isTextFile,
	    /*[in]*/ FileShare		share)
{
  UNUSED_ALWAYS (isTextFile);
  UNUSED_ALWAYS (share);

  SessionImpl::theSession->trace_files->WriteFormattedLine
    ("core",
     T_("opening file %s (%d 0x%x %d %d)"),
     Q_(path),
     static_cast<int>(mode.Get()),
     static_cast<int>(access.Get()),
     static_cast<int>(share.Get()),
     static_cast<int>(isTextFile));

  int flags = 0;
  string strFlags;

  if (mode == FileMode::Create)
    {
      flags |= O_CREAT;
    }
  else if (mode == FileMode::Append)
    {
      flags |= O_APPEND;
    }

  if (access == FileAccess::ReadWrite)
    {
      flags |= O_RDWR;
      if (mode == FileMode::Append)
	{
	  strFlags += "a+";
	}
      else
	{
	  strFlags += "r+";
	}
    }
  else if (access == FileAccess::Read)
    {
      flags |= O_RDONLY;
      strFlags += "r";
    }
  else if (access == FileAccess::Write)
    {
      flags |= O_WRONLY;
      if (mode == FileMode::Append)
	{
	  strFlags += "a";
	}
      else
	{
	  flags |= O_TRUNC;
	  strFlags += "w";
	}
    }

  if (mode == FileMode::Create)
    {
      PathName dir (path);
      dir.MakeAbsolute ();
      dir.RemoveFileSpec();
      if (! Directory::Exists(dir))
	{
	  Directory::Create (dir);
	}
    }

  int fd;

  fd =
    open(path.Get(),
	 flags,
	 (((flags & O_CREAT) == 0)
	  ? 0
	  : (0
	     | S_IRUSR | S_IWUSR
	     | S_IRGRP
	     | S_IROTH
	     | 0)));

  if (fd < 0)
    {
      FATAL_CRT_ERROR ("open", path.Get());
    }

  try
    {
      return (FdOpen(fd, strFlags.c_str()));
    }
  catch (const exception &)
    {
      close (fd);
      throw;
    }
}
Пример #4
0
bool
DviImpl::FindSource (/*[in]*/ const char *	lpszFileName,
		     /*[in]*/ int		line,
		     /*[out]*/ DviPosition &	position)
{
  CheckCondition ();

  MIKTEX_BEGIN_CRITICAL_SECTION (&criticalSectionMonitor)
    {
      trace_search->WriteFormattedLine
	("libdvi",
	 T_("searching src special %d %s"),
	 line,
	 lpszFileName);
      
      SourceSpecial * pSourceSpecial1Best = 0;
      SourceSpecial * pSourceSpecial2Best = 0;

      int pageIdx1 = -1;
      int pageIdx2 = -1;

      // get fully qualified path to the directory location of the
      // document
      PathName documentLocation (fqDviFileName);
      documentLocation.RemoveFileSpec ();

      // file name relative to the location of the DVI document
      const char * lpszRelFileName;

      // absolute file name
      PathName fqFileName;

      if (Utils::IsAbsolutePath(lpszFileName))
	{
	  lpszRelFileName =
	    Utils::GetRelativizedPath(lpszFileName, documentLocation.Get());
	  fqFileName = lpszFileName;
	  fqFileName.MakeAbsolute ();
	}
      else
	{
	  lpszRelFileName = lpszFileName;
	  fqFileName = documentLocation;
	  fqFileName += lpszFileName;
	  fqFileName.MakeAbsolute ();
	}

      //
      // scan the document
      //

      for (int pageIdx = 0; pageIdx < GetNumberOfPages(); ++ pageIdx)
	{
	  DviPageImpl * pPage;

	  try
	    {
	      pPage = reinterpret_cast<DviPageImpl*>(GetLoadedPage(pageIdx));
	    }
	  catch (const OperationCancelledException &)
	    {
	      return (false);
	    }

	  if (pPage == 0)
	    {
	      throw DviPageNotFoundException
		(0,
		 T_("The DVI page could not be found."),
		 0,
		 __FILE__,
		 __LINE__);
	    }

	  AutoUnlockPage autoUnlockPage (pPage);

	  SourceSpecial * pSourceSpecial1 = 0;
	  SourceSpecial * pSourceSpecial2 = 0;
	  
	  SourceSpecial * pSourceSpecial;
	  
	  for (int j = -1;
	       (pSourceSpecial = pPage->GetNextSpecial<SourceSpecial>(j)) != 0;
	       )
	    {
	      const char * lpszName = pSourceSpecial->GetFileName();

	      // try exact match
	      bool nameMatch =
		(MyPathNameCompare(lpszName, lpszFileName) == 0);

	      // try fully qualified file names
	      if (! nameMatch)
		{
		  PathName fqName;
		  if (Utils::IsAbsolutePath(lpszName))
		    {
		      fqName = lpszName;
		      fqName.MakeAbsolute ();
		    }
		  else
		    {
		      fqName = documentLocation;
		      fqName += lpszName;
		      fqName.MakeAbsolute ();
		    }
		  nameMatch = (MyPathNameCompare(fqName, fqFileName) == 0);
		}

	      // try relative file names
	      if (! nameMatch && lpszRelFileName != 0)
		{
		  const char * lpszRelName;
		  if (! Utils::IsAbsolutePath(lpszName))
		    {
		      lpszRelName = lpszName;
		    }
		  else
		    {
		      lpszRelName =
			Utils::GetRelativizedPath(lpszName,
						  documentLocation.Get());
		    }
		  nameMatch =
		    (lpszRelName != 0
		     && MyPathNameCompare(lpszRelName, lpszRelFileName) == 0);
		}

	      if (! nameMatch)
		{
		  continue;
		}

	      if (line >= pSourceSpecial->GetLineNum())
		{
		  if (pSourceSpecial1 == 0
		      || (pSourceSpecial->GetLineNum()
			  > pSourceSpecial1->GetLineNum()))
		    {
		      pSourceSpecial1 = pSourceSpecial;
		    }
		}

	      if (line <= pSourceSpecial->GetLineNum())
		{
		  if (pSourceSpecial2 == 0
		      || (pSourceSpecial->GetLineNum()
			  < pSourceSpecial2->GetLineNum()))
		    {
		      pSourceSpecial2 = pSourceSpecial;
		    }
		}
	    }
	  
	  if (pSourceSpecial1 != 0 && pSourceSpecial2 != 0)
	    {
	      pSourceSpecial1Best = pSourceSpecial1;
	      pageIdx1 = pageIdx;
	      pSourceSpecial2Best = pSourceSpecial2;
	      pageIdx2 = pageIdx;
	      break;
	    }
	  else if (pSourceSpecial1 != 0
		   && (pSourceSpecial1Best == 0
		       || (abs(pSourceSpecial1Best->GetLineNum() - line)
			   > abs(pSourceSpecial1->GetLineNum() - line))))
	    {
	      pSourceSpecial1Best = pSourceSpecial1;
	      pageIdx1 = pageIdx;
	      pSourceSpecial2Best = 0;
	      pageIdx2 = -1;
	    }
	  else if (pSourceSpecial2 != 0
		   && (pSourceSpecial2Best == 0
		       || (abs(pSourceSpecial2Best->GetLineNum() - line)
			   > abs(pSourceSpecial2->GetLineNum() - line))))
	    {
	      pSourceSpecial2Best = pSourceSpecial2;
	      pageIdx2 = pageIdx;
	      pSourceSpecial1Best = 0;
	      pageIdx1 = -1;
	    }
	}

      //
      // evaluate the results
      //

      if (pSourceSpecial1Best == 0 && pSourceSpecial2Best == 0)
	{
	  trace_search->WriteLine ("libdvi", T_("search failed"));
	  return (false);
	}
      
      if (pSourceSpecial1Best == 0)
	{
	  trace_search->WriteFormattedLine
	    ("libdvi",
	     T_("found src2 on page #%d"),
	     pageIdx2);
	  trace_search->WriteFormattedLine
	    ("libdvi",
	     "   src2 = [%d (%d,%d)]",
	     pSourceSpecial2Best->GetLineNum(),
	     pSourceSpecial2Best->GetX(),
	     pSourceSpecial2Best->GetY());
	  position.pageIdx = pageIdx2;
	  position.x = pSourceSpecial2Best->GetX();
	  position.y = pSourceSpecial2Best->GetY();
	}
      else if (pSourceSpecial2Best == 0)
	{
	  trace_search->WriteFormattedLine
	    ("libdvi",
	     T_("found src1 on page #%d"),
	     pageIdx1);
	  trace_search->WriteFormattedLine
	    ("libdvi",
	     "   src1 = [%d (%d,%d)]",
	     pSourceSpecial1Best->GetLineNum(),
	     pSourceSpecial1Best->GetX(),
	     pSourceSpecial1Best->GetY());
	  position.pageIdx = pageIdx1;
	  position.x = pSourceSpecial1Best->GetX();
	  position.y = pSourceSpecial1Best->GetY();
	}
      else
	{
	  position.pageIdx = pageIdx1;
	  trace_search->WriteFormattedLine
	    ("libdvi",
	     T_("found src region on page #%d"),
	     position.pageIdx);
	  trace_search->WriteFormattedLine
	    ("libdvi",
	     "   src1 = [%d (%d,%d)]",
	     pSourceSpecial1Best->GetLineNum(),
	     pSourceSpecial1Best->GetX(),
	     pSourceSpecial1Best->GetY());
	  trace_search->WriteFormattedLine
	    ("libdvi",
	     "   src2 = [%d (%d,%d)]",
	     pSourceSpecial2Best->GetLineNum(),
	     pSourceSpecial2Best->GetX(),
	     pSourceSpecial2Best->GetY());
	  position.x = pSourceSpecial1Best->GetX ();
	  if (pSourceSpecial2Best->GetLineNum()
	      == pSourceSpecial1Best->GetLineNum())
	    {
	      position.y = pSourceSpecial1Best->GetY();
	    }
	  else
	    {
	      position.y = (pSourceSpecial1Best->GetY()
		   + ((line - pSourceSpecial1Best->GetLineNum())
		      * (pSourceSpecial2Best->GetY()
			 - pSourceSpecial1Best->GetY())
		      / (pSourceSpecial2Best->GetLineNum()
			 - pSourceSpecial1Best->GetLineNum())));
	    }
	  trace_search->WriteFormattedLine
	    ("libdvi",
	     "   interpolated (x,y) = (%d,%d)",
	     position.x,
	     position.y);
	}

      return (true);
    }
  MIKTEX_END_CRITICAL_SECTION();
}
Пример #5
0
bool
PostScript::FindGraphicsFile (/*[in]*/ const char *	lpszFileName,
			      /*[out]*/ PathName &	result)
{
  if (*lpszFileName == '`')
  {
    if (pDviImpl->TryGetTempFile(lpszFileName, result))
    {
      return (true);
    }
    else if (strncmp(lpszFileName + 1, "gunzip -c ", 10) == 0)
    {
      Uncompress (lpszFileName + 11, result);
      return (true);
    }
    else if (strncmp(lpszFileName + 1, "bunzip2 -c ", 11) == 0)
    {
      Uncompress (lpszFileName + 12, result);
      return (true);
    }
    else if (! AllowShellCommand(lpszFileName + 1))
    {
      tracePS->WriteFormattedLine ("libdvi", T_("Ignoring shell command %s"), Q_(lpszFileName + 1));
      return (false);
    }
    else
    {
      char szTempFileName[_MAX_PATH];
      Utils::CopyString (szTempFileName,
	_MAX_PATH,
	PathName().SetToTempFile().Get());
      string command;
      command = lpszFileName + 1;
      command += " > ";
      command += Q_(szTempFileName);
      MIKTEX_ASSERT (pDviImpl != 0);
      PathName docDir = pDviImpl->GetDviFileName();
      docDir.RemoveFileSpec();
      StdoutReader reader;
      bool done =
	Process::ExecuteSystemCommand(command.c_str(), 0,
	&reader, docDir.Get());
      if (! done)
      {
	// <fixme>hard-coded string</fixme>
	File::Delete (szTempFileName);
	FATAL_MIKTEX_ERROR ("PostScript::FindGraphicsFile",
	  T_("Execution of an embedded shell ")
	  T_("command failed for some reason!"),
	  0);
      }
      pDviImpl->RememberTempFile (lpszFileName + 1, szTempFileName);
      result = szTempFileName;
      return (true);
    }
  }
  else if (IsZFileName(lpszFileName))
  {
    if (pDviImpl->TryGetTempFile(lpszFileName, result))
    {
      return (true);
    }
    Uncompress (lpszFileName, result);
    return (true);
  }
  else
  {
    return (pDviImpl->FindGraphicsFile(lpszFileName, result));
  }
}