Scene:
1. Under Mac Cocoa Development, you can use Nsuserdefaults to store configuration information easily, and it also supports complex types such as basic types of nsdictionary,nsarray. We use this class to store the runtime configuration,
such as multi-language options, check the update options, shortcut key settings, and so on. This information is automatically stored in the relevant location of the Bundleid.
Reference: http://blog.csdn.net/yepeng2014/article/details/49003753
This configuration is very important, if Windows is the only way to compare with it is the registry, but the registry is not easy to visually through the path to see, more trouble.
The SDK for Windows can really refer to cocoa and provide an API for developers to develop regular software.
2. Here the Nsuserdefaults interface is written in Windows version, currently only simple to store and read the string type, primitive type, object type, then return void*, because C + + does not have an ID type.
3. The main use of Rapidjson as a storage format, the relative nsuserdefaults XML format is slightly simpler, because JSON reads and writes very quickly.
4. It is software-independent and can be used across products.
Bas_user_defaults.h:
#ifndef __bas_user_defaults_h#define __bas_user_defaults_h#include "Bas_exp.h" #include <windows.h>// Use JSON to express content. Class Lib_basic basuserdefaults{public:~basuserdefaults (); static void Initstandarduserdefaults (const wchar_t* brand,const wchar_t* product), static basuserdefaults* standarduserdefaults (), static void Inituserdefaults ( Const char* Name,const wchar_t* path), static basuserdefaults* getuserdefaults (const char* name), void SetString (const char* key,const char* value), void Setint (const char* key,int value), const char* GetString (const char* key);//1. Return to RAPIDJSO N::value object. void* getvalueobject (const char* key), int GetInt (const char* key);D word getrgb (const char* key); void Writeall ();p rivate:basuserdefaults (const char* name,const wchar_t* path); void ReadAll (); char* name_;wchar_t* path_;void* Document_;}; #endif
Bas_user_defaults.cpp:
#include "basic/bas_user_defaults.h" #include <vector> #include <string> #include <map> #include < iostream> #include <stdio.h> #include "rapidjson/document.h" #include "rapidjson/prettywriter.h" #include " Rapidjson/filestream.h "#include" rapidjson/stringbuffer.h "#include" basic/bas_utility_sys.h "static const char* Guserdefaultname = "Userdefault"; static wchar_t* Guserdefaultbrand = null;static wchar_t* guserdefaultproduct = NULL; Static std::map<std::string,basuserdefaults*> gnametodefault;static Rapidjson::D ocument* getdocument (void* Data) {return (Rapidjson::D ocument*) data;} Basuserdefaults::~basuserdefaults () {free (name_), free (path_);} void Basuserdefaults::initstandarduserdefaults (const wchar_t* brand,const wchar_t* product) {Guserdefaultbrand = Wcsdup (brand); guserdefaultproduct = Wcsdup (product); wchar_t* AppData = Basutilitysys::getappdatadir (); Wcscat ( Appdata,guserdefaultbrand):: CreateDirectory (Appdata,null), wcscat (appdata,l "\"); Wcscat (APPDATA,GUSERDEFAULTProduct):: CreateDirectory (Appdata,null), wcscat (appdata,l "\"), Wcscat (appdata,l "Preferences.json"); Nituserdefaults (Guserdefaultname,appdata); free (AppData);} void Basuserdefaults::inituserdefaults (const char* name,const wchar_t* path) {Gnametodefault[name] = new Basuserdefaults (Name,path);} void Basuserdefaults::readall () {rapidjson::D ocument* Document = getdocument (Document_); file* file = _wfopen (path_,l "RB"); if (file) {fseek (file,0,seek_end); Long size = Ftell (file); fseek (File,0,seek_set); char* buf = (char*) malloc (size+1); memset (buf,0,size+1); fread (buf,size,1,file); Buf[size] = 0;fclose (file); if ( Document->parse<0> (BUF). Hasparseerror ()) {std::cout << "Parse Error" << std::endl;document->setobject ();} Free (BUF);}} void Basuserdefaults::writeall () {rapidjson::D ocument* Document = getdocument (Document_); file* file = _wfopen (path_,l "WB"); Rapidjson::stringbuffer buffer; Rapidjson::P rettywriter<rapidjson::stringbuffer> writer (buffer);d ocument->accept (writer); Std::strinG result = buffer. GetString (); fwrite ((void*) Result.data (), Result.size (), 1,file); fclose (file);d elete document;} Basuserdefaults::basuserdefaults (const char* name,const wchar_t* path) {name_ = StrDup (name);p ath_ = wcsdup (path); Rapidjson::D ocument* Document = new Rapidjson::D ocument ();d Ocument_ = Document;document->setobject ();// Reads the JSON content under the path. ReadAll ();} basuserdefaults* basuserdefaults::getuserdefaults (const char* name) {return gnametodefault[name];} basuserdefaults* Basuserdefaults::standarduserdefaults () {return getuserdefaults (guserdefaultname);} void basuserdefaults::setstring (const char* key,const char* temp_value) {rapidjson::D ocument* Document = GetDocument ( DOCUMENT_); Rapidjson::value value (rapidjson::kstringtype); value. SetString (Temp_value,strlen (Temp_value), Document->getallocator ());d Ocument->addmember (Key,value, Document->getallocator ());} int Basuserdefaults::getint (const char* key) {rapidjson::D ocument* Document = getdocument (Document_); return (*document ) [key]. GetInt ();} void* Basuserdefaults::getvalueobject (const char* key) {rapidjson::D ocument* Document = getdocument (Document_); Rapidjson:: value& value = (*document) [Key];return &value;} void Basuserdefaults::setint (const char* key,int temp_value) {rapidjson::D ocument* Document = getdocument (Document_); Rapidjson::value value (rapidjson::knumbertype), value. Setint (Temp_value);d Ocument->addmember (Key,value,document->getallocator ());} Const char* basuserdefaults::getstring (const char* key) {rapidjson::D ocument* Document = getdocument (Document_); return (*document) [Key]. GetString ();} DWORD Basuserdefaults::getrgb (const char* key) {rapidjson::D ocument* Document = getdocument (document_); Const char* Value = (*document) [key]. GetString (); char* temp = strdup (value); char* t = temp;char* p = strchr (temp, ', '); *p = 0;int r = atoi (temp); temp = P+1;p = STRCHR (temp, ', '); *p = 0;int g = atoi (temp); int b = atoi (p+1); free (t); return RGB (R,G,B);}
Usage 1: Similar to nsuserdefaults using the default configuration, default profiles are created based on company name and product name
When the software starts:
Basuserdefaults::initstandarduserdefaults (Uiutilityproduct::getcorporationw (). C_STR (), UiUtilityProduct:: GETPRODUCTALIASW (). C_STR ());
When the software shuts down:
basuserdefaults* user = Basuserdefaults::standarduserdefaults (); User->writeall ();d elete user;
Use case 2: Read the configuration file packaged with the software, read-only.
wchar_t* install_dir_temp = Basutilityapp::getproductinstalldir (); std::wstring Url_path (install_dir_temp); url_ Path.append (L "Option.json"); Basuserdefaults::inituserdefaults ("url", Url_path.c_str ()); free (install_dir_temp);
Common Configuration files:
{ "help": "Http://www.xxx.com/online-help", "purchase": "http://www.xxx.com/purchase/xxx.html", " Usupport ":" http://www.xxx.com/support.html ", " Color:toolbox:background ":" 217,250,243 ", " Color:toolbar: Tab:font ":" 0,0,0 "}
Reference:
Https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/index.html
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Software]_[windows]_[reference nsuserdefaults custom Windows Basuserdefaults]