Represents the location of a directory or a file inside a kOS. It contains a volumeId and a VolumePath.
Inheritance: VolumePath
Exemple #1
0
 private void AssignSourcePathToSection(GlobalPath filePath, IEnumerable<Opcode> section)
 {
     foreach (Opcode opcode in section)
     {
         opcode.SourcePath = filePath;
     }
 }
Exemple #2
0
 public void AttachTo(TermWindow termWindow, Volume attachVolume, GlobalPath path)
 {
     term = termWindow;
     WindowRect = new Rect(0, 0, 470, 280); // will be resized and moved in onGUI.
     frozen = false;
     loadingVolume = attachVolume;
     loadingPath = path;
     LoadContents(attachVolume, path);
 }
Exemple #3
0
        public bool Copy(GlobalPath sourcePath, GlobalPath destinationPath, bool verifyFreeSpace = true)
        {
            Volume sourceVolume = GetVolumeFromPath(sourcePath);
            Volume destinationVolume = GetVolumeFromPath(destinationPath);

            VolumeItem source = sourceVolume.Open(sourcePath);
            VolumeItem destination = destinationVolume.Open(destinationPath);

            if (source == null)
            {
                throw new KOSPersistenceException("Path does not exist: " + sourcePath);
            }

            if (source is VolumeDirectory)
            {
                if (destination is VolumeFile)
                {
                    throw new KOSPersistenceException("Can't copy directory into a file");
                }

                if (destination == null)
                {
                    destination = destinationVolume.CreateDirectory(destinationPath);
                }
                else if (!sourcePath.IsRoot)
                {
                    destinationPath = destinationPath.Combine(sourcePath.Name);
                    destination = destinationVolume.OpenOrCreateDirectory(destinationPath);
                }

                if (destination == null)
                {
                    throw new KOSException("Path was expected to point to a directory: " + destinationPath);
                }

                return CopyDirectory(sourcePath, destinationPath, verifyFreeSpace);
            } else
            {
                if (destination is VolumeFile || destination == null)
                {
                    Volume targetVolume = GetVolumeFromPath(destinationPath);
                    return CopyFile(source as VolumeFile, destinationPath, targetVolume, verifyFreeSpace);
                }
                else
                {
                    return CopyFileToDirectory(source as VolumeFile, destination as VolumeDirectory, verifyFreeSpace);
                }
            }
        }
Exemple #4
0
        public override List<CodePart> Compile(GlobalPath filePath, int startLineNum, string scriptText, string contextId, CompilerOptions options)
        {
            var parts = new List<CodePart>();
            ParseTree parseTree = parser.Parse(scriptText);
            if (parseTree.Errors.Count == 0)
            {
                var compiler = new Compiler();
                LoadContext(contextId);

                CodePart mainPart;
                try
                {
                    mainPart = compiler.Compile(startLineNum, parseTree, currentContext, options);
                }
                catch (KOSCompileException e)
                {
                    e.AddSourceText((short)startLineNum, scriptText);
                    throw;
                }

                // add locks and triggers
                parts.AddRange(currentContext.UserFunctions.GetNewParts());
                parts.AddRange(currentContext.Triggers.GetNewParts());
                parts.AddRange(currentContext.Subprograms.GetNewParts());

                parts.Add(mainPart);

                AssignSourceId(parts, filePath);

                //if (contextId != "interpreter") _cache.AddToCache(scriptText, parts);
            }
            else
            {
                // TODO: Come back here and check on the possibility of reporting more
                // errors than just the first one.  It appears that TinyPG builds a
                // whole array of error messages so people could see multiple syntax
                // errors in one go if we supported the reporting of it.  It may be that
                // it was deliberately not done because it might be too verbose that way
                // for the small text terminal.

                ParseError error = parseTree.Errors[0];
                throw new KOSParseException(error, scriptText);
            }

            return parts;
        }
