From 369ff9492df96305ab363b9fc99a616be7a9095a Mon Sep 17 00:00:00 2001 From: hwltony Date: Thu, 18 Apr 2013 23:24:37 +0800 Subject: [PATCH 1/8] json --- src/com/json/JSONOpe.java | 250 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 250 insertions(+) create mode 100644 src/com/json/JSONOpe.java diff --git a/src/com/json/JSONOpe.java b/src/com/json/JSONOpe.java new file mode 100644 index 0000000..24fafd9 --- /dev/null +++ b/src/com/json/JSONOpe.java @@ -0,0 +1,250 @@ +package com.json; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Iterator; + +import junit.framework.TestCase; + +import org.json.JSONException; +import org.json.JSONObject; + +/** + * @author hwl + * + */ +public class JSONOpe extends TestCase{ + //当前json对象 + private JSONObject curobj; + //命令 第一个参数 + private String commandFirst = ""; + //命令第二个参数 + private String commandSecond = ""; + //控制是否退出 + private boolean quitFlag = false; + + /** + * 查看目录 + */ + public void getls(){ + Iterator ite = curobj.keys(); + while(ite.hasNext()){ + System.out.println(ite.next().toString()); + } + } + /** + * 进入到某个目录下 + * @param obj + * @param path + * @param flag + */ + public void getcd(JSONObject obj,String path,boolean flag){ + if(!flag){ + flag = obj.has(path); + if(flag) + try { + curobj = (JSONObject) obj.get(path); + } catch (JSONException e) { + //e.printStackTrace(); + System.out.println("cast error!"); + } + else{ + Iterator ite = obj.keys(); + while(ite.hasNext()){ + String key = ite.next().toString(); + try { + obj = (JSONObject) obj.get(key); + getcd(obj,path,flag); + } catch (JSONException e) { + //e.printStackTrace(); + System.out.println("json is end!"); + } + } + } + } + } + /** + * 查看信息 + * @param key + */ + public void getcat(String key){ + Iterator ite = curobj.keys(); + while(ite.hasNext()){ + String temp = ite.next().toString(); + if(temp.equals(key)){ + JSONObject o; + try { + o = (JSONObject) curobj.get(key); + System.out.println(o); + } catch (JSONException e) { + e.printStackTrace(); + } + + } + } + } + /** + * 增加命令 + */ + public void getadd(){ + System.out.println("key:"); + String key = ""; + key = getfromConsole(); + System.out.println("value:"); + String value = ""; + value = getfromConsole(); + try { + JSONObject temp = new JSONObject(value); + System.out.println("add success!"); + curobj.put(key, temp); + } catch (JSONException e) { + //e.printStackTrace(); + System.out.println("the format is wrong!try again"); + }finally{ + } + + } + /** + * 删除命令 + */ + public void getremove(){ + System.out.println("please give the key:"); + String key = ""; + key = getfromConsole();//接收关键字key + curobj.remove(key); + System.out.println(key+" was deleted from JSON"); + } + /** + * 从控制台读入命令 + * @return + */ + public String getfromConsole(){ + String line = ""; + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + try { + line = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return line; + } + /** + * 分析命令 + * @return + */ + private int readString() { + int com = 0; + String line = null; + line = getfromConsole(); + if("".equals(line)){ + return com; + } + String s[] = line.split(" "); + if(s.length>0&&s.length<3){ + //看第一个参数 + String s0 = s[0]; + if("ls".equals(s0)){ + com = 1; + }else if("cd".endsWith(s0)){ + com = 2; + commandSecond = s[1];//第二个参数 + }else if("cat".equals(s0)){ + com = 3; + commandSecond = s[1]; + }else if("add".equals(s0)){ + com = 4; + }else if("remove".equals(s0)){ + com = 5; + }else if("!help".equals(s0)){ + com = 6; + }else if("!quit".equals(s0)){ + com = 7; + }else{ + return com; + } + }else{ + return com; + } + return com; + } + /** + * 帮助命令 + */ + public void gethelp(){ + StringBuffer sb = new StringBuffer(); + sb.append("ls :command to list the items in current position\n"); + sb.append("cd :command to go to the entry like go to a directory\n"); + sb.append("cat :command to display th item data\n"); + sb.append("add :command to add new address entry to JSON\n"); + sb.append("remove :command to remove one or more address entries\n"); + sb.append("!help :get help\n"); + sb.append("!quit :quit from the application"); + System.out.println(sb.toString()); + } + /** + * 退出命令 + * @param test + */ + public void getquit(JSONOpe test){ + test.setQuitFlag(false); + System.out.println("quited!"); + } + /** + * 程序入口 + * @param args + * @throws JSONException + */ + public static void main(String[] args) throws JSONException { + String sjson = "{\"entries\": {\"lilei\" : {\"age\": 27,\"mobile\" : \"13700000000\",\"address\" : \"Earth somewhere\"}, \"hanmeimei\" : {\"age\": 26,\"mobile\" : \"13700000001\",\"address\" : \"Earth somewhere else\"}}}"; + JSONOpe jsonOpe = new JSONOpe(); + JSONObject root = new JSONObject(sjson); + JSONObject curobj = root; + jsonOpe.setCurobj(curobj); + jsonOpe.setQuitFlag(true); + //循环输入信息 + while(jsonOpe.isQuitFlag()){ + int n = 0; + n = jsonOpe.readString(); + switch(n){ + case 0:System.out.println("input error!type \"!help\" for help\"");break; + case 1:jsonOpe.getls();break; + case 2:{ + boolean flag = false;//每次查询都重置标志位 + curobj = root; + jsonOpe.getcd(curobj,jsonOpe.getCommandSecond(),flag);break; + } + case 3:jsonOpe.getcat(jsonOpe.getCommandSecond());break; + case 4:jsonOpe.getadd();break; + case 5:jsonOpe.getremove();break; + case 6:jsonOpe.gethelp();break; + case 7:jsonOpe.getquit(jsonOpe);break; + default:break; + } + } + } + public JSONObject getCurobj() { + return curobj; + } + public void setCurobj(JSONObject curobj) { + this.curobj = curobj; + } + public String getCommandFirst() { + return commandFirst; + } + public void setCommandFirst(String commandFirst) { + this.commandFirst = commandFirst; + } + public String getCommandSecond() { + return commandSecond; + } + public void setCommandSecond(String commandSecond) { + this.commandSecond = commandSecond; + } + public boolean isQuitFlag() { + return quitFlag; + } + public void setQuitFlag(boolean quitFlag) { + this.quitFlag = quitFlag; + } +} From 9eb9fa7b988eaa2fda63114ccf71d5f7059e6c78 Mon Sep 17 00:00:00 2001 From: Romain2015 Date: Thu, 30 May 2013 20:42:19 +0800 Subject: [PATCH 2/8] Create test --- test | 1 + 1 file changed, 1 insertion(+) create mode 100644 test diff --git a/test b/test new file mode 100644 index 0000000..9daeafb --- /dev/null +++ b/test @@ -0,0 +1 @@ +test From ebd040ff36bfa20ca057eb30f6e917161b5e244d Mon Sep 17 00:00:00 2001 From: Jian Luo Date: Thu, 30 May 2013 21:12:16 +0800 Subject: [PATCH 3/8] First commit from Luo Jian --- Command.cpp | 226 +++++++++++++++ Command.h | 36 +++ JSONList.cpp | 790 +++++++++++++++++++++++++++++++++++++++++++++++++++ JSONList.h | 53 ++++ Main.cpp | 132 +++++++++ data.txt | 1 + 6 files changed, 1238 insertions(+) create mode 100644 Command.cpp create mode 100644 Command.h create mode 100644 JSONList.cpp create mode 100644 JSONList.h create mode 100644 Main.cpp create mode 100644 data.txt diff --git a/Command.cpp b/Command.cpp new file mode 100644 index 0000000..5d46619 --- /dev/null +++ b/Command.cpp @@ -0,0 +1,226 @@ +#include "Command.h" +#include +#include + +using namespace std; + +/** +* Default Constructor +*/ +Command::Command(void) +{ + //string jsonStr = "{\"entries\": {\"lilei\" : {\"age\": 27,\"mobile\" : \"13700000000\",\"address\" : \"Earth somewhere\"}, \"hanmeimei\" : {\"age\": 26,\"mobile\" : \"13700000001\",\"address\" : \"Earth somewhere else\"}}}"; + //m_jsonList = new JSONList(jsonStr); + + m_shellName = "AB"; + + string jsonStr = ReadFile(); + + m_jsonList = new JSONList(jsonStr); +} + +/** +* Constructor +* @param shellName: the name of the shell +*/ +Command::Command(string shellName) +{ + m_shellName = shellName; + + string jsonStr = ReadFile(); + + m_jsonList = new JSONList(jsonStr); +} + +Command::~Command(void) +{ +} + +/** +* Run the command application +*/ +void Command::Run(void) +{ + bool quitFlag = false; + while (!quitFlag) + { + DisplayShell(); + int com = 0; + com = ReadString(); + + switch (com) + { + case 0: + cout << "Command Wrong!\n\n"; + GetHelp(); + break; + case 1: + m_jsonList->getls(); + break; + case 2: + m_jsonList->getcd(m_secondCmd); + break; + case 3: + m_jsonList->getcat(m_secondCmd); + break; + case 4: + m_jsonList->getadd(); + break; + case 5: + m_jsonList->getremove(); + break; + case 6: + GetHelp(); + break; + case 7: + if (m_jsonList->GetDirty()) + { + WriteFile(); + } + quitFlag = true; + break; + default: + break; + } + } +} + +/** +* Display the shell name +*/ +void Command::DisplayShell() +{ + cout << m_shellName << ">"; +} + +/** +* Read a command line from console +*/ +string Command::GetFromConsole() +{ + string line = ""; + getline(cin,line); + return line; +} + +/** +* Analyze the command line +* @return com: command type, 0-7 +*/ +int Command::ReadString() +{ + int com = 0; + string line = ""; + + line = GetFromConsole(); + + // delete the space in front of the command line + int start = 0; + while (line[start] == ' ') + { + start++; + } + line = line.substr(start); + + if (line.empty()) + { + return com; + } + + int i = line.find(" "); + if (i == -1)// only have the first command + { + m_firstCmd = line; + m_secondCmd = ""; + }else + { + m_firstCmd = line.substr(0,i); + m_secondCmd = line.substr(i+1); + + // delete the space in front of the second command + int j = 0; + while (m_secondCmd[j] == ' ') + { + j++; + } + m_secondCmd = '\"' + m_secondCmd.substr(j) + '\"'; + } + + if ("ls" == m_firstCmd && m_secondCmd.empty()) + { + com = 1; + }else if ("cd" == m_firstCmd && !m_secondCmd.empty()) + { + com = 2; + }else if ("cat" == m_firstCmd && !m_secondCmd.empty()) + { + com = 3; + }else if ("add" == m_firstCmd && m_secondCmd.empty()) + { + com = 4; + }else if ("remove" == m_firstCmd && m_secondCmd.empty()) + { + com = 5; + }else if ("!help" == m_firstCmd && m_secondCmd.empty()) + { + com = 6; + }else if ("!quit" == m_firstCmd && m_secondCmd.empty()) + { + com = 7; + } + + return com; +} + +/** +* Read data from file "data.txt" +* @return jsonStr: the data string saved int file "data.txt" +*/ +string Command::ReadFile() +{ + ifstream inFile; + inFile.open("data.txt"); + if (!inFile) + { + cout << "File open error, data load failure!\n"; + exit(0); + } + + string jsonStr; + getline(inFile, jsonStr); + inFile.close(); + + return jsonStr; +} + +/** +* Write data into file "data.txt" when quit from application. +*/ +void Command::WriteFile() +{ + ofstream outFile("data.txt"); + if (!outFile) + { + cout << "File open error, data save failure!\n"; + exit(0); + } + outFile << m_jsonList->GetValue(m_jsonList->GetRootNode()); + outFile.close(); +} + + +/** +* Get help message for the command application +*/ +void Command::GetHelp() +{ + string sb = "Help!\n"; + sb.append("ls :command to list the items in current position\n"); + sb.append("cd [entry] :command to go to the entry like go to a directory\n"); + sb.append("cat [key] :command to display th item data\n"); + sb.append("add :command to add new address entry to JSON\n"); + sb.append("remove [key] :command to remove one or more address entries\n"); + sb.append("!help :commadn to get help\n"); + sb.append("!quit :command to quit from the application\n"); + cout << sb; +} \ No newline at end of file diff --git a/Command.h b/Command.h new file mode 100644 index 0000000..3aa03e1 --- /dev/null +++ b/Command.h @@ -0,0 +1,36 @@ +#ifndef COMMAND +#define COMMAND + +#include"JSONList.h" + +class Command +{ +public: + Command(void); + Command(std::string shell); + ~Command(void); + + void Run(void); + + std::string GetFromConsole(); + + int ReadString(); + + std::string ReadFile(); + + void WriteFile(); + + void DisplayShell(); + + void GetHelp(); + +private: + JSONList *m_jsonList; + + std::string m_firstCmd; + std::string m_secondCmd; + std::string m_shellName; +}; + +#endif + diff --git a/JSONList.cpp b/JSONList.cpp new file mode 100644 index 0000000..e26e4cd --- /dev/null +++ b/JSONList.cpp @@ -0,0 +1,790 @@ +#include "JSONList.h" +#include +//#include + + +using namespace std; + +/** +* Default Constructor +*/ +JSONList::JSONList(void) +{ + m_pRootNode = new JSONNode(); + m_pRootNode->key = "root"; + m_pRootNode->pNext = NULL; + m_pRootNode->pPrev = NULL; + m_pRootNode->pSub = NULL; + m_pCurrNode = NULL; + +} + +/** +* Constructor +* @param jsonStr: a complete JSON data structure for address book +*/ +JSONList::JSONList(string jsonStr) +{ + jsonStr = DeleteChar(jsonStr,' '); + if (!isLegalOuter(jsonStr)) + { + cout << "JSON data structure wrong, constructor failure!\n"; + exit(0); + } + m_dirty = false; + m_pRootNode = new JSONNode(); + m_pRootNode->key = "root"; + m_pRootNode->pPrev = NULL; + m_pRootNode->pNext = NULL; + m_pRootNode->pSub = NULL; + + int i = jsonStr.find("{"); + int j = jsonStr.find(":"); + string key = jsonStr.substr(i+1,j-i-1); + if (key.empty()) + { + exit(0); + } + int k = jsonStr.rfind("}",jsonStr.size()-2); + jsonStr = jsonStr.substr(j+1,k-j); + + JSONNode *node = new JSONNode(); + node->key = key; + if (jsonStr.empty()) + { + node->value = "{}"; + }else + { + node->value = "{}";//jsonStr; + } + + m_pCurrNode = node; + m_pRootNode->pSub = node; + node->pPrev = m_pRootNode; + node->pNext = NULL; + node->pSub = NULL; + + JSONNode * pFlag = m_pCurrNode; + int flag = 1; + while (!jsonStr.empty() && jsonStr != "{}") + { + i = jsonStr.find("\""); + j = jsonStr.find(":"); + k = jsonStr.find("}"); + key = jsonStr.substr(i,j-i); + string value = jsonStr.substr(j+1,k-j); + jsonStr = jsonStr.substr(k+1); + if (jsonStr == "}") + { + jsonStr = ""; + } + JSONNode *node = new JSONNode(); + node->key = key; + node->value = "{}"; + node->pNext = NULL; + if (flag == 1) + { + pFlag->pSub = node; + }else + { + pFlag->pNext = node; + } + flag++; + node->pPrev = pFlag; + pFlag = node; + + int subFlag = 1; + JSONNode *subPFlag = node; + while (!value.empty()) + { + int ii = value.find("\""); + int jj = value.find(":"); + int kk = value.find(","); + string subKey = value.substr(ii,jj-ii); + string subValue = value.substr(jj+1,kk-jj-1); + if (kk == -1) + { + subValue = value.substr(jj+1,value.size()-jj-2); + value = ""; + } + value = value.substr(kk+1); + JSONNode *subNode = new JSONNode(); + subNode->key = subKey; + subNode->value = subValue; + subNode->pNext = NULL; + subNode->pSub = NULL; + if (subFlag == 1) + { + subPFlag->pSub = subNode; + }else + { + subPFlag->pNext = subNode; + } + subFlag++; + subNode->pPrev = subPFlag; + subPFlag = subNode; + } + + } +} + +/** +* Default Destructor +*/ +JSONList::~JSONList(void) +{ + +} + +/** +* List all keys in current directory +*/ +void JSONList::getls() +{ + if (m_pCurrNode == NULL) + { + return; + } + + JSONNode *pTemp = m_pCurrNode; + while (pTemp != NULL) + { + cout << DeleteChar(pTemp->key, '\"') << " "; + pTemp = pTemp->pNext; + } + cout << endl; +} + +/** +* Get the value of a JSONNode recursively +* @return: the value of a given node +*/ +string JSONList::GetValue(JSONNode *node) +{ + if (node->pSub == NULL) + { + return (node->value); + } + + /* + if (node->value == "") + { + node->value = "{}"; + } + node->value = "{}";*/ + + string value = "{}"; + int len; + + JSONNode *pTempNode = node->pSub; + while (pTempNode != NULL) + { + len = value.size(); + value.insert(len-1,pTempNode->key); + + len = value.size(); + value.insert(len-1,":"); + + len = value.size(); + value.insert(len-1,GetValue(pTempNode)); + + pTempNode = pTempNode->pNext; + + if (pTempNode != NULL) + { + len = value.size(); + value.insert(len-1,","); + } + } + + return value; +} + +/** +* Go to a entry like go to a directory +* @param key: the key of a entry +*/ +void JSONList::getcd(string key) +{ + JSONNode *pTempNode = m_pRootNode->pSub; + while (pTempNode != NULL) + { + if (pTempNode->key == key) + { + if (pTempNode->pSub != NULL || m_pRootNode->pSub->pSub == NULL) + { + m_pCurrNode = pTempNode->pSub; + return; + }else if (m_pRootNode->pSub->pSub == NULL) + { + m_pCurrNode = NULL; + cout << "NULL in directory, !help\n"; + return; + } + } + + if (pTempNode->pNext != NULL) + { + pTempNode = pTempNode->pNext; + }else + { + pTempNode = pTempNode->pSub; + } + } + +} + +/** +* Display the value of the key +* @param key: the key +*/ +void JSONList::getcat(string key) +{ + JSONNode *pTempNode = m_pCurrNode; + while (pTempNode != NULL) + { + if (pTempNode->key == key) + { + cout << key << ": " << GetValue(pTempNode) << endl; + } + pTempNode = pTempNode->pNext; + } +} + +/** +* +*/ +void JSONList::getadd() +{ + if (m_pCurrNode != NULL && (m_pCurrNode->pPrev->pPrev == NULL || m_pCurrNode->pPrev->pPrev->key != "root")) + { + cout << "Go to \"entries\" before add!\n"; + return; + } + string key; + string content; + cout << "key: "; + getline(cin, key); + key = '\"' + DeleteChar(key, ' ') + '\"'; + + if (isKeyExist(key)) + { + cout << key << " exist in database, enter a different one, try again!\n"; + return; + } + + cout << "value: "; + getline(cin, content); + content = DeleteChar(content, ' '); + + if (!isLegalInner(key + ':' + content)) + { + cout << "Enter wrong value!\n"; + cout << "Format example:\n key: lilei\n value: {\"age\":28,\"mobile\":\"13700000000\",\"address\":\"China\"}\n"; + return; + } + + JSONNode *node = new JSONNode(); + node->key = key; + node->value = content; + node->pNext = NULL; + node->pSub = NULL; + + JSONNode *pTempNode; + if (m_pCurrNode == NULL) + { + pTempNode = m_pRootNode->pSub; + }else + { + pTempNode = m_pCurrNode; + } + + if (m_pRootNode->pSub->pSub != NULL) + { + while (pTempNode->pNext != NULL) + { + pTempNode = pTempNode->pNext; + } + node->pPrev = pTempNode; + pTempNode->pNext = node; + }else + { + node->pPrev = pTempNode; + pTempNode->pSub = node; + } + + + + content = DeleteChar(content, ' '); + int subFlag = 1; + JSONNode *subPFlag = node; + while (!content.empty()) + { + int ii = content.find("\""); + int jj = content.find(":"); + int kk = content.find(","); + string subKey = content.substr(ii,jj-ii); + string subValue = content.substr(jj+1,kk-jj-1); + if (kk == -1) + { + subValue = content.substr(jj+1,content.size()-jj-2); + content = ""; + } + content = content.substr(kk+1); + JSONNode *subNode = new JSONNode(); + subNode->key = subKey; + subNode->value = subValue; + subNode->pNext = NULL; + subNode->pSub = NULL; + if (subFlag == 1) + { + subPFlag->pSub = subNode; + }else + { + subPFlag->pNext = subNode; + } + subFlag++; + subNode->pPrev = subPFlag; + subPFlag = subNode; + } + m_pCurrNode = m_pRootNode->pSub->pSub; + + m_dirty = true; +} + +/** +* +*/ +void JSONList::getremove() +{ + if (m_pCurrNode->pPrev->pPrev == NULL || m_pCurrNode->pPrev->pPrev->key != "root") + { + cout << "Go to \"entries\" before remove!\n"; + return; + } + cout << "Please give the key: "; + + string key; + getline(cin, key); + key = '\"' + key + '\"'; + JSONNode *pTempNode = m_pCurrNode; + while (pTempNode != NULL) + { + if (pTempNode->key == key) + { + break; + } + pTempNode = pTempNode->pNext; + } + + if (pTempNode == NULL) + { + return; + } + + if (pTempNode->pSub == NULL) + { + if (m_pCurrNode->key == key) + { + m_pCurrNode = m_pCurrNode->pNext; + } + pTempNode->pNext->pPrev = pTempNode->pPrev; + pTempNode->pPrev->pNext = pTempNode->pNext; + delete pTempNode; + pTempNode = NULL; + return; + } + + JSONNode *pSubNode = pTempNode->pSub; + JSONNode *pCurNode = pSubNode; + while (pCurNode != pTempNode) + { + while (pSubNode->pNext != NULL) + { + pSubNode = pSubNode->pNext; + } + pCurNode = pSubNode->pPrev; + delete pSubNode; + pSubNode = NULL; + pCurNode->pNext = NULL; + pSubNode = pCurNode; + } + + pCurNode->pSub = NULL; + pCurNode = pCurNode->pPrev; + delete pTempNode; + pTempNode = NULL; + if (pCurNode->pPrev->key == "root") + { + pCurNode->pSub = NULL; + m_pCurrNode = NULL; + } + pCurNode->pNext = NULL; + //m_pCurrNode = pCurNode; + m_dirty = true; +} + +/** +* +*/ +string JSONList::DeleteChar(string str, char ch) +{ + //string noSpaceStr = ""; + int count = 0; + int len = str.size(); + for (int i = 0; i < len; i++) + { + if (str[i] == ch) + { + count++; + } + } + int *arr = new int[count]; + + int j = 0; + for (int i = 0; i < len; i++) + { + if (str[i] == ch) + { + arr[j++] = i; + if (j == count) + { + break; + } + } + } + + /*for (int i = 0; i < count; i++) + { + cout << arr[i] << endl; + }*/ + + for (int i = count-1; i >= 0; i--) + { + str.erase(arr[i],1); + } + + delete []arr; + + return str; +} + +/*string JSONList::deleteQuote(string str) +{ + int count = 0; + int len = str.size(); + for (int i = 0; i < len; i++) + { + if (str[i] == '\"') + { + count++; + } + } + int *arr = new int[count]; + + int j = 0; + for (int i = 0; i < len; i++) + { + if (str[i] == '\"') + { + arr[j++] = i; + if (j == count) + { + break; + } + } + } + + for (int i = count-1; i >= 0; i--) + { + str.erase(arr[i],1); + } + + delete []arr; + + return str; +}*/ + +bool JSONList::isFormatLegal(string content) +{ + int len = content.size(); + if (content[0] != '{' || content[len-1] != '}') + { + return false; + } + + string str = content.substr(1,len-2); + int i = str.find(","); + while (i != -1) + { + string str0 = str.substr(0,i); + if (str0[0] != '\"') + { + return false; + }else + { + str0 = str0.substr(1); + int j = str0.find("\""); + if (j == -1) + { + return false; + }else if (str0[j+1] != ':') + { + return false; + } + } + str = str.substr(i+1); + i = str.find(","); + } + + if (str.empty()) + { + return false; + }else + { + string str0 = str.substr(0,i); + if (str0[0] != '\"') + { + return false; + }else + { + str0 = str0.substr(1); + int j = str0.find("\""); + if (j == -1) + { + return false; + }else if (str0[j+1] != ':') + { + return false; + } + } + } + + return true; +} + +/** +* +*/ +bool JSONList::isLegalOuter(string content) +{ + int len = content.size(); + if (content[0] != '{' || content[len-1] != '}') + { + return false; + } + + content = content.substr(1,len-2); + len = content.size(); + int i = content.find(':'); + if (i == -1 || i == len-1) + { + return false; + } + + string key = content.substr(0,i); + if (key != "\"entries\"" || content[i+1] != '{' || content[len-1] != '}') + { + return false; + } + + content = content.substr(i+2,len-i-3); + len = content.size(); + if (content.empty()) + { + return true; + } + + i = content.find('}'); + if (i == -1) + { + return false; + } + + //vector strVector; + + while (i < len-1) + { + if (content[i+1] != ',') + { + return false; + } + + //strVector.push_back(content.substr(0,i+1)); + if (!isLegalInner(content.substr(0,i+1))) + { + return false; + } + content = content.substr(i+2); + len = content.size(); + + i = content.find('}'); + if (i == -1) + { + return false; + } + } + //strVector.push_back(content); + if (!isLegalInner(content)) + { + return false; + } + + return true; +} + +/** +* +*/ +bool JSONList::isLegalInner(string content) +{ + int len = content.size(); + int i = content.find(':'); + if (i == -1 || i == len-1 || content[i+1] != '{' || content[len-1] != '}') + { + return false; + } + + string key = content.substr(0,i); + if (key[0] != '\"' || key[key.size()-1] != '\"' || key.substr(1,key.size()-2).empty()) + { + return false; + } + + content = content.substr(i+2,len-i-3); + len = content.size(); + i = content.find(','); + if (i == -1) + { + return false; + } + string str = content.substr(0,i); + int j = str.find(':'); + if (j == -1 || str.substr(0,j) != "\"age\"" || !isNumber(str.substr(j+1)) ) + { + return false; + } + + content = content.substr(i+1); + len = content.size(); + i = content.find(','); + if (i == -1) + { + return false; + } + str = content.substr(0,i); + j = str.find(':'); + if (j == -1 || str.substr(0,j) != "\"mobile\"" || !isMobile(str.substr(j+1)) ) + { + return false; + } + + content = content.substr(i+1); + len = content.size(); + + str = content; + j = str.find(':'); + if (j == -1 || str.substr(0,j) != "\"address\"" || !isAddress(str.substr(j+1)) ) + { + return false; + } + + return true; +} + +/** +* +*/ +bool JSONList::isNumber(string str) +{ + int len = str.size(); + for (int i = 0; i < len; i++) + { + if (str[i] < '0' && str[i] > '9') + { + return false; + } + } + + if (str[0] == '0') + { + return false; + } + + return true; +} + +/** +* +*/ +bool JSONList::isMobile(string str) +{ + int len = str.size(); + if (len != 13) + { + return false; + } + + if (str[0] != '\"' || str[len-1] != '\"') + { + return false; + } + + str = str.substr(1,len-2); + + return isNumber(str); +} + +/** +* +*/ +bool JSONList::isAddress(string str) +{ + int len = str.size(); + if (str[0] != '\"' || str[len-1] != '\"' || str.substr(1,len-2).empty()) + { + return false; + } + return true; +} + +/** +* +*/ +bool JSONList::isKeyExist(string key) +{ + JSONNode *pTempNode = m_pCurrNode; + while (pTempNode !=NULL) + { + if (pTempNode->key == key) + { + return true; + } + pTempNode = pTempNode->pNext; + } + return false; +} + +/** +* +*/ +JSONNode* JSONList::GetRootNode() +{ + return m_pRootNode; +} + +/** +* +*/ +JSONNode* JSONList::GetCurrNode() +{ + return m_pCurrNode; +} + +/** +* +*/ +bool JSONList::GetDirty() +{ + return m_dirty; +} \ No newline at end of file diff --git a/JSONList.h b/JSONList.h new file mode 100644 index 0000000..ee2c5aa --- /dev/null +++ b/JSONList.h @@ -0,0 +1,53 @@ +#ifndef JSON_LIST +#define JSON_LIST + +#include + +struct JSONNode +{ + std::string key; + std::string value; + + JSONNode *pPrev; + JSONNode *pNext; + JSONNode *pSub; +}; + +class JSONList +{ +public: + JSONList(void); + JSONList(std::string jsonStr); + ~JSONList(void); + + void getls(); + void getcd(std::string key); + void getcat(std::string key); + void getadd(); + void getremove(); + + std::string DeleteChar(std::string str, char ch); + //std::string deleteQuote(std::string str); + + bool isFormatLegal(std::string content); + bool isKeyExist(std::string key); + + bool isLegalOuter(std::string content); + bool isLegalInner(std::string content); + bool isNumber(std::string str); + bool isMobile(std::string str); + bool isAddress(std::string str); + + std::string GetValue(JSONNode *node); + + JSONNode *GetRootNode(); + JSONNode *GetCurrNode(); + bool GetDirty(); + +private: + JSONNode *m_pRootNode; + JSONNode *m_pCurrNode; + bool m_dirty;//true if change the the address book +}; + +#endif diff --git a/Main.cpp b/Main.cpp new file mode 100644 index 0000000..78e0e1b --- /dev/null +++ b/Main.cpp @@ -0,0 +1,132 @@ +#include"JSONList.h" +#include"Command.h" +#include + +using namespace std; + +/*string deleteSpaceOfString(string str) +{ + //string noSpaceStr = ""; + int count = 0; + int len = str.size(); + for (int i = 0; i < len; i++) + { + if (str[i] == ' ') + { + count++; + } + } + int *arr = new int[count]; + + int j = 0; + for (int i = 0; i < len; i++) + { + if (str[i] == ' ') + { + arr[j++] = i; + if (j == count) + { + break; + } + } + } + for (int i = 0; i < count; i++) + { + cout << arr[i] << endl; + } + for (int i = count-1; i >= 0; i--) + { + str.erase(arr[i],1); + } + delete []arr; + return str; +}*/ + +int main(int argc, char **argv) +{ + string str = "luojian henhao"; + int i = str.find("jianluo"); + + string name(argv[1]); + size_t loc = name.find_last_of("/"); + if (loc == string::npos) + { + Command cmd; + cmd.Run(); + }else + { + string temp(name.substr(loc+1,name.size()-loc)); + Command cmd(temp); + cmd.Run(); + } + + + /*string jsonStr = "{\"entries\": {\"lilei\" : {\"age\": 27,\"mobile\" : \"13700000000\",\"address\" : \"Earth somewhere\"}, \"hanmeimei\" : {\"age\": 26,\"mobile\" : \"13700000001\",\"address\" : \"Earth somewhere else\"}}}"; + jsonStr = deleteSpaceOfString(jsonStr); + //string str = "\"hello"; + + string key,value; + //getline(cin,key); + //getline(cin,value); + //string temp = ",\"" + key + "\":" + value; + //temp = deleteSpaceOfString(temp); + + JSONList jsonlist(jsonStr); + jsonlist.getls(); + jsonlist.getcd("\"entries\""); + jsonlist.getls(); + string nodeValue = jsonlist.getValue(jsonlist.curr); + cout << jsonlist.curr->key << endl; + cout << nodeValue << endl; + + jsonlist.getcat("\"hanmeimei\""); + + jsonlist.getadd("\"xiaoming\"","{ \"age\": 28, \"mobile\" : \"13700000002\", \"address\" : \"Earth somewhere too\" }"); + jsonlist.getls(); + int len = jsonlist.curr->value.size(); + jsonlist.getcat("\"xiaoming\""); + + jsonlist.getremove("\"xiaoming\""); + //jsonlist.curr->value.insert(len-2,temp); + + jsonlist.getls();*/ + + + /*int i = jsonStr.find("{"); + int j = jsonStr.find(":"); + string key = jsonStr.substr(i+1,j-i); + int k = jsonStr.rfind("}",jsonStr.size()-2); + jsonStr = jsonStr.substr(j+1,k-j); + //i = jsonStr.find("{"); + //j = jsonStr.find(":"); + //key = jsonStr.substr(i+1,j); + //jsonStr = jsonStr.substr(j+1); + //size_t a = strlen(str.c_str()); + + JSONList list; + JSONNode *tmp = new JSONNode(); + tmp->key = key; + tmp->value = jsonStr; + tmp->pPrev = list.root; + list.root->pNext = tmp; + list.curr = tmp; + tmp->pNext = NULL; + + //jsonStr.find_first_of + + cout << list.curr->key << endl; + cout << list.curr->value; + while (!jsonStr.empty()) + { + i = jsonStr.find("{"); + j = jsonStr.find(":"); + k = jsonStr.find("}"); + key = jsonStr.substr(i+1,j-i-1); + jsonStr = jsonStr.substr(j+2,k-j-1); + JSONNode *node = new JSONNode(); + node->key = key; + //node->value = + }*/ + + return 0; +} \ No newline at end of file diff --git a/data.txt b/data.txt new file mode 100644 index 0000000..dd9ab23 --- /dev/null +++ b/data.txt @@ -0,0 +1 @@ +{"entries":{"lilei":{"age":22,"mobile":"12345678900","address":"China"}}} \ No newline at end of file From ba50717627cfb0c9df447e14bda2d550c2afadb0 Mon Sep 17 00:00:00 2001 From: Jian Luo Date: Thu, 30 May 2013 21:22:23 +0800 Subject: [PATCH 4/8] Add makefile by Luo Jian --- makefile | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 makefile diff --git a/makefile b/makefile new file mode 100644 index 0000000..8c5d534 --- /dev/null +++ b/makefile @@ -0,0 +1,10 @@ +AddBook: Main.o Command.o JSONList.o + g++ -o AddBook Main.o Command.o JSONList.o +Main.o: Main.cpp Command.h + g++ -c Main.cpp +Command.o: Command.cpp JSONList.h + g++ -c Command.cpp +JSONList.o: JSONList.cpp + g++ -c JSONList.cpp +clean: + rm AddBook Main.o Command.o JSONList.o From 6252328184fcd3b324c955f13e6592859eddaba4 Mon Sep 17 00:00:00 2001 From: Jian Luo Date: Thu, 30 May 2013 21:25:58 +0800 Subject: [PATCH 5/8] Delete some useless comment codes --- Main.cpp | 108 +------------------------------------------------------ 1 file changed, 1 insertion(+), 107 deletions(-) diff --git a/Main.cpp b/Main.cpp index 78e0e1b..88fdf79 100644 --- a/Main.cpp +++ b/Main.cpp @@ -4,50 +4,12 @@ using namespace std; -/*string deleteSpaceOfString(string str) -{ - //string noSpaceStr = ""; - int count = 0; - int len = str.size(); - for (int i = 0; i < len; i++) - { - if (str[i] == ' ') - { - count++; - } - } - int *arr = new int[count]; - - int j = 0; - for (int i = 0; i < len; i++) - { - if (str[i] == ' ') - { - arr[j++] = i; - if (j == count) - { - break; - } - } - } - for (int i = 0; i < count; i++) - { - cout << arr[i] << endl; - } - for (int i = count-1; i >= 0; i--) - { - str.erase(arr[i],1); - } - delete []arr; - return str; -}*/ - int main(int argc, char **argv) { string str = "luojian henhao"; int i = str.find("jianluo"); - string name(argv[1]); + string name(argv[0]); size_t loc = name.find_last_of("/"); if (loc == string::npos) { @@ -59,74 +21,6 @@ int main(int argc, char **argv) Command cmd(temp); cmd.Run(); } - - - /*string jsonStr = "{\"entries\": {\"lilei\" : {\"age\": 27,\"mobile\" : \"13700000000\",\"address\" : \"Earth somewhere\"}, \"hanmeimei\" : {\"age\": 26,\"mobile\" : \"13700000001\",\"address\" : \"Earth somewhere else\"}}}"; - jsonStr = deleteSpaceOfString(jsonStr); - //string str = "\"hello"; - - string key,value; - //getline(cin,key); - //getline(cin,value); - //string temp = ",\"" + key + "\":" + value; - //temp = deleteSpaceOfString(temp); - - JSONList jsonlist(jsonStr); - jsonlist.getls(); - jsonlist.getcd("\"entries\""); - jsonlist.getls(); - string nodeValue = jsonlist.getValue(jsonlist.curr); - cout << jsonlist.curr->key << endl; - cout << nodeValue << endl; - - jsonlist.getcat("\"hanmeimei\""); - - jsonlist.getadd("\"xiaoming\"","{ \"age\": 28, \"mobile\" : \"13700000002\", \"address\" : \"Earth somewhere too\" }"); - jsonlist.getls(); - int len = jsonlist.curr->value.size(); - jsonlist.getcat("\"xiaoming\""); - - jsonlist.getremove("\"xiaoming\""); - //jsonlist.curr->value.insert(len-2,temp); - - jsonlist.getls();*/ - - - /*int i = jsonStr.find("{"); - int j = jsonStr.find(":"); - string key = jsonStr.substr(i+1,j-i); - int k = jsonStr.rfind("}",jsonStr.size()-2); - jsonStr = jsonStr.substr(j+1,k-j); - //i = jsonStr.find("{"); - //j = jsonStr.find(":"); - //key = jsonStr.substr(i+1,j); - //jsonStr = jsonStr.substr(j+1); - //size_t a = strlen(str.c_str()); - - JSONList list; - JSONNode *tmp = new JSONNode(); - tmp->key = key; - tmp->value = jsonStr; - tmp->pPrev = list.root; - list.root->pNext = tmp; - list.curr = tmp; - tmp->pNext = NULL; - - //jsonStr.find_first_of - - cout << list.curr->key << endl; - cout << list.curr->value; - while (!jsonStr.empty()) - { - i = jsonStr.find("{"); - j = jsonStr.find(":"); - k = jsonStr.find("}"); - key = jsonStr.substr(i+1,j-i-1); - jsonStr = jsonStr.substr(j+2,k-j-1); - JSONNode *node = new JSONNode(); - node->key = key; - //node->value = - }*/ return 0; } \ No newline at end of file From f81a6ed12ed29f473fc314b47331a974418ee0a7 Mon Sep 17 00:00:00 2001 From: Jian Luo Date: Thu, 30 May 2013 21:37:19 +0800 Subject: [PATCH 6/8] delete test --- test | 1 - 1 file changed, 1 deletion(-) delete mode 100644 test diff --git a/test b/test deleted file mode 100644 index 9daeafb..0000000 --- a/test +++ /dev/null @@ -1 +0,0 @@ -test From 48a91020e1ecf7295144e3e9281af5c0d87d72cc Mon Sep 17 00:00:00 2001 From: Jian Luo Date: Thu, 30 May 2013 21:41:37 +0800 Subject: [PATCH 7/8] Delete some useless codes --- Main.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/Main.cpp b/Main.cpp index 88fdf79..8eb12b6 100644 --- a/Main.cpp +++ b/Main.cpp @@ -6,9 +6,6 @@ using namespace std; int main(int argc, char **argv) { - string str = "luojian henhao"; - int i = str.find("jianluo"); - string name(argv[0]); size_t loc = name.find_last_of("/"); if (loc == string::npos) From ab207ba9987546a4f9bc30c3815fc4e1e6290717 Mon Sep 17 00:00:00 2001 From: Jian Luo Date: Fri, 31 May 2013 12:13:43 +0800 Subject: [PATCH 8/8] Final Commit by Luo Jian --- Command.cpp | 11 +- Command.h | 5 + JSONList.cpp | 299 +++++++++++++++++++-------------------------------- JSONList.h | 6 ++ Main.cpp | 7 +- data.txt | 2 +- 6 files changed, 133 insertions(+), 197 deletions(-) diff --git a/Command.cpp b/Command.cpp index 5d46619..aee8334 100644 --- a/Command.cpp +++ b/Command.cpp @@ -1,6 +1,11 @@ +/**Copyright (c) 2013 Jian Luo (romain_cool@163.com) +* Command.cpp +* The implementation of Command class: implement all the functions in Command class +*/ #include "Command.h" #include #include +#include using namespace std; @@ -9,9 +14,6 @@ using namespace std; */ Command::Command(void) { - //string jsonStr = "{\"entries\": {\"lilei\" : {\"age\": 27,\"mobile\" : \"13700000000\",\"address\" : \"Earth somewhere\"}, \"hanmeimei\" : {\"age\": 26,\"mobile\" : \"13700000001\",\"address\" : \"Earth somewhere else\"}}}"; - //m_jsonList = new JSONList(jsonStr); - m_shellName = "AB"; string jsonStr = ReadFile(); @@ -34,6 +36,7 @@ Command::Command(string shellName) Command::~Command(void) { + delete m_jsonList; } /** @@ -223,4 +226,4 @@ void Command::GetHelp() sb.append("!help :commadn to get help\n"); sb.append("!quit :command to quit from the application\n"); cout << sb; -} \ No newline at end of file +} diff --git a/Command.h b/Command.h index 3aa03e1..390de31 100644 --- a/Command.h +++ b/Command.h @@ -1,3 +1,7 @@ +/**Copyright (c) 2013 Jian Luo (romain_cool@163.com) +* Command.h +* Command class: acts as the interface between the user and the application +*/ #ifndef COMMAND #define COMMAND @@ -29,6 +33,7 @@ class Command std::string m_firstCmd; std::string m_secondCmd; + std::string m_shellName; }; diff --git a/JSONList.cpp b/JSONList.cpp index e26e4cd..e7b8645 100644 --- a/JSONList.cpp +++ b/JSONList.cpp @@ -1,7 +1,10 @@ +/**Copyright (c) 2013 Jian Luo (romain_cool@163.com) +* JSONList.cpp +* The implementation of JSONList class: implement all the functions in JSONList class +*/ #include "JSONList.h" #include -//#include - +#include using namespace std; @@ -31,6 +34,7 @@ JSONList::JSONList(string jsonStr) cout << "JSON data structure wrong, constructor failure!\n"; exit(0); } + m_dirty = false; m_pRootNode = new JSONNode(); m_pRootNode->key = "root"; @@ -74,10 +78,12 @@ JSONList::JSONList(string jsonStr) key = jsonStr.substr(i,j-i); string value = jsonStr.substr(j+1,k-j); jsonStr = jsonStr.substr(k+1); + if (jsonStr == "}") { jsonStr = ""; } + JSONNode *node = new JSONNode(); node->key = key; node->value = "{}"; @@ -108,6 +114,7 @@ JSONList::JSONList(string jsonStr) value = ""; } value = value.substr(kk+1); + JSONNode *subNode = new JSONNode(); subNode->key = subKey; subNode->value = subValue; @@ -133,71 +140,57 @@ JSONList::JSONList(string jsonStr) */ JSONList::~JSONList(void) { - + DeleteNode(&m_pRootNode); + m_pRootNode = NULL; } /** -* List all keys in current directory +* */ -void JSONList::getls() +void JSONList::DeleteNode(JSONNode **node) { - if (m_pCurrNode == NULL) + if (*node == NULL) { return; } - JSONNode *pTemp = m_pCurrNode; - while (pTemp != NULL) + if ((*node)->pSub == NULL && (*node)->pNext == NULL) { - cout << DeleteChar(pTemp->key, '\"') << " "; - pTemp = pTemp->pNext; + JSONNode *pTempNode = (*node)->pPrev; + + delete (*node); + (*node) = NULL; + + return; } - cout << endl; + + DeleteNode(&(*node)->pNext); + (*node)->pNext = NULL; + + DeleteNode(&(*node)->pSub); + (*node)->pSub = NULL; + + DeleteNode(&(*node)); + (*node) = NULL; } /** -* Get the value of a JSONNode recursively -* @return: the value of a given node +* List all keys in current directory */ -string JSONList::GetValue(JSONNode *node) +void JSONList::getls() { - if (node->pSub == NULL) - { - return (node->value); - } - - /* - if (node->value == "") + if (m_pCurrNode == NULL) { - node->value = "{}"; + return; } - node->value = "{}";*/ - string value = "{}"; - int len; - - JSONNode *pTempNode = node->pSub; - while (pTempNode != NULL) + JSONNode *pTemp = m_pCurrNode; + while (pTemp != NULL) { - len = value.size(); - value.insert(len-1,pTempNode->key); - - len = value.size(); - value.insert(len-1,":"); - - len = value.size(); - value.insert(len-1,GetValue(pTempNode)); - - pTempNode = pTempNode->pNext; - - if (pTempNode != NULL) - { - len = value.size(); - value.insert(len-1,","); - } + cout << DeleteChar(pTemp->key, '\"') << " "; + pTemp = pTemp->pNext; } - - return value; + cout << endl; } /** @@ -252,7 +245,8 @@ void JSONList::getcat(string key) } /** -* +* Add command: add a node with a given key and a given value +* Enter key and value with a given format */ void JSONList::getadd() { @@ -313,8 +307,6 @@ void JSONList::getadd() pTempNode->pSub = node; } - - content = DeleteChar(content, ' '); int subFlag = 1; JSONNode *subPFlag = node; @@ -353,7 +345,8 @@ void JSONList::getadd() } /** -* +* Delete command: delete a node with a given key +* Enter a key which exists in database */ void JSONList::getremove() { @@ -382,100 +375,79 @@ void JSONList::getremove() return; } - if (pTempNode->pSub == NULL) - { - if (m_pCurrNode->key == key) - { - m_pCurrNode = m_pCurrNode->pNext; - } - pTempNode->pNext->pPrev = pTempNode->pPrev; - pTempNode->pPrev->pNext = pTempNode->pNext; - delete pTempNode; - pTempNode = NULL; - return; - } + JSONNode *pTempPrev = pTempNode->pPrev; - JSONNode *pSubNode = pTempNode->pSub; - JSONNode *pCurNode = pSubNode; - while (pCurNode != pTempNode) + if (pTempNode->pPrev->pPrev->key == "root") { - while (pSubNode->pNext != NULL) + pTempPrev->pSub = pTempNode->pNext; + + m_pCurrNode = m_pCurrNode->pNext; + if (m_pCurrNode != NULL) { - pSubNode = pSubNode->pNext; + m_pCurrNode->pPrev = m_pRootNode->pSub; } - pCurNode = pSubNode->pPrev; - delete pSubNode; - pSubNode = NULL; - pCurNode->pNext = NULL; - pSubNode = pCurNode; - } - - pCurNode->pSub = NULL; - pCurNode = pCurNode->pPrev; - delete pTempNode; - pTempNode = NULL; - if (pCurNode->pPrev->key == "root") + m_pRootNode->pSub->pSub = m_pCurrNode; + }else { - pCurNode->pSub = NULL; - m_pCurrNode = NULL; + pTempPrev->pNext = pTempNode->pNext; } - pCurNode->pNext = NULL; - //m_pCurrNode = pCurNode; + + DeleteNode(&(pTempNode->pSub)); + delete (pTempNode); + m_dirty = true; } /** -* +* Get the value of a JSONNode recursively +* @return: the value of a given node */ -string JSONList::DeleteChar(string str, char ch) +string JSONList::GetValue(JSONNode *node) { - //string noSpaceStr = ""; - int count = 0; - int len = str.size(); - for (int i = 0; i < len; i++) + if (node->pSub == NULL) { - if (str[i] == ch) - { - count++; - } + return (node->value); } - int *arr = new int[count]; - int j = 0; - for (int i = 0; i < len; i++) - { - if (str[i] == ch) - { - arr[j++] = i; - if (j == count) - { - break; - } - } - } + string value = "{}"; + int len; - /*for (int i = 0; i < count; i++) + JSONNode *pTempNode = node->pSub; + while (pTempNode != NULL) { - cout << arr[i] << endl; - }*/ + len = value.size(); + value.insert(len-1,pTempNode->key); - for (int i = count-1; i >= 0; i--) - { - str.erase(arr[i],1); - } + len = value.size(); + value.insert(len-1,":"); - delete []arr; + len = value.size(); + value.insert(len-1,GetValue(pTempNode)); - return str; + pTempNode = pTempNode->pNext; + + if (pTempNode != NULL) + { + len = value.size(); + value.insert(len-1,","); + } + } + + return value; } -/*string JSONList::deleteQuote(string str) +/** +* Delete a given char from a string +* @param str +* @param ch +*/ +string JSONList::DeleteChar(string str, char ch) { int count = 0; int len = str.size(); for (int i = 0; i < len; i++) { - if (str[i] == '\"') + if (str[i] == ch) { count++; } @@ -485,7 +457,7 @@ string JSONList::DeleteChar(string str, char ch) int j = 0; for (int i = 0; i < len; i++) { - if (str[i] == '\"') + if (str[i] == ch) { arr[j++] = i; if (j == count) @@ -503,68 +475,12 @@ string JSONList::DeleteChar(string str, char ch) delete []arr; return str; -}*/ - -bool JSONList::isFormatLegal(string content) -{ - int len = content.size(); - if (content[0] != '{' || content[len-1] != '}') - { - return false; - } - - string str = content.substr(1,len-2); - int i = str.find(","); - while (i != -1) - { - string str0 = str.substr(0,i); - if (str0[0] != '\"') - { - return false; - }else - { - str0 = str0.substr(1); - int j = str0.find("\""); - if (j == -1) - { - return false; - }else if (str0[j+1] != ':') - { - return false; - } - } - str = str.substr(i+1); - i = str.find(","); - } - - if (str.empty()) - { - return false; - }else - { - string str0 = str.substr(0,i); - if (str0[0] != '\"') - { - return false; - }else - { - str0 = str0.substr(1); - int j = str0.find("\""); - if (j == -1) - { - return false; - }else if (str0[j+1] != ':') - { - return false; - } - } - } - - return true; } /** -* +* Judge the format of the outer of a JSON data structure +* Format: {"entries": {"xx": {}, "yy": {}}} +* @param content */ bool JSONList::isLegalOuter(string content) { @@ -601,8 +517,6 @@ bool JSONList::isLegalOuter(string content) return false; } - //vector strVector; - while (i < len-1) { if (content[i+1] != ',') @@ -610,7 +524,6 @@ bool JSONList::isLegalOuter(string content) return false; } - //strVector.push_back(content.substr(0,i+1)); if (!isLegalInner(content.substr(0,i+1))) { return false; @@ -624,7 +537,7 @@ bool JSONList::isLegalOuter(string content) return false; } } - //strVector.push_back(content); + if (!isLegalInner(content)) { return false; @@ -634,7 +547,9 @@ bool JSONList::isLegalOuter(string content) } /** -* +* Judge the format of the inner of a JSON data structure +* Format: lilei: {\"age\":28,\"mobile\":\"13700000000\",\"address\":\"China\"} +* @param content */ bool JSONList::isLegalInner(string content) { @@ -693,7 +608,8 @@ bool JSONList::isLegalInner(string content) } /** -* +* Judge the format of a number +* @param str */ bool JSONList::isNumber(string str) { @@ -715,7 +631,8 @@ bool JSONList::isNumber(string str) } /** -* +* Judge the format of a mobile number +* @param str */ bool JSONList::isMobile(string str) { @@ -736,7 +653,8 @@ bool JSONList::isMobile(string str) } /** -* +* Judge the format of an address +* @param str */ bool JSONList::isAddress(string str) { @@ -749,7 +667,8 @@ bool JSONList::isAddress(string str) } /** -* +* Find a node with a given key +* @param key */ bool JSONList::isKeyExist(string key) { @@ -766,7 +685,7 @@ bool JSONList::isKeyExist(string key) } /** -* +* Get the root node */ JSONNode* JSONList::GetRootNode() { @@ -774,7 +693,7 @@ JSONNode* JSONList::GetRootNode() } /** -* +* Get the current node */ JSONNode* JSONList::GetCurrNode() { @@ -782,9 +701,9 @@ JSONNode* JSONList::GetCurrNode() } /** -* +* Get the dirty flag */ bool JSONList::GetDirty() { return m_dirty; -} \ No newline at end of file +} diff --git a/JSONList.h b/JSONList.h index ee2c5aa..3f6d1f7 100644 --- a/JSONList.h +++ b/JSONList.h @@ -1,3 +1,7 @@ +/**Copyright (c) 2013 Jian Luo (romain_cool@163.com) +* JSONList.h +* JSONList class: acts as the implementation of the commands of the application +*/ #ifndef JSON_LIST #define JSON_LIST @@ -26,6 +30,8 @@ class JSONList void getadd(); void getremove(); + void DeleteNode(JSONNode **node); + std::string DeleteChar(std::string str, char ch); //std::string deleteQuote(std::string str); diff --git a/Main.cpp b/Main.cpp index 8eb12b6..4d1a2c7 100644 --- a/Main.cpp +++ b/Main.cpp @@ -1,6 +1,9 @@ +/**Copyright (c) 2013 Jian Luo (romain_cool@163.com) +* Main.cpp +* The entrance of the command application +*/ #include"JSONList.h" #include"Command.h" -#include using namespace std; @@ -20,4 +23,4 @@ int main(int argc, char **argv) } return 0; -} \ No newline at end of file +} diff --git a/data.txt b/data.txt index dd9ab23..0b0416b 100644 --- a/data.txt +++ b/data.txt @@ -1 +1 @@ -{"entries":{"lilei":{"age":22,"mobile":"12345678900","address":"China"}}} \ No newline at end of file +{"entries":{"lilei":{"age":22,"mobile":"12345678900","address":"China"},"hanmeimei":{"age":22,"mobile":"12345678900","address":"China"},"xiaoming":{"age":22,"mobile":"12345678900","address":"China"}}} \ No newline at end of file