Example #1
0
void PixInsightX11Installer::DirectorySearch_Recursive( StringList& foundItems, const String& dirPath, const String& baseDir )
{
   if ( dirPath.Has( ".." ) )
      throw Error( "SearchDirectory(): Attempt to redirect outside the base directory." );
   if ( !dirPath.BeginsWith( baseDir ) )
      throw Error( "SearchDirectory(): Attempt to redirect outside the base directory." );
   if ( !File::DirectoryExists( dirPath ) )
      throw Error( "SearchDirectory(): Attempt to search a nonexistent directory." );

   String currentDir = dirPath;
   if ( !currentDir.EndsWith( '/' ) )
      currentDir += '/';

   StringList directories;
   FindFileInfo info;
   for ( File::Find f( currentDir + "*" ); f.NextItem( info ); )
      if ( info.IsDirectory() )
      {
         if ( info.name != "." && info.name != ".." )
         {
            directories.Add( info.name );
            foundItems.Add( currentDir + info.name + '/' );
         }
      }
      else
         foundItems.Add( currentDir + info.name );

   for ( StringList::const_iterator i = directories.Begin(); i != directories.End(); ++i )
      DirectorySearch_Recursive( foundItems, currentDir + *i, baseDir );
}
Example #2
0
static void SearchDirectory_Recursive( StringList& fileNames, const String& fileName, bool recursive )
{
   String fileDir = File::ExtractDrive( fileName ) + File::ExtractDirectory( fileName );
   String wildSpec = File::ExtractName( fileName ) + File::ExtractExtension( fileName );

   FindFileInfo info;

   for ( File::Find f( fileName ); f.NextItem( info ); )
      if ( !info.IsDirectory() )
         if ( info.name.WildMatch( wildSpec ) )
         {
            String path = fileDir + '/' + info.name;
            if ( !fileNames.Contains( path ) )
               fileNames.Add( path );
         }

   if ( recursive )
   {
      StringList directories;

      for ( File::Find f( fileDir + "/*" ); f.NextItem( info ); )
         if ( info.IsDirectory() && info.name != "." && info.name != ".." )
            directories.Add( info.name );

      for ( StringList::const_iterator i = directories.Begin(); i != directories.End(); ++i )
         SearchDirectory_Recursive( fileNames, fileDir + '/' + *i + '/' + wildSpec, true );
   }
}
Example #3
0
void ExternalProcess::SetEnvironment( const StringList& environment )
{
   Array<const char16_type*> vars;
   for ( StringList::const_iterator i = environment.Begin(); i != environment.End(); ++i )
      vars.Add( i->c_str() );
   if ( (*API->ExternalProcess->SetExternalProcessEnvironment)( handle, vars.Begin(), vars.Length() ) == api_false )
      throw APIFunctionError( "SetExternalProcessEnvironment" );
}
Example #4
0
void ExternalProcess::Start( const String& program, const StringList& arguments )
{
   Array<const char16_type*> argv;
   for ( StringList::const_iterator i = arguments.Begin(); i != arguments.End(); ++i )
      argv.Add( i->c_str() );
   if ( (*API->ExternalProcess->StartExternalProcess)( handle, program.c_str(), argv.Begin(), argv.Length() ) == api_false )
      throw APIFunctionError( "StartExternalProcess" );
}
Example #5
0
void PixInsightX11Installer::CopyFiles( const String& targetDir, const String& sourceDir )
{
   if ( !targetDir.BeginsWith( '/' ) )
      throw Error( "CopyFiles(): Relative target directory." );
   if ( !sourceDir.BeginsWith( '/' ) )
      throw Error( "CopyFiles(): Relative source directory." );
   if ( targetDir.EndsWith( '/' ) || sourceDir.EndsWith( '/' ) )
      throw Error( "CopyFiles(): Incorrectly terminated directories." );
   if ( !File::DirectoryExists( targetDir ) )
      throw Error( "CopyFiles(): Nonexistent target directory." );
   if ( !File::DirectoryExists( sourceDir ) )
      throw Error( "CopyFiles(): Nonexistent source directory." );

   StringList sourceItems = SearchDirectory( sourceDir );

   size_type sourceDirLen = sourceDir.Length();
   for ( StringList::const_iterator i = sourceItems.Begin(); i != sourceItems.End(); ++i )
   {
      String relSourcePath = *i;
      relSourcePath.DeleteLeft( sourceDirLen );

      String targetPath = targetDir + relSourcePath;
      if ( targetPath.EndsWith( '/' ) )
      {
         /*
          * Create a subdirectory
          */
         targetPath.Delete( targetPath.UpperBound() );
         if ( !File::DirectoryExists( targetPath ) )
         {
            File::CreateDirectory( targetPath );
            String sourcePath = *i;
            sourcePath.Delete( sourcePath.UpperBound() );
            File::CopyTimesAndPermissions( targetPath, sourcePath );
         }
      }
      else
      {
         /*
          * Copy a file
          */
         /*
          * ### N.B. We don't have to create subdirectories here becase they
          * have been reported by SearchDirectory(), and we are creating them
          * before copying files. SearchDirectory() promises that all
          * subdirectories are reported before their contained files.
          */
         /*
         String targetSubdir = File::ExtractDirectory( targetPath );
         if ( targetSubdir.EndsWith( '/' ) )
            targetSubdir.Delete( targetSubdir.UpperBound() );
         if ( !File::DirectoryExists( targetSubdir ) )
            File::CreateDirectory( targetSubdir );
         */
         File::CopyFile( targetPath, *i );
      }
   }
}
Example #6
0
 static String TR( const StringList& cols )
 {
    String tr( "<tr style=\"background:" + ((tableRow & 1) ? oddBgColor : evenBgColor) + "\">" );
    for ( StringList::const_iterator i = cols.Begin(); i != cols.End(); ++i )
       tr += TD( *i );
    tr += "</tr>";
    ++tableRow;
    return tr;
 }
Example #7
0
void FontComboBox::RemoveItalicFonts()
{
   StringList installedFaces = Font::AvailableFonts();
   SortedStringList faces = GetFaceList( this );
   for ( StringList::const_iterator i = installedFaces.Begin(); i != installedFaces.End(); ++i )
      if ( Font::IsItalicFont( *i ) )
         faces.Remove( *i );
   SetFaceList( this, faces );
}
Example #8
0
int ExternalProcess::ExecuteProgram( const String& program, const StringList& arguments )
{
   Array<const char16_type*> argv;
   for ( StringList::const_iterator i = arguments.Begin(); i != arguments.End(); ++i )
      argv.Add( i->c_str() );
   int retVal = (*API->ExternalProcess->ExecuteProgram)( program.c_str(), argv.Begin(), argv.Length() );
   if ( retVal < -1 )
      ExternalProcessPrivate::Throw( ExternalProcessContext::FailedToStart );
   return retVal;
}
Example #9
0
void FontComboBox::AddFixedPitchFonts()
{
   StringList installedFaces = Font::AvailableFonts();
   SortedStringList faces =  GetFaceList( this );
   for ( StringList::const_iterator i = installedFaces.Begin(); i != installedFaces.End(); ++i )
      if ( !faces.Has( *i ) )
         if ( Font::IsFixedPitchFont( *i ) )
            faces.Add( *i );
   SetFaceList( this, faces );
}
Example #10
0
bool FileDataCacheItem::FromString( const String& s )
{
   path.Clear();
   lastUsed = 0;
   time.year = 0;

   StringList tokens;
   s.Break( tokens, char16_type( '\n' ) );

   for ( StringList::const_iterator i = tokens.Begin(); i != tokens.End(); )
   {
      if ( *i == "path" )
      {
         if ( ++i == tokens.End() )
            return false;
         path = i->Trimmed();
         ++i;
      }
      else if ( *i == "lastUsed" )
      {
         if ( ++i == tokens.End() )
            return false;
         lastUsed = i->ToUInt();
         ++i;
      }
      else if ( *i == "time" )
      {
         if ( tokens.End() - i < 3 )
            return false;
         int y, m, d; double f;
         JDToComplexTime( y, m, d, f, i[1].ToUInt()+0.5 );
         unsigned t = i[2].ToUInt();
         time.year = y;
         time.month = m;
         time.day = d;
         time.milliseconds = t - 86399000;
         time.hour = TruncI( (t = TruncI( t/1000.0 ))/3600.0 );
         time.minute = TruncI( (t -= time.hour*3600)/60.0 );
         time.second = t - time.minute*60;
         i += 3;
      }
      else if ( *i == "data" )
      {
         if ( !GetDataFromTokens( tokens ) )
            return false;
         ++i;
      }
      else
      {
         ++i;
      }
   }

   return !path.IsEmpty() && lastUsed > 0 && time.year > 0 && ValidateData();
}
Example #11
0
ExternalProcess::pid_type ExternalProcess::StartProgram( const String& program, const StringList& arguments, const String& workingDirectory )
{
   Array<const char16_type*> argv;
   for ( StringList::const_iterator i = arguments.Begin(); i != arguments.End(); ++i )
      argv.Add( i->c_str() );
   uint64 pid = 0;
   api_bool ok = (*API->ExternalProcess->StartProgram)( program.c_str(), argv.Begin(), argv.Length(), workingDirectory.c_str(), &pid );
   if ( ok == api_false || pid == 0 )
      ExternalProcessPrivate::Throw( ExternalProcessContext::FailedToStart );
   return pid_type( pid );
}
Example #12
0
void FontComboBox::RemoveWritingSystem( const String& writingSystem )
{
   StringList installedFaces = Font::AvailableFonts();
   SortedStringList faces = GetFaceList( this );
   for ( StringList::const_iterator i = installedFaces.Begin(); i != installedFaces.End(); ++i )
   {
      StringList supportedSystems = Font::AvailableFontWritingSystems( *i );
      if ( supportedSystems.Has( writingSystem ) )
         faces.Remove( *i );
   }
   SetFaceList( this, faces );
}
Example #13
0
   void LoadFiltersFromGlobalString( const IsoString& globalKey )
   {
      filters.Clear();

      String s = PixInsightSettings::GlobalString( globalKey  );
      if ( !s.IsEmpty() )
      {
         for ( size_type p = 0; ; )
         {
            size_type p1 = s.Find( '(', p );
            if ( p1 == String::notFound )
               break;

            size_type p2 = s.Find( ')', p1 );
            if ( p2 == String::notFound )
               break;

            String extStr = s.Substring( p1, p2-p1 );
            if ( !extStr.IsEmpty() )
            {
               StringList extLst;
               extStr.Break( extLst, ' ', true );
               if ( !extLst.IsEmpty() )
               {
                  FileFilter filter;

                  for ( StringList::const_iterator i = extLst.Begin(); i != extLst.End(); ++i )
                  {
                     size_type d = i->Find( '.' );
                     if ( d != String::notFound )
                        filter.AddExtension( i->Substring( d ) );
                  }

                  if ( !filter.Extensions().IsEmpty() )
                  {
                     String desc = s.Substring( p, p1-p );
                     desc.Trim();
                     filter.SetDescription( desc );

                     filters.Add( filter );
                  }
               }
            }

            p = p2 + 1;
         }
      }
   }
