Esempio n. 1
0
        public virtual bool writeToFile(cape.File file)
        {
            if (!(file != null))
            {
                return(false);
            }
            if (getIsDirectory())
            {
                return(file.createDirectoryRecursive());
            }
            var reader = getContentReader();

            if (!(reader != null))
            {
                return(false);
            }
            var fp = file.getParent();

            if (fp != null)
            {
                fp.createDirectoryRecursive();
            }
            var writer = file.write();

            if (!(writer != null))
            {
                if (reader is cape.Closable)
                {
                    ((cape.Closable)reader).close();
                }
                return(false);
            }
            var buf = new byte[4096 * 4];
            var v   = true;
            var n   = 0;

            while ((n = reader.read(buf)) > 0)
            {
                var nr = writer.write(buf, n);
                if (nr != n)
                {
                    v = false;
                    break;
                }
            }
            if (!v)
            {
                file.remove();
            }
            if (reader != null && reader is cape.Closable)
            {
                ((cape.Closable)reader).close();
            }
            if (writer != null && writer is cape.Closable)
            {
                ((cape.Closable)writer).close();
            }
            return(v);
        }
Esempio n. 2
0
        public cape.File getRelativeFile(string fileName)
        {
            if (!(file != null))
            {
                return(null);
            }
            if (!(fileName != null))
            {
                return(null);
            }
            var p = file.getParent();

            if (!(p != null))
            {
                return(null);
            }
            return(p.entry(fileName));
        }
        public static capex.data.SQLiteDatabase forFile(cape.File file, bool allowCreate = true, cape.LoggingContext logger = null)
        {
            if (!(file != null))
            {
                return(null);
            }
            var v = capex.data.SQLiteDatabase.instance();

            if (!(v != null))
            {
                return(null);
            }
            if (logger != null)
            {
                v.setLogger(logger);
            }
            if (!file.isFile())
            {
                if (allowCreate == false)
                {
                    return(null);
                }
                var pp = file.getParent();
                if (pp.isDirectory() == false)
                {
                    if (pp.createDirectoryRecursive() == false)
                    {
                        cape.Log.error(v.getLogger() as cape.LoggingContext, "Failed to create directory: " + pp.getPath());
                    }
                }
                if (v.initialize(file, true) == false)
                {
                    v = null;
                }
            }
            else if (v.initialize(file, false) == false)
            {
                v = null;
            }
            return(v);
        }
Esempio n. 4
0
        public static capex.text.TextTemplate forFile(cape.File file, string markerBegin, string markerEnd, int type = 0, System.Collections.Generic.List <cape.File> includeDirs = null, cape.LoggingContext logContext = null)
        {
            if (file == null)
            {
                return(null);
            }
            var text = file.getContentsString("UTF-8");

            if (object.Equals(text, null))
            {
                return(null);
            }
            var ids = includeDirs;

            if (ids == null)
            {
                ids = new System.Collections.Generic.List <cape.File>();
                ids.Add(file.getParent());
            }
            return(capex.text.TextTemplate.forString(text, markerBegin, markerEnd, type, ids, logContext));
        }
Esempio n. 5
0
 public cape.File writeToDir(cape.File dir, bool fullPath = true, bool overwrite = true)
 {
     if (!(dir != null))
     {
         return(null);
     }
     if (!(name != null))
     {
         return(null);
     }
     cape.File path = null;
     if (fullPath == false)
     {
         string nn = null;
         var    r  = cape.String.lastIndexOf(name, '/');
         if (r < 1)
         {
             nn = name;
         }
         else
         {
             nn = cape.String.subString(name, r + 1);
         }
         if (object.Equals(nn, null) || cape.String.getLength(nn) < 1)
         {
             return(null);
         }
         path = dir.entry(nn);
     }
     else
     {
         path = dir;
         var array = cape.String.split(name, '/');
         if (array != null)
         {
             var n = 0;
             var m = array.Count;
             for (n = 0; n < m; n++)
             {
                 var x = array[n];
                 if (x != null)
                 {
                     if (x != null && cape.String.getLength(x) > 0)
                     {
                         path = path.entry(x);
                     }
                 }
             }
         }
         var dd = path.getParent();
         if (!dd.isDirectory())
         {
             dd.createDirectoryRecursive();
         }
         if (!dd.isDirectory())
         {
             return(null);
         }
     }
     if (overwrite == false)
     {
         if (path.exists())
         {
             return(null);
         }
     }
     if (!writeToFile(path))
     {
         return(null);
     }
     return(path);
 }