Exemple #5
0
        public bool Move(GlobalPath sourcePath, GlobalPath destinationPath)
        {
            if (sourcePath.IsRoot)
            {
                throw new KOSPersistenceException("Can't move root directory: " + sourcePath);
            }

            if (sourcePath.IsParent(destinationPath))
            {
                throw new KOSPersistenceException("Can't move directory to a subdirectory of itself: " + destinationPath);
            }

            Volume sourceVolume = GetVolumeFromPath(sourcePath);
            Volume destinationVolume = GetVolumeFromPath(destinationPath);

            bool verifyFreeSpace = sourceVolume != destinationVolume;

            if (!Copy(sourcePath, destinationPath, verifyFreeSpace))
            {
                return false;
            }

            if (!sourceVolume.Delete(sourcePath))
            {
                throw new KOSPersistenceException("Can't remove: " + sourcePath);
            }

            return true;
        }
Exemple #6
0
        public Volume GetVolumeFromPath(GlobalPath path)
        {
            Volume volume = GetVolume(path.VolumeId);

            if (volume == null)
            {
                throw new KOSPersistenceException("Volume not found: " + path.VolumeId);
            }

            return volume;
        }
Exemple #7
0
        private void CompareDirectories(GlobalPath dir1Path, GlobalPath dir2Path)
        {
            Volume dir1Volume = volumeManager.GetVolumeFromPath(dir1Path);
            Volume dir2Volume = volumeManager.GetVolumeFromPath(dir2Path);

            VolumeDirectory dir1 = dir1Volume.Open(dir1Path) as VolumeDirectory;
            VolumeDirectory dir2 = dir2Volume.Open(dir2Path) as VolumeDirectory;

            Assert.NotNull(dir1);
            Assert.NotNull(dir2);

            int dir1Count = dir1.List().Count;
            int dir2Count = dir2.List().Count;

            if (dir1Count != dir2Count)
            {
                Assert.Fail("Item count not equal: " + dir1Count + " != " + dir2Count);
            }

            foreach (KeyValuePair<string, VolumeItem> pair in dir1.List())
            {
                VolumeItem dir2Item = dir2Volume.Open(dir2Path.Combine(pair.Key));

                if (pair.Value is VolumeDirectory && dir2Item is VolumeDirectory)
                {
                    CompareDirectories(dir1Path.Combine(pair.Key), dir2Path.Combine(pair.Key));
                } else if (pair.Value is VolumeFile && dir2Item is VolumeFile)
                {
                    VolumeFile file1 = pair.Value as VolumeFile;
                    VolumeFile file2 = dir2Item as VolumeFile;

                    Assert.AreEqual(file1.ReadAll(), file2.ReadAll());
                } else
                {
                    Assert.Fail("Items are not of the same type: " + dir1Path.Combine(pair.Key) + ", " + dir2Path.Combine(pair.Key));
                }
            }
        }
Exemple #8
0
 public PathValue(GlobalPath path, SafeSharedObjects sharedObjects)
     : this()
 {
     Path = path;
     this.sharedObjects = sharedObjects;
 }
Exemple #9
0
 public bool IsParent(GlobalPath path)
 {
     return VolumeId.Equals(path.VolumeId) && base.IsParent(path);
 }
Exemple #10
0
        private string BuildLocationString(GlobalPath path, int line)
        {
            if (line < 0)
            {
                // Special exception - if line number is negative then this isn't from any
                // line of user's code but from the system itself (like the triggers the compiler builds
                // to recalculate LOCK THROTTLE and LOCK STEERING each time there's an Update).
                return "(kOS built-in Update)";
            }

            return string.Format("{0}, line {1}", path, line);
        }
Exemple #11
0
 private void AssignSourceId(IEnumerable<CodePart> parts, GlobalPath filePath)
 {
     currentContext.LastSourcePath = filePath;
     foreach (CodePart part in parts)
     {
         part.AssignSourceName(currentContext.LastSourcePath);
     }
 }
Exemple #12
0
 public bool IsParent(GlobalPath path)
 {
     return(VolumeId.Equals(path.VolumeId) && base.IsParent(path));
 }
