Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:28

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file Create_Files.cs
0005  *
0006  * @brief Format a USB memory stick and create files on the stick
0007  * Small C# application which formats a USB memory stick with a FAT
0008  * file system and creates files on the memory stick. The files will
0009  * have names with various multibyte strings.
0010  * The whole procedure is part of creating a MS Windows FAT disk image
0011  * whith files with exotic multibyte filenames. The image will get 
0012  * mounted under RTEMS for testing the mutibyte/UTF-8 feature of the
0013  * RTEMS FAT file system and the compatibility to MS Windows.
0014  */
0015 
0016 /*
0017  * Copyright (c) 2013 embedded brains GmbH & Co. KG
0018  *
0019  * Redistribution and use in source and binary forms, with or without
0020  * modification, are permitted provided that the following conditions
0021  * are met:
0022  * 1. Redistributions of source code must retain the above copyright
0023  *    notice, this list of conditions and the following disclaimer.
0024  * 2. Redistributions in binary form must reproduce the above copyright
0025  *    notice, this list of conditions and the following disclaimer in the
0026  *    documentation and/or other materials provided with the distribution.
0027  *
0028  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0029  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0030  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0031  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0032  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0033  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0034  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0035  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0036  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0037  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0038  * POSSIBILITY OF SUCH DAMAGE.
0039  */
0040 using System;
0041 using System.Collections.Generic;
0042 using System.IO;
0043 using System.Diagnostics;
0044 using System.Threading;
0045 using System.Text;
0046 
0047 namespace Create_Files
0048 {
0049     public static class Create_Files
0050     {
0051         // Strings for file names and file contents
0052         public static string[] Strings = new string[] { 
0053                 "this is a long filename", 
0054                 "đây là một tên tập tin dài",
0055                 "Bu uzun bir dosya adı",
0056                 "هذا هو اسم ملف طويل",
0057                 "αυτό είναι ένα μεγάλο όνομα αρχείου",
0058                 "это длинное имя",
0059                 "гэта доўгае імя",
0060                 "това е дълго име на файла",
0061                 "这是一个长文件名",
0062                 "মেৰিকা মহাদেশ, উত্তৰ আমেৰিকা আৰু দক্ষিণ আমেৰিকা মহাদেশক লৈ গঠিত এক",
0063                 "آمریکا قاره یکته قارهٰ زمینˇ قاره‌ٰنˇ مئن ایسسه کی زمینˇ هنه‌شر (مساحت)ˇ جی ۳۸٪ و زمینˇ خوشکی‌ئنˇ جی ۴۲۸٪ ای قاره شی ایسسه", 
0064                 "Manâhestôtse 910,720,588 (July 2008 est.)",
0065                 "Elle s'étend depuis l'océan Arctique au nord jusqu'au cap Horn dans le passage de Drake au sud, à la confluence des océans", 
0066                 "ཨ་མེ་རི་ཀ, ཨ་མེ་རི་ཁ, མེ་གླིང་", 
0067                 "е су земље западне хемисфере или Новог света које се састоје од континената Северна Америка", 
0068                 "This is a filename with with 255 characters. The following numbers are aligned in that way, that the character 0 is the mentioned one. xx140xxxxxxx150xxxxxxx160xxxxxxx170xxxxxxx180xxxxxxx190xxxxxxx200xxxxxxx210xxxxxxx220xxxxxxx230xxxxxxx240xxxxxxx250xxxxx", 
0069                 "Bu gezegen Roma mitolojisindeki savaş ilahı Mars'a", 
0070                 "Amerike su zemlje zapadne hemisfere ili Novog svijeta koje se sastoje od kontinenata Sjeverna Amerika i Južna Amerika sa svim pridruženim otocima i regijama.", 
0071                 "იანებს ორ კონტინენტს, სამხრეთ და ჩრდილოეთ ამერიკას ახლომდებარე კუნძულებთან ერ",
0072                 " Є то єдиный контінент, котрого цїла теріторія лежыть на Западній півкулї тай разом"
0073         };
0074 
0075         // Use the features of MS Windows to format the USB memory stick. We want a genuine Microsoft FAT file system
0076         public static void FormatDrive(string driveLetter)
0077         {
0078             ProcessStartInfo StartInfo = new ProcessStartInfo();
0079             StartInfo.FileName = Environment.SystemDirectory + "\\cmd.exe";
0080             StartInfo.Arguments = "/C \"format " + driveLetter + " /FS:FAT\"";
0081             StartInfo.UseShellExecute = false;
0082             StartInfo.RedirectStandardInput = true;
0083             Process Process = Process.Start(StartInfo);
0084             //Thread.Sleep(1000);
0085             Process.StandardInput.WriteLine();
0086             Process.StandardInput.WriteLine();
0087             Process.WaitForExit();
0088         }
0089         // Format a USB meory stick and create files on the new volume
0090         // args[0] The drive to be formatted. E.g. "e:"
0091         public static void Main(string[] args)
0092         {
0093             // Display help text on the console
0094             if ((args.Length <= 0) || (args[0].Equals("-h", StringComparison.InvariantCultureIgnoreCase) || args[0].Equals("-help", StringComparison.InvariantCultureIgnoreCase)))
0095             {
0096                 Console.WriteLine("create_files.bat <DRIVE>");
0097                 Console.WriteLine("Will format DRIVE and create files on the new formated drive.");
0098             }
0099             else
0100             {
0101                 // Show a warning
0102                 Console.WriteLine(args[0] + " will get formated!");
0103                 while (true)
0104                 {
0105                     Console.WriteLine("Press y to continue or press n to abort. [y\\n]");
0106                     string Input = Console.ReadLine();
0107                     if (Input[0].Equals('y') || Input[0].Equals('Y'))
0108                         break;
0109                     else if (Input[0].Equals('n') || Input[0].Equals('N'))
0110                         return;
0111                 }
0112                 // Format the USB memory stick
0113                 FormatDrive(args[0]);
0114 
0115                 // Create the files and write their own file names into them
0116                 for (int i = 0; i < Strings.GetLength(0); i++)
0117                 {
0118                     File.WriteAllText(Path.Combine(args[0], Strings[i]), Strings[i], Encoding.UTF8);
0119                     Console.WriteLine("The file \"" + Strings[i]  + "\" created.");
0120                 }
0121 
0122                 // Create a c header file which contains an array with the strings and a #define 
0123                 // for the number of strings
0124                 string HeaderPath = Path.Combine(args[0]/*Environment.CurrentDirectory*/, "files.h");
0125                 Console.WriteLine("The header \"" + HeaderPath + "\" will write.");
0126                 StreamWriter HeaderStream = new StreamWriter(HeaderPath);
0127 
0128                 HeaderStream.Write("\n" +
0129                                     "/*\n" +
0130                                     " *  Array with files, that were created in the FAT-filesystem image.bin.\n" +
0131                                     " *\n" +
0132                                     " *  WARNING: Automatically generated by Create_Files.cs -- do not edit!\n" +
0133                                     " */\n" +
0134                                     "\n" +
0135                                     "#ifndef __FILE_H__\n" +
0136                                     "#define __FILE_H__\n" +
0137                                     "\n" +
0138                                     "#ifdef __cplusplus\n" +
0139                                     "extern C {\n" +
0140                                     "#endif\n" +
0141                                     "\n" +
0142                                     "static const char *const filenames[] = {\n");
0143                 for (int i = 0; i < Strings.GetLength(0); i++)
0144                     HeaderStream.WriteLine("  \"" + Strings[i] + ((i == (Strings.GetLength(0) - 1)) ? "\"" : "\","));
0145                 string NumberOfFilesStr = Strings.GetLength(0).ToString(System.Globalization.CultureInfo.InvariantCulture);
0146                 HeaderStream.Write("};\n" +
0147                                     "#define FILES_FILENAMES_NUMBER_OF " + NumberOfFilesStr + "\n" +
0148                                     "\n" +
0149                                     "#ifdef __cplusplus\n" +
0150                                     "}\n" +
0151                                     "#endif\n" +
0152                                     "\n" +
0153                                     "#endif /* __FILE_H__ */\n" +
0154                                     "\n");
0155 
0156                 // Finalize
0157                 HeaderStream.Flush();
0158                 HeaderStream.Close();
0159                 HeaderStream.Dispose();
0160             }
0161         }
0162     }
0163 }