masscode_merge/merge_masscode.bas
2024-10-14 17:46:25 -04:00

120 lines
4.4 KiB
QBasic

$header
#include "parson.c"
$header
Function main(argc as int, argv as pchar ptr)
dim as JSON_Object Ptr root1, root2
root1 = loadJson("db.json")
root2 = loadJson("mac_db.json")
if root1 and root2 then
addFolderName(root1)
addFolderName(root2)
updateFolderNames(root1, root2)
saveJson(root1, "output\db.json")
closeJson(root1)
closeJson(root2)
end if
End Function
Function loadJson(filename as string) as JSON_Object Ptr
dim as JSON_Value Ptr root
root = json_parse_file(filename)
if root then
return json_value_get_object(root)
end if
return null
End Function
Sub saveJson(obj as JSON_Object Ptr, filename as string)
dim as JSON_Value Ptr obj_value = json_object_get_wrapping_value(obj)
if json_serialize_to_file_pretty(obj_value, filename) = JSONSuccess then
print wrap$(filename), " Successfully Created."
end if
End Sub
Sub closeJson(obj as JSON_Object Ptr)
dim as JSON_Value Ptr root_value = json_object_get_wrapping_value(obj)
json_value_free(root_value)
End Sub
Sub addFolderName(obj as JSON_Object Ptr)
dim as JSON_Array Ptr folder_array, snippet_array
folder_array = json_object_get_array(obj, "folders")
snippet_array = json_object_get_array(obj, "snippets")
dim as uint folder_count = json_array_get_count(folder_array)
dim as uint snippet_count = json_array_get_count(snippet_array)
for uint i = 0 to folder_count -1
dim as JSON_Object Ptr folder = json_array_get_object(folder_array, i)
for uint j = 0 to snippet_count - 1
dim as JSON_Object Ptr snippet = json_array_get_object(snippet_array, j)
if not json_object_get_string_len(snippet, "folderId") = 0 then
if json_object_get_string$(folder, "id") = json_object_get_string$(snippet, "folderId") then
json_object_set_string(snippet, "folderName", json_object_get_string$(folder, "name") )
end if
end if
next
next
End Sub
Function isDuplicate(snippet as JSON_Object Ptr, folder1_snippets as JSON_Array Ptr) as Boolean
dim as uint count = json_array_get_count(folder1_snippets)
for uint i = 0 to count - 1
dim as JSON_Object Ptr fsnippet = json_array_get_object(folder1_snippets, i)
if json_object_get_string$(snippet, "name") = json_object_get_string$(fsnippet, "name") then
return true
end if
next
return false
End Function
Sub updateFolderNames(obj1 as JSON_Object Ptr, obj2 as JSON_Object Ptr)
dim as JSON_Array Ptr folder1_array, folder1_snippets, snippet2_array
folder1_array = json_object_get_array(obj1, "folders")
folder1_snippets = json_object_get_array(obj1, "snippets")
snippet2_array = json_object_get_array(obj2, "snippets")
dim as uint folder1_count = json_array_get_count(folder1_array)
dim as uint folder1_snippets_count = json_array_get_count(folder1_snippets)
dim as uint snippet_count = json_array_get_count(snippet2_array)
' Process snippets with folder names
for uint i = 0 to folder1_count - 1
dim as JSON_Object Ptr folder = json_array_get_object(folder1_array, i)
for uint j = 0 to snippet_count - 1
dim as JSON_Object Ptr snippet = json_array_get_object(snippet2_array, j)
if not json_object_get_string_len(snippet, "folderName") = 0 then
if json_object_get_string$(snippet, "folderName") = json_object_get_string$(folder, "name") then
json_object_set_string(snippet, "folderId", json_object_get_string(folder, "id"))
if not isDuplicate(snippet, folder1_snippets) then
json_array_append_value(folder1_snippets, json_value_deep_copy(json_object_get_wrapping_value(snippet)))
end if
end if
end if
next
next
' Process snippets without folder names (Inbox items)
for uint j = 0 to snippet_count - 1
dim as JSON_Object Ptr snippet = json_array_get_object(snippet2_array, j)
if json_object_get_string_len(snippet, "folderName") = 0 then
if not isDuplicate(snippet, folder1_snippets) then
json_array_append_value(folder1_snippets, json_value_deep_copy(json_object_get_wrapping_value(snippet)))
end if
end if
next
End Sub