Пример #1
0
        public static Document ReadFromFile(string pid, string did)
        {
            string   fileName = pid + "\\" + did + ".txt";
            Document finalDoc;

            try
            {
                // Creates a new reader
                StreamReader sr = new StreamReader(fileName);
                using (TextReader tr = sr)
                {
                    // Gets the title from the string and
                    string title = tr.ReadLine();

                    // Gets the path from the string
                    string path = tr.ReadLine();

                    // Gets the name of the owner from the string and makes a user
                    string ownerString = tr.ReadLine();
                    User   owner       = new User(ownerString);

                    // Gets the images id from the file, and add them as picture elements to the list of pictures.
                    string   imageString = tr.ReadLine();
                    string[] imageArray  = imageString.Split(new string[] { "," }, StringSplitOptions.None);

                    List <Picture> pictures = new List <Picture>();
                    foreach (string s in imageArray)
                    {
                        if ((String.Compare(s, "") != 0))
                        {
                            string dir           = Directory.GetCurrentDirectory();
                            string pathToPicture = dir + @"\" + pid + @"\" + s + ".JPG";
                            Bitmap picture       = new Bitmap(pathToPicture);
                            pictures.Add(new Picture(picture, s));
                        }
                    }

                    // Reading the modified and deleted booleans
                    bool modified = Boolean.Parse(tr.ReadLine());
                    bool deleted  = Boolean.Parse(tr.ReadLine());



                    // Create a StringBuilder and append the rest of the file to it, it will contain both the log and the text of the document.
                    StringBuilder rest = new StringBuilder();
                    while (tr.Peek() != -1)
                    {
                        rest.AppendLine(tr.ReadLine());
                    }
                    string restString = rest.ToString();

                    // Splitting on the string "---------------ENDOFLOG------------------", by this we assume the user will NEVER write this exact
                    // string in his document, if he does, our program will not work.
                    string[] restArray = restString.Split(new string[] { "---------------ENDOFLOG------------------" }, StringSplitOptions.None);

                    if (restArray.Length != 2)
                    {
                        throw new Exception("The user wrote the string \"---------------ENDOFLOG------------------\", as part of his text");
                    }

                    // Remove the Mac line feed character from the text string.
                    string textString = Regex.Replace(restArray[1], "\r", "");
                    // Remove normal line feed at start of text.
                    string textS = textString.Substring(1);
                    // Remove normal line feed at end of text.
                    string text = "";
                    if (textS.Length != 0)
                    {
                        text = textS.Substring(0, textS.Length - 1);
                    }

                    // The list of entries the Document's Log will receive
                    List <Document.DocumentLog.Entry> entryList = new List <Document.DocumentLog.Entry>();
                    // Remove Mac line feed from Log.
                    string logWithoutMacLineFeed = Regex.Replace(restArray[0], "\r", "");
                    // Split the log into separate strings for each Entry.
                    string[] entryArray = logWithoutMacLineFeed.Split(new string[] { "Entry" }, StringSplitOptions.None);

                    // This loop takes each string in entryArray ( excluding the first, which is just "Log:" ), and creates an Entry from it.
                    for (int index = 1; index < entryArray.Length; index++)
                    {
                        // Create the Entrys log ( first line being the description, which will be removed before the Entry is created).
                        List <string> log   = new List <string>();
                        string[]      entry = entryArray[index].Split(new string[] { "\n" }, StringSplitOptions.None);
                        for (int index2 = 1; index2 < entry.Length; index2++)
                        {
                            if (!(String.Compare(entry[index2], "") == 0))
                            {
                                log.Add(entry[index2]);
                            }
                        }
                        // Find the first word in the description, which is the Users name.
                        string pattern = @"^\S*";
                        Match  m       = Regex.Match(log[0], pattern);
                        // Create the User object.
                        User user = new User(m.ToString());
                        // Get all remaining characters untill a number is found ( this being the description ).
                        string stringWithoutOwner = log[0].Substring(m.Index + m.Length);
                        pattern = @"\D*";
                        Match m2 = Regex.Match(stringWithoutOwner, pattern);
                        // Trim for whitespaces at start and end of string.
                        string desc = m2.ToString().Trim();
                        // Remove last 7 characters in string ( this part of the string is auto generated by the log, and not actually
                        // part of the description.
                        string description = desc.Substring(0, desc.Length - 7);
                        // Finally get the last part of the string, which is the DateTime of the Entry.
                        string dateString = stringWithoutOwner.Substring(m2.Index + m2.Length);
                        // Remove line feed from date string.
                        string dateStringWithoutLineFeed = Regex.Replace(dateString, "\n", "");
                        // Convert date string into DateTime object.
                        DateTime time = Convert.ToDateTime(dateString);
                        // As mentioned above, first entry in the log was the description, and that should not be part
                        // of the log, so it is removed.
                        log.RemoveAt(0);
                        // Created the Entry object, and add it to the entryList.
                        entryList.Add(new Document.DocumentLog.Entry(user, description, log, time));
                    }


                    tr.ReadToEnd();
                    sr.DiscardBufferedData();
                    tr.Dispose();
                    // Finally makes the document to return
                    finalDoc = Document.CreateDocumentFromFile(did, text, title, modified, deleted, owner, pictures, path, new Document.DocumentLog(entryList));
                }
                //sr.ReadToEnd();
                sr.Dispose();
                sr.Close();

                object o = sr.GetLifetimeService();
                return(finalDoc);
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("No file exists by that name");
                return(null);
            }
        }