Homemade Instant Chat Background - Write a Configuration, Read Configuration

Keywords: C++ JSON ftp socket less

I'm ashamed that I haven't studied well for half a year. Six months ago, I thought about how I would do if I were asked to make a game chat module. Although I have some ideas, I just want to think about it. Now, use your spare time to achieve it.

Affected by the work, I also want to define all defines and referenced header files in one file:

 1  ///
 2  /// @file    define.h
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-07-13 21:57:13
 5  ///
 6 
 7 #ifndef __DEFINE_H__
 8 #define __DEFINE_H__
 9  
10 #include <json/json.h>
11 #include <iostream>
12 #include <fstream>
13 #include <sstream>
14 
15 namespace marrs{
16 
17 using namespace std;
18 
19 #define BASE_CONF_PATH "../conf/modular.conf"
20 
21 #define BASE_CONF_KEY_IP "Ip"
22 
23 typedef short                Int_16;
24 typedef int                  Int_32;
25 typedef long                 Int_64;
26 typedef unsigned short       UInt_16;
27 typedef unsigned int         UInt_32;
28 typedef unsigned long        UInt_64;
29 typedef char                 Char;
30 typedef string               String;
31 typedef void                 Void;
32 
33 typedef short*               pInt_16;
34 typedef int*                 pInt_32;
35 typedef long*                pInt_64;
36 typedef unsigned short*      pUInt_16;
37 typedef unsigned int*        pUInt_32;
38 typedef unsigned long*       pUInt_64;
39 typedef char*                pChar; 
40 typedef void*                pVoid;
41 
42 }
43 
44 #endif

 

The first few lines compile. vimrc in the home directory, and each time a new class of files is generated, they are written into the file by default.

 1 autocmd BufNewFile *.[ch],*.hpp,*.cpp,*.cc exec ":call Addreadme()"
 2 
 3 function Addreadme()
 4     call setline(1, " ///")
 5     call append(1, " /// @file    " .expand("%:t"))
 6     call append(2, " /// @author  marrs(chenchengxi993@gmail.com)")
 7     call append(3, " /// @date    ".strftime("%Y-%m-%d %H:%M:%S"))
 8     call append(4, " ///")
 9     call append(5, " ")
10     call append(6, "#include <iostream>")
11     call append(7, " ")
12     call append(8, "namespace marrs{")
13     call append(9, " ")
14     call append(10, "using std::cout;")
15     call append(11, "using std::endl;")
16     call append(12, " ")
17     call append(13, " ")
18     call append(14, " ")
19     call append(15, "}")
20 endf 

Then it's a personal habit to write the configuration and read the configuration first.

Considering the ftp and Mini Search Engine we wrote before, let's write a configuration for socket. Personal preferences are in json format, as follows:

{
    "Ip" : "192.168.188.128",
    "Port" : 2000,
}

 

Well, there's a little bit less content. Let's do it first, and then we need to add it later.

The advantage of Writing Configuration is that if something that may change is written dead and changed, the program will be recompiled. With the configuration file, just restart it, or even do not need to restart, tell the program to reload the file. Let's start with a simple way to read the configuration.

 1  ///
 2  /// @file    conf.h
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-07-13 21:49:14
 5  ///
 6 
 7 #ifndef __CONF_H__
 8 #define __CONF_H__
 9 
10 #include "define.h"
11 
12 namespace marrs{
13 
14 class BaseConf
15 {
16     public:
17         BaseConf();
18         ~BaseConf();
19         Void LoadConf();
20 
21     public:
22         Int_32 GetVal(String str_key, Int_32 & rInt32_value);
23         UInt_32 GetVal(String str_key, UInt_32 & rUInt32_value);
24         String GetVal(String str_key, String & str_value);
25 
26     private:
27         Json::Value jsnData;
28         Json::FastWriter jsnWriter;
29         Json::Reader jsnReader;
30 };
31 
32 }
33 
34 #endif
 1  ///
 2  /// @file    conf.cc
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-07-13 22:32:32
 5  ///
 6  
 7 #include "conf.h"
 8 
 9 namespace marrs{
10 
11 BaseConf::BaseConf()
12 {
13     LoadConf();    
14 }
15 
16 BaseConf::~BaseConf()
17 {
18     //do nothing
19 }
20 
21 Void BaseConf::LoadConf()
22 {
23     ifstream ifs_conf;
24     ifs_conf.open(BASE_CONF_PATH);
25     if (!ifs_conf.good())
26     {
27         //todo  log and exit
28         return;
29     }
30     
31     if(!jsnReader.parse(ifs_conf, jsnData))
32     {
33         //todo log and exit
34         return;
35     }
36 }
37 
38 Int_32 BaseConf::GetVal(String str_key, Int_32 & rInt32_value)
39 {
40     if(jsnData.isMember(str_key))
41     {
42         rInt32_value = jsnData[str_key].asInt();
43     }
44     return rInt32_value;
45 }
46 
47 UInt_32 BaseConf::GetVal(String str_key, UInt_32 & rUInt32_value)
48 {
49     if(jsnData.isMember(str_key))
50     {
51         rUInt32_value = jsnData[str_key].asUInt();
52     }
53     return rUInt32_value;
54 }
55 
56 
57 String BaseConf::GetVal(String str_key, String & str_value)
58 {
59     if(jsnData.isMember(str_key))
60     {
61         str_value = jsnData[str_key].asString();
62     }
63     return str_value;
64 }
65 
66 }//end namespace

The next step is to test whether it can be read successfully.

 1  ///
 2  /// @file    main.cc
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-07-13 23:11:35
 5  ///
 6 
 7 #include "base/conf.h"
 8 
 9 using namespace marrs;
10 
11 int main()
12 {
13     BaseConf conf;
14     String str_ip;
15     conf.GetVal(BASE_CONF_KEY_IP, str_ip);
16     cout << str_ip << endl;
17     return 0;
18 }
19  

Running results:

[ccx@ubuntu ~/chat/src]$>./main.exe 
192.168.188.128

It meets expectations.

Let's stop here today. Write a little in a day and you can always finish it.

  https://github.com/ccx19930930/my_Instant_chat

Posted by liam1412 on Thu, 13 Jun 2019 17:25:59 -0700