예제 #1
0
        /// <summary>
        /// Generates JSON formatted data structures a set of structures and their associated fields and exports the data to a JSON file
        /// specified in the FileName parameter.  This export file will contain the relevant information for each structure and be in a format
        /// that will allow for it to be loaded into a web page (such as the StructInfo.html page of the CHeaderParser program) or any
        /// other program that can consume and process JSON formatted data.
        /// The Structure/Union JSON export file will be in the following JSON format:
        /// "structs": [ { "structname",  "structdata": { "datasize", "fieldcount", "elements",
        ///                                                                 "fields": [ { "offset", "fieldtypename", "fieldname",
        ///                                                                                "fieldindex", "datasize", "elements" } ] } }]
        /// </summary>
        /// <param name="aryStructNames">An array of strings, containing the names of each struct to export.</param>
        /// <param name="strFileName">The full name and path of the struture/union export data file.  It will either be a JSON file or JS file
        /// containing all the JSON data assigned to a global variable.</param>
        /// <param name="exportType">Indicates whether the export file will be generated so that it can be loaded into a page hosted
        /// either locally or on a web server.   If an export data file is to be generated that is to be used in a locally hosted web file, then
        /// a javascript file will be generated that will load the entire exported JSON data structure into a global variable.  When the a web
        /// file hosted on a server is used, the data will be exported to a standard JSON formatted data file that can be consumed by the
        /// server hosted  web application.</param>
        public bool ExportStructs(string[] aryStructNames, string strFileName, WebExportTypeEnum exportType)
        {
            try
            {
                CHeaderDataSet.tblStructuresRow[] aryStructs = new CHeaderDataSet.tblStructuresRow[aryStructNames.Length];

                for (int i = 0; i < aryStructNames.Length; i++)
                {
                    aryStructs[i] = m_HeaderAccess.GetStruct(aryStructNames[i]);
                }

                return(ExportStructs(aryStructs, strFileName, exportType));
            }
            catch (Exception err)
            {
                ErrorHandler.ShowErrorMessage(err, "Error in ExportStructs Overload 2 function of DataExport class.");
                return(false);
            }
        }
예제 #2
0
        /// <summary>
        /// Generates JSON formatted data structures a set of structures and their associated fields and exports the data to a JSON file
        /// specified in the FileName parameter.  This export file will contain the relevant information for each structure and be in a format
        /// that will allow for it to be loaded into a web page (such as the StructInfo.html page of the CHeaderParser program) or any
        /// other program that can consume and process JSON formatted data.
        /// The Structure/Union JSON export file will be in the following JSON format:
        /// "structs": [ { "structname",  "structdata": { "datasize", "fieldcount", "elements",
        ///                                                                 "fields": [ { "offset", "fieldtypename", "fieldname",
        ///                                                                                "fieldindex", "datasize", "elements" } ] } }]
        /// </summary>
        /// <param name="aryStructs">Array of structures to export into JSON formatted export data file.</param>
        /// <param name="strFileName">The full name and path of the struture/union export data file.  It will either be a JSON file or JS file
        /// containing all the JSON data assigned to a global variable.</param>
        /// <param name="exportType">Indicates whether the export file will be generated so that it can be loaded into a page hosted
        /// either locally or on a web server.   If an export data file is to be generated that is to be used in a locally hosted web file, then
        /// a javascript file will be generated that will load the entire exported JSON data structure into a global variable.  When the a web
        /// file hosted on a server is used, the data will be exported to a standard JSON formatted data file that can be consumed by the
        /// server hosted  web application.</param>
        public bool ExportStructs(CHeaderDataSet.tblStructuresRow[] aryStructs, string strFileName, WebExportTypeEnum exportType)
        {
            FileStream fs = null;

            try
            {
                fs = File.Create(strFileName);
                StreamWriter swrt = new StreamWriter(fs);

                string strJSONExportData = "";

                if (exportType == WebExportTypeEnum.Local)
                {
                    //If a locally hosted web page is to consume the exported JSON data, the file will be saved as a javascript file that will
                    //have all the exported JSON data assigned to a global variable named jsonExportData.  The locally hosted web page
                    //can then add a source to the script to allow for the global variable to be loaded statically along with the web page.
                    StringBuilder sbJSONExportData = new StringBuilder(GenerateStructJSONExportData(aryStructs));
                    sbJSONExportData.Insert(0, "var jsonExportData = ");
                    sbJSONExportData.Append(";");

                    strJSONExportData = sbJSONExportData.ToString();
                }
                else
                {
                    strJSONExportData = GenerateStructJSONExportData(aryStructs);
                }//end if

                swrt.Write(strJSONExportData);
                swrt.Flush();
                swrt.Close();

                fs = null;

                return(true);
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("Access to the directory is denied!", "Export Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            catch (Exception err)
            {
                ErrorHandler.ShowErrorMessage(err, "Error in ExportStructs Overload 1 function of DataExport class.");
                return(false);
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }