Exemplo n.º 1
0
 /// <summary>
 /// Loads the list of flashcards from the user's app data directory
 /// </summary>
 private void LoadCards()
 {
     try
     {
         //attempts to load every file in the card directory
         foreach (string name in Directory.EnumerateFiles(CARD_DIR))
         {
             //if a flashcard can be loaded
             if (Flashcard.TryLoad(Path.GetFileName(name), out Flashcard card))
             {
                 //add it to the list
                 Cards.Add(Path.GetFileName(name), card);
             }
             else
             {
                 //otherwise, delete the file
                 if (File.Exists(name))
                 {
                     File.Delete(name);
                 }
             }
         }
     }
     catch (DirectoryNotFoundException)
     {
         //if the directory doesn't exist, create it
         Directory.CreateDirectory(CARD_DIR);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Attempts to load a flashcard from the given path
        /// </summary>
        /// <param name="filepath">The path from which to load a flashcard</param>
        /// <param name="card">The loaded flashcard</param>
        /// <returns>Whether the operation was successful</returns>
        public static bool TryLoad(string filename, out Flashcard card)
        {
            //sets the output in case of early exit
            card = null;

            //navigates to the card directory
            string filepath = Path.Combine(FLERForm.CARD_DIR, filename);

            if (!File.Exists(filepath))
            {
                return(false); //if the file doesn't exist, the load fails
            }

            using FileStream stream = File.OpenRead(filepath); //the file to be read

            if (!VerifyChecksum(stream))
            {
                return(false); //if it doesn't have a valid checksum, the load fails
            }

            using MemoryStream copy = new MemoryStream(); //a copy of the data without the 96-checksum

            //cuts off the last 96 bits
            stream.Position = 0;
            stream.CopyTo(copy, (int)stream.Length - 12);
            copy.Position = 0;

            using GZipStream deflate = new GZipStream(copy, CompressionMode.Decompress); //a gzip decompression stream
            using StreamReader sr    = new StreamReader(deflate, Encoding.UTF8);         //a string reader to get the json data

            try
            {
                //parse the json data and set the output
                card = JsonConvert.DeserializeObject <Flashcard>(sr.ReadToEnd());

                //if either face doesn't exist, the load fails
                if (card.Visible == null || card.Hidden == null)
                {
                    card = null;
                }

                //sets the filename from which the flashcard was loaded
                card.Filename = filename;

                return(card != null); //returns whether the load was successful
            }
            catch (Exception)
            {
                return(false); //if any errors occurred, the load fails
            }
        }