Exemple #13
0
 /// <summary>
 /// Compile source text into compiled codeparts.
 /// </summary>
 /// <param name="filePath">The name that should get reported to the user on
 /// runtime errors in this compiled code. Even if the text is not from an
 /// actual file this should still be a pseudo-filename for reporting, for
 /// example "(commandline)" or "(socket stream)"
 /// </param>
 /// <param name="startLineNum">Assuming scriptText is a subset of some bigger buffer, line 1 of scripttext
 /// corresponds to line (what) of the more global something, for reporting numbers on errors.</param>
 /// <param name="scriptText">The text to be compiled.</param>
 /// <param name="contextId">The name of the runtime context (i.e. "interpreter").</param>
 /// <param name="options">settings for the compile</param>
 /// <returns>The CodeParts made from the scriptText</returns>
 public abstract List<CodePart> Compile(GlobalPath filePath, int startLineNum, string scriptText, string contextId, CompilerOptions options);
Exemple #14
0
 /// <summary>
 /// Compile source text into compiled codeparts.
 /// </summary>
 /// <param name="filePath">The name that should get reported to the user on
 /// runtime errors in this compiled code. Even if the text is not from an
 /// actual file this should still be a pseudo-filename for reporting, for
 /// example "(commandline)" or "(socket stream)"
 /// </param>
 /// <param name="startLineNum">Assuming scriptText is a subset of some bigger buffer, line 1 of scripttext
 /// corresponds to line (what) of the more global something, for reporting numbers on errors.</param>
 /// <param name="scriptText">The text to be compiled.</param>
 /// <param name="contextId">The name of the runtime context (i.e. "interpreter").</param>
 /// <returns>The CodeParts made from the scriptText</returns>
 public virtual List<CodePart> Compile(GlobalPath filePath, int startLineNum, string scriptText, string contextId)
 {
     return Compile(filePath, startLineNum, scriptText, contextId, new CompilerOptions());
 }
Exemple #15
0
 /// <summary>
 /// Compile source text into compiled codeparts.
 /// </summary>
 /// <param name="filePath">The name that should get reported to the user on
 /// runtime errors in this compiled code. Even if the text is not from an
 /// actual file this should still be a pseudo-filename for reporting, for
 /// example "(commandline)" or "(socket stream)"
 /// </param>
 /// <param name="startLineNum">Assuming scriptText is a subset of some bigger buffer, line 1 of scripttext
 /// corresponds to line (what) of the more global something, for reporting numbers on errors.</param>
 /// <param name="scriptText">The text to be compiled.</param>
 /// <returns>The CodeParts made from the scriptText</returns>
 public virtual List<CodePart> Compile(GlobalPath filePath, int startLineNum, string scriptText)
 {
     return Compile(filePath, startLineNum, scriptText, string.Empty);
 }
Exemple #16
0
        protected bool CopyDirectory(GlobalPath sourcePath, GlobalPath destinationPath, bool verifyFreeSpace)
        {
            if (sourcePath.IsParent(destinationPath))
            {
                throw new KOSPersistenceException("Can't copy directory to a subdirectory of itself: " + destinationPath);
            }

            Volume sourceVolume = GetVolumeFromPath(sourcePath);
            Volume destinationVolume = GetVolumeFromPath(destinationPath);

            VolumeDirectory source = sourceVolume.Open(sourcePath) as VolumeDirectory;

            VolumeItem destinationItem = destinationVolume.Open(destinationPath);

            if (destinationItem is VolumeFile)
            {
                throw new KOSPersistenceException("Can't copy directory into a file");
            }

            VolumeDirectory destination = destinationItem as VolumeDirectory;

            if (destination == null)
            {
                destination = destinationVolume.CreateDirectory(destinationPath);
            }

            var l = source.List();

            foreach (KeyValuePair<string, VolumeItem> pair in l)
            {
                if (pair.Value is VolumeDirectory)
                {
                    if (!CopyDirectory(sourcePath.Combine(pair.Key), destinationPath.Combine(pair.Key), verifyFreeSpace))
                    {
                        return false;
                    }
                }
                else
                {
                    if (!CopyFileToDirectory(pair.Value as VolumeFile, destination, verifyFreeSpace))
                    {
                        return false;
                    }
                }
            }

            return true;
        }
Exemple #17
0
 protected bool CopyFile(VolumeFile volumeFile, GlobalPath destinationPath, Volume targetVolume,
     bool verifyFreeSpace)
 {
     return targetVolume.SaveFile(destinationPath, volumeFile.ReadAll(), verifyFreeSpace) != null;
 }