Example #14
0
ArgumentList ExtractArguments( const StringList& argv, argument_item_mode mode, ArgumentOptions options )
{
   bool noItems             = mode == ArgumentItemMode::NoItems;
   bool itemsAsFiles        = mode == ArgumentItemMode::AsFiles;
   bool itemsAsViews        = mode == ArgumentItemMode::AsViews;
   bool allowWildcards      = !noItems && options.IsFlagSet( ArgumentOption::AllowWildcards );
   bool noPreviews          = itemsAsViews && options.IsFlagSet( ArgumentOption::NoPreviews );
   bool recursiveDirSearch  = itemsAsFiles && allowWildcards && options.IsFlagSet( ArgumentOption::RecursiveDirSearch );
   bool recursiveSearchArgs = recursiveDirSearch && options.IsFlagSet( ArgumentOption::RecursiveSearchArgs );

   // This is the recursive search mode flag, controlled by --r[+|-]
   bool recursiveSearch = false;

   // The list of existing view identifiers, in case itemsAsViews = true.
   SortedStringList imageIds;

   // The list of extracted arguments
   ArgumentList arguments;

   for ( StringList::const_iterator i = argv.Begin(); i != argv.End(); ++i )
   {
      if ( i->StartsWith( '-' ) )
      {
         Argument arg( i->At( 1 ) );

         if ( recursiveSearchArgs && arg.Id() == s_recursiveSearchArg )
         {
            if ( arg.IsSwitch() )
               recursiveSearch = arg.SwitchState();
            else if ( arg.IsLiteral() )
               recursiveSearch = true;
            else
               arguments.Add( arg );
         }
         else
            arguments.Add( arg );
      }
      else
      {
         if ( noItems )
            throw ParseError( "Non-parametric arguments are not allowed", *i  );

         StringList items;

         if ( itemsAsFiles )
         {
            String fileName = *i;
            if ( fileName.StartsWith( '\"' ) )
               fileName.Delete( 0 );
            if ( fileName.EndsWith( '\"' ) )
               fileName.Delete( fileName.UpperBound() );

            fileName.Trim();
            if ( fileName.IsEmpty() )
               throw ParseError( "Empty path specification", *i );

            fileName = File::FullPath( fileName );

            if ( fileName.HasWildcards() )
            {
               if ( !allowWildcards )
                  throw ParseError( "Wildcards not allowed", fileName );

               items = SearchDirectory( fileName, recursiveSearch );
            }
            else
               items.Add( fileName );
         }
         else if ( itemsAsViews )
         {
            String viewId = *i;

            if ( !allowWildcards )
               if ( viewId.HasWildcards() )
                  throw ParseError( "Wildcards not allowed", viewId );

            size_type p = viewId.Find( "->" );

            if ( p != String::notFound )
            {
               if ( noPreviews )
                  throw ParseError( "Preview identifiers not allowed", viewId );

               String imageId = viewId.Left( p );
               if ( imageId.IsEmpty() )
                  throw ParseError( "Missing image identifier", viewId );

               String previewId = viewId.Substring( p+2 );
               if ( previewId.IsEmpty() )
                  throw ParseError( "Missing preview identifier", viewId );

               FindPreviews( items, imageId, previewId );
            }
            else
            {
               if ( viewId.HasWildcards() )
               {
                  Array<ImageWindow> W = ImageWindow::AllWindows();
                  for ( size_type i = 0; i < W.Length(); ++i )
                  {
                     View v = W[i].MainView();
                     if ( String( v.Id() ).WildMatch( viewId ) )
                        AddView( items, v );
                  }
               }
               else
               {
                  ImageWindow w = ImageWindow::WindowById( IsoString( viewId ) );
                  if ( w.IsNull() )
                     throw ParseError( "Image not found", viewId );
                  AddView( items, w.MainView() );
               }
            }
         }
         else
            items.Add( *i );

         Argument arg( *i, items );
         arguments.Add( arg );
      }
   }

   return arguments;
}
int AssignICCProfileProcess::ProcessCommandLine( const StringList& argv ) const
{
   ArgumentList arguments =
   ExtractArguments( argv,
                     ArgumentItemMode::AsViews,
                     ArgumentOption::AllowWildcards|ArgumentOption::NoPreviews );

   AssignICCProfileInstance instance( this );

   bool launchInterface = false;
   int count = 0;

   for ( ArgumentList::const_iterator i = arguments.Begin(); i != arguments.End(); ++i )
   {
      const Argument& arg = *i;

      if ( arg.IsNumeric() )
         throw Error( "Unknown numeric argument: " + arg.Token() );
      else if ( arg.IsString() )
      {
         if ( arg.Id() == "profile" )
         {
            instance.targetProfile = arg.StringValue();
            instance.targetProfile.Trim();
            if ( instance.targetProfile.IsEmpty() )
               throw Error( "Empty profile identifier: " + arg.Token() );
         }
         else if ( arg.Id() == "filename" )
         {
            String filename = arg.StringValue();
            filename.Trim();
            if ( filename.IsEmpty() )
               throw Error( "Empty file name: " + arg.Token() );

            instance.targetProfile.Clear();

            StringList dirs = ICCProfile::ProfileDirectories();
            for ( StringList::const_iterator i = dirs.Begin(); i != dirs.End(); ++i )
            {
               String path = *i + '/' + filename;
               if ( File::Exists( path ) )
               {
                  ICCProfile icc( path );
                  if ( icc.IsProfile() )
                  {
                     instance.targetProfile = icc.Description();
                     break;
                  }
               }
            }

            if ( instance.targetProfile.IsEmpty() )
               throw Error( "The specified file name does not correspond to a valid ICC profile: " + filename );
         }
         else
            throw Error( "Unknown string argument: " + arg.Token() );
      }
      else if ( arg.IsSwitch() )
      {
         if ( arg.Id() == "default" )
            instance.mode = arg.SwitchState() ? AssignMode::AssignDefaultProfile : AssignMode::AssignNewProfile;
         else if ( arg.Id() == "untag" )
            instance.mode = arg.SwitchState() ? AssignMode::LeaveUntagged : AssignMode::AssignNewProfile;
         else
            throw Error( "Unknown switch argument: " + arg.Token() );
      }
      else if ( arg.IsLiteral() )
      {
         if ( arg.Id() == "default" )
            instance.mode = AssignMode::AssignDefaultProfile;
         else if ( arg.Id() == "untag" )
            instance.mode = AssignMode::LeaveUntagged;
         else if ( arg.Id() == "-interface" )
            launchInterface = true;
         else if ( arg.Id() == "-help" )
         {
            ShowHelp();
            return 0;
         }
         else
            throw Error( "Unknown argument: " + arg.Token() );
      }
      else if ( arg.IsItemList() )
      {
         ++count;

         if ( arg.Items().IsEmpty() )
         {
            Console().WriteLn( "No view(s) found: " + arg.Token() );
            continue;
         }

         for ( StringList::const_iterator j = arg.Items().Begin(); j != arg.Items().End(); ++j )
         {
            View v = View::ViewById( *j );
            if ( v.IsNull() )
               throw Error( "No such view: " + *j );
            instance.LaunchOn( v );
         }
      }
   }

   if ( launchInterface )
      instance.LaunchInterface();
   else if ( count == 0 )
   {
      if ( ImageWindow::ActiveWindow().IsNull() )
         throw Error( "There is no active image window." );
      instance.LaunchOnCurrentWindow();
   }

   return 0;
}
int ICCProfileTransformationProcess::ProcessCommandLine( const StringList& argv ) const
{
   ArgumentList arguments =
   ExtractArguments( argv,
                     ArgumentItemMode::AsViews,
                     ArgumentOption::AllowWildcards|ArgumentOption::NoPreviews );

   ICCProfileTransformationInstance instance( this );

   bool launchInterface = false;
   int count = 0;

   for ( ArgumentList::const_iterator i = arguments.Begin(); i != arguments.End(); ++i )
   {
      const Argument& arg = *i;

      if ( arg.IsNumeric() )
      {
         throw Error( "Unknown numeric argument: " + arg.Token() );
      }
      else if ( arg.IsString() )
      {
         if ( arg.Id() == "profile" )
         {
            instance.targetProfile = arg.StringValue();
            instance.targetProfile.Trim();
            if ( instance.targetProfile.IsEmpty() )
               throw Error( "Empty profile identifier: " + arg.Token() );
         }
         else if ( arg.Id() == "filename" )
         {
            String filename = arg.StringValue();
            filename.Trim();
            if ( filename.IsEmpty() )
               throw Error( "Empty file name: " + arg.Token() );

            instance.targetProfile.Clear();

            StringList dirs = ICCProfile::ProfileDirectories();
            for ( StringList::const_iterator i = dirs.Begin(); i != dirs.End(); ++i )
            {
               String path = *i + '/' + filename;
               if ( File::Exists( path ) )
               {
                  ICCProfile icc( path );
                  if ( icc.IsProfile() )
                  {
                     instance.targetProfile = icc.Description();
                     break;
                  }
               }
            }

            if ( instance.targetProfile.IsEmpty() )
               throw Error( "The specified file name does not correspond to a valid ICC profile: " + filename );
         }
         else if ( arg.Id() == "rendering-intent" )
         {
            if ( arg.StringValue() == "perceptual" )
               instance.renderingIntent = ICCTRenderingIntent::Perceptual;
            else if ( arg.StringValue() == "saturation" )
               instance.renderingIntent = ICCTRenderingIntent::Saturation;
            else if ( arg.StringValue() == "relative" || arg.StringValue() == "relative-colorimetric" )
               instance.renderingIntent = ICCTRenderingIntent::RelativeColorimetric;
            else if ( arg.StringValue() == "absolute" || arg.StringValue() == "absolute-colorimetric" )
               instance.renderingIntent = ICCTRenderingIntent::AbsoluteColorimetric;
            else
               throw Error( "Invalid rendering intent: " + arg.Token() );
         }
         else
            throw Error( "Unknown string argument: " + arg.Token() );
      }
      else if ( arg.IsSwitch() )
      {
         if ( arg.Id() == "to-default-profile" )
            instance.toDefaultProfile = arg.SwitchState();
         else if ( arg.Id() == "black-point-compensation" )
            instance.useBlackPointCompensation = arg.SwitchState();
         else if ( arg.Id() == "floating-point-transform" )
            instance.useFloatingPointTransformation = arg.SwitchState();
         else
            throw Error( "Unknown switch argument: " + arg.Token() );
      }
      else if ( arg.IsLiteral() )
      {
         if ( arg.Id() == "to-default-profile" )
            instance.toDefaultProfile = true;
         else if ( arg.Id() == "black-point-compensation" )
            instance.useBlackPointCompensation = true;
         else if ( arg.Id() == "floating-point-transform" )
            instance.useFloatingPointTransformation = true;
         else if ( arg.Id() == "-interface" )
            launchInterface = true;
         else if ( arg.Id() == "-help" )
         {
            ShowHelp();
            return 0;
         }
         else
            throw Error( "Unknown argument: " + arg.Token() );
      }
      else if ( arg.IsItemList() )
      {
         ++count;

         if ( arg.Items().IsEmpty() )
         {
            Console().WriteLn( "No view(s) found: " + arg.Token() );
            continue;
         }

         for ( StringList::const_iterator j = arg.Items().Begin(); j != arg.Items().End(); ++j )
         {
            View v = View::ViewById( *j );
            if ( v.IsNull() )
               throw Error( "No such view: " + *j );
            instance.LaunchOn( v );
         }
      }
   }

   if ( launchInterface )
      instance.LaunchInterface();
   else if ( count == 0 )
   {
      if ( ImageWindow::ActiveWindow().IsNull() )
         throw Error( "There is no active image window." );
      instance.LaunchOnCurrentWindow();
   }

   return 0;
}