Exemple #18
0
        private string GetSourceLine(GlobalPath path, int line)
        {
            string returnVal = "(Can't show source line)";

            if (line < 0)
            {
                // Special exception - if line number is negative then this isn't from any
                // line of user's code but from the system itself (like the triggers the compiler builds
                // to recalculate LOCK THROTTLE and LOCK STEERING each time there's an Update).
                return "<<System Built-In Flight Control Updater>>";
            }

            if (path is InternalPath)
            {
                return (path as InternalPath).Line(line);
            }

            Volume vol;

            try
            {
                vol = Shared.VolumeMgr.GetVolumeFromPath(path);
            }
            catch (KOSPersistenceException)
            {
                return returnVal;
            }

            VolumeFile file = vol.Open(path) as VolumeFile;
            if (file != null)
            {
                if (file.ReadAll().Category == FileCategory.KSM)
                    return  "<<machine language file: can't show source line>>";

                string[] splitLines = file.ReadAll().String.Split('\n');
                if (splitLines.Length >= line)
                {
                    returnVal = splitLines[line-1];
                }
            }

            return returnVal;
        }
Exemple #19
0
 public void OpenPopupEditor(Volume v, GlobalPath path)
 {
     popupEditor.AttachTo(this, v, path);
     popupEditor.Open();
 }
Exemple #20
0
 public void LoadContents(Volume vol, GlobalPath path)
 {
     if (isDirty)
     {
         Freeze(true);
         InvokeDirtySaveLoadDialog();
         loadingVolume = vol;
         loadingPath = path;
     }
     else
     {
         loadingVolume = vol;
         loadingPath = path;
         DelegateLoadContents(this);
     }
 }
Exemple #21
0
        /// <summary>
        /// Create a GlobalPath from a base path and a relative path.
        /// </summary>
        /// <returns>GlobalPath that represents the new path.</returns>
        /// <param name="pathString">Path string relative to basePath.</param>
        /// <param name="basePath">Base path.</param>
        public static GlobalPath FromStringAndBase(string pathString, GlobalPath basePath)
        {
            if (IsAbsolute(pathString))
            {
                throw new KOSInvalidPathException("Relative path expected", pathString);
            }

            if (pathString.Equals(CurrentDirectoryPath))
            {
                return basePath;
            }

            List<string> mergedSegments = new List<string>();
            mergedSegments.AddRange(basePath.Segments);
            mergedSegments.AddRange(GetSegmentsFromString(pathString));

            return new GlobalPath(basePath.VolumeId, mergedSegments);
        }
Exemple #22
0
        public void SetupVolumes()
        {
            dir1Path = GlobalPath.FromString("0:" + dir1);
            subdir1Path = dir1Path.Combine(subdir1);
            subdir2Path = dir1Path.Combine(subdir2);
            subsubdir1Path = subdir1Path.Combine(subsubdir1);

            file1Path = GlobalPath.FromString("0:" + file1);
            dir1File1Path = dir1Path.Combine(file1);
            dir1File2Path = dir1Path.Combine(file2);
            dir1File3Path = dir1Path.Combine(file3);
            subdir1File1Path = subdir1Path.Combine(file1);
            subsubdir1File1Path = subsubdir1Path.Combine(file1);

            SourceVolume.Clear();
            TargetVolume.Clear();

            SourceVolume.CreateDirectory(subdir2Path);
            SourceVolume.CreateDirectory(subsubdir1Path);

            SourceVolume.CreateFile(file1Path).WriteLn(file1);
            SourceVolume.CreateFile(dir1File3Path).WriteLn(file2);
            SourceVolume.CreateFile(subsubdir1File1Path).WriteLn("subsubdir1File1");
        }
Exemple #23
0
 public PathValue FromPath(GlobalPath path)
 {
     return new PathValue(path, sharedObjects);
 }
Exemple #24
0
 public void AssignSourceName(GlobalPath filePath)
 {
     AssignSourcePathToSection(filePath, FunctionsCode);
     AssignSourcePathToSection(filePath, InitializationCode);
     AssignSourcePathToSection(filePath, MainCode);
 }