diff --git a/.github/workflows/macos_gunittest.cfg b/.github/workflows/macos_gunittest.cfg index 9a124d438a5..65c839474c1 100644 --- a/.github/workflows/macos_gunittest.cfg +++ b/.github/workflows/macos_gunittest.cfg @@ -16,7 +16,6 @@ exclude = python/grass/script/testsuite/test_script_doctests.py python/grass/temporal/testsuite/unittests_temporal_raster_algebra_equal_ts.py python/grass/temporal/testsuite/unittests_temporal_raster_conditionals_complement_else.py - raster/r.contour/testsuite/testrc.py raster/r.in.gdal/testsuite/test_r_in_gdal.py raster/r.in.lidar/testsuite/test_base_resolution.sh raster/r.in.lidar/testsuite/test_base_resolution.sh diff --git a/.github/workflows/super-linter.yml b/.github/workflows/super-linter.yml index 6d206560acc..f40f071f0d8 100644 --- a/.github/workflows/super-linter.yml +++ b/.github/workflows/super-linter.yml @@ -21,11 +21,19 @@ jobs: runs-on: ubuntu-latest + permissions: + contents: read + packages: read + # To report GitHub Actions status checks + statuses: write + steps: - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 - name: Lint code base - uses: github/super-linter@v5 + uses: super-linter/super-linter/slim@v5.7.2 env: + DEFAULT_BRANCH: main + # To report GitHub Actions status checks GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Listed but commented out linters would be nice to have. # (see https://github.com/github/super-linter#environment-variables) diff --git a/.github/workflows/verify-success.yml b/.github/workflows/verify-success.yml index 8ebeb7c4665..237c239f576 100644 --- a/.github/workflows/verify-success.yml +++ b/.github/workflows/verify-success.yml @@ -101,10 +101,10 @@ jobs: echo "cancelled=${{ inputs.fail_if_cancelled && fromJson(steps.has-result.outputs.cancelled) && 1 || 0 }}" >> "$GITHUB_OUTPUT" - echo "skipped=${{ inputs.fail_if_skipped && + echo "skipped=${{ inputs.fail_if_skipped && fromJson(steps.has-result.outputs.skipped) && 1 || 0 }}" >> "$GITHUB_OUTPUT" - echo "success=${{ inputs.require_success && + echo "success=${{ inputs.require_success && !fromJson(steps.has-result.outputs.success) && 1 || 0 }}" >> "$GITHUB_OUTPUT" - name: Set messages for each job result type diff --git a/.gunittest.cfg b/.gunittest.cfg index d68237e12fe..19c2cc950dd 100644 --- a/.gunittest.cfg +++ b/.gunittest.cfg @@ -16,7 +16,6 @@ exclude = python/grass/script/testsuite/test_script_doctests.py python/grass/temporal/testsuite/unittests_temporal_raster_algebra_equal_ts.py python/grass/temporal/testsuite/unittests_temporal_raster_conditionals_complement_else.py - raster/r.contour/testsuite/testrc.py raster/r.in.gdal/testsuite/test_r_in_gdal.py raster/r.in.lidar/testsuite/test_base_resolution.sh raster/r.in.lidar/testsuite/test_base_resolution.sh diff --git a/Makefile b/Makefile index e2f389cf0de..c95128f3cec 100644 --- a/Makefile +++ b/Makefile @@ -91,6 +91,7 @@ $(ARCH_DISTDIR)/%: % $(INSTALL_DATA) $< $@ LIBDIRS = \ + lib/external/parson \ lib/external/shapelib \ lib/datetime \ lib/gis \ diff --git a/doc/infrastructure.md b/doc/infrastructure.md index fb9c371bf27..ebe914b6ef2 100644 --- a/doc/infrastructure.md +++ b/doc/infrastructure.md @@ -261,7 +261,7 @@ User settings: - The GRASS GIS CI user at Docker hub is "grassgis" (joined June 3, 2023), see also -- Docker Hub access token are managed via grass-ci-admin@osgeo.org. +- Docker Hub access token are managed via . - The OSGeo Org membership is managed at through OSGeo-SAC diff --git a/grasslib.dox b/grasslib.dox index ee2323ddb30..25d6ee9179d 100644 --- a/grasslib.dox +++ b/grasslib.dox @@ -122,7 +122,7 @@ href="https://grass.osgeo.org">https://grass.osgeo.org \subsection misclibs Miscellaneous Libraries - datetime: \ref datetime (DateTime library) - - external: \ref external (External libraries from other projects such as shapelib and \ref ccmathlib) + - external: \ref external (External libraries from other projects such as shapelib, parson and \ref ccmathlib) - fonts: \ref fonts (GRASS fonts library) - init: \ref init (GRASS initialization code + scripts) - iostream: \ref iostream (fast I/O library) @@ -131,6 +131,22 @@ href="https://grass.osgeo.org">https://grass.osgeo.org - manage: \ref managelib - symbol: \ref symbol (Drawing symbols for %point %vector data library) +\subsection external Adding External Libraries + + The following files must be added or updated when including a new external library. + + - add: lib/external/newlib + - add: lib/external/newlib/*.c + - add: lib/external/newlib/*.h + - add: lib/external/newlib/LICENSE + - add: lib/external/newlib/Makefile + - update: lib/external/Makefile + - update: lib/external/README.license + - update: lib/README.md + - update: include/Make/Install.make + - update: include/Make/Grass.make + - update: Makefile + \section location File structure of GRASS Location A GRASS raster map consists of several files in several subdirectories in a mapset, diff --git a/gui/wxpython/core/utils.py b/gui/wxpython/core/utils.py index 2db97bc7ebf..e4eb3fb834f 100644 --- a/gui/wxpython/core/utils.py +++ b/gui/wxpython/core/utils.py @@ -1198,5 +1198,168 @@ def is_shell_running(): return True +def parse_mapcalc_cmd(command): + """Parse r.mapcalc/r3.mapcalc module command + + >>> parse_mapcalc_cmd(command="r.mapcalc map = 1") + "r.mapcalc expression='map = 1'" + + >>> parse_mapcalc_cmd(command="r.mapcalc map = 1") + "r.mapcalc expression='map = 1'" + + >>> parse_mapcalc_cmd(command="r.mapcalc map=1") + "r.mapcalc expression='map=1'" + + >>> parse_mapcalc_cmd(command="r.mapcalc map = a - b") + "r.mapcalc expression='map = a - b'" + + >>> parse_mapcalc_cmd(command="r.mapcalc expression=map = a - b") + "r.mapcalc expression='map = a - b'" + + >>> parse_mapcalc_cmd(command="r.mapcalc expression=map = a - b") + "r.mapcalc expression='map = a - b'" + + >>> cmd = "r.mapcalc expr='map = a - b' region=clip --overwrite" + >>> parse_mapcalc_cmd(command=cmd) + "r.mapcalc --overwrite expr='map = a - b' region=clip" + + >>> cmd = 'r.mapcalc expr="map = a - b" region=clip --overwrite' + >>> parse_mapcalc_cmd(command=cmd) + "r.mapcalc --overwrite expr='map = a - b' region=clip" + + >>> cmd = "r.mapcalc -s map = (a - b) / c region=clip --overwrite --verbose" + >>> parse_mapcalc_cmd(command=cmd) + "r.mapcalc -s --overwrite --verbose expression='map = (a - b) / c' region=clip" + + >>> cmd = 'r.mapcalc e="map = 1" region=clip --overwrite' + >>> parse_mapcalc_cmd(command=cmd) + "r.mapcalc --overwrite e='map = 1' region=clip" + + >>> cmd = 'r.mapcalc ex="map = 1" region=clip --overwrite' + >>> parse_mapcalc_cmd(command=cmd) + "r.mapcalc --overwrite ex='map = 1' region=clip" + + >>> cmd = 'r.mapcalc exp="map = 1" region=clip --overwrite' + >>> parse_mapcalc_cmd(command=cmd) + "r.mapcalc --overwrite exp='map = 1' region=clip" + + >>> cmd = 'r.mapcalc expr="map = 1" region=clip --overwrite' + >>> parse_mapcalc_cmd(command=cmd) + "r.mapcalc --overwrite expr='map = 1' region=clip" + + >>> cmd = 'r.mapcalc expre="map = 1" region=clip --overwrite' + >>> parse_mapcalc_cmd(command=cmd) + "r.mapcalc --overwrite expre='map = 1' region=clip" + + >>> cmd = 'r.mapcalc expres="map = 1" region=clip --overwrite' + >>> parse_mapcalc_cmd(command=cmd) + "r.mapcalc --overwrite expres='map = 1' region=clip" + + >>> cmd = 'r.mapcalc express="map = 1" region=clip --overwrite' + >>> parse_mapcalc_cmd(command=cmd) + "r.mapcalc --overwrite express='map = 1' region=clip" + + >>> cmd = 'r.mapcalc expressi="map = 1" region=clip --overwrite' + >>> parse_mapcalc_cmd(command=cmd) + "r.mapcalc --overwrite expressi='map = 1' region=clip" + + >>> cmd = 'r.mapcalc expressio="map = 1" region=clip --overwrite' + >>> parse_mapcalc_cmd(command=cmd) + "r.mapcalc --overwrite expressio='map = 1' region=clip" + + >>> cmd = 'r.mapcalc expression="map = 1" region=clip --overwrite' + >>> parse_mapcalc_cmd(command=cmd) + "r.mapcalc --overwrite expression='map = 1' region=clip" + + >>> cmd = 'r.mapcalc exp="map = a + e" region=clip --overwrite' + >>> parse_mapcalc_cmd(command=cmd) + "r.mapcalc --overwrite exp='map = a + e' region=clip" + + >>> cmd = 'r.mapcalc exp="map = a + exp(5)" region=clip --overwrite' + >>> parse_mapcalc_cmd(command=cmd) + "r.mapcalc --overwrite exp='map = a + exp(5)' region=clip" + + :param str command: r.mapcalc command string + + :return str: parsed r.mapcalc command string + """ + flags = [] + others_params_args = [] + expr_param_regex = re.compile(r"e.*?=") + flag_regex = re.compile( + r"^-[a-z]|^--overwrite|^--quiet|^--verbose|^--help|^--o|^--q|^--v|^--h", + ) + + command = split(command) + module = command.pop(0) + + for arg in command[:]: + flag = flag_regex.search(arg) + if flag: + flags.append(command.pop(command.index(flag.group()))) + elif "region=" in arg or "file=" in arg or "seed=" in arg: + others_params_args.append(command.pop(command.index(arg))) + + cmd = " ".join(command) + expr_param = expr_param_regex.search(cmd) + if not expr_param: + expr_param_name = "expression=" + else: + # Remove expression param + command = split(cmd.replace(expr_param.group(), "")) + expr_param_name = expr_param.group() + # Add quotes + if "'" not in cmd or '"' not in cmd: + cmd = f"'{' '.join(command)}'" + expression_param_arg = f"{expr_param_name}{cmd}" + + return " ".join( + [ + module, + *flags, + expression_param_arg, + *others_params_args, + ] + ) + + +def replace_module_cmd_special_flags(command): + """Replace module command special flags short version with + full version + + Flags: + + --o -> --overwrite + --q -> --quiet + --v -> --verbose + --h -> --help + + >>> cmd = "r.mapcalc -s --o --v expression='map = 1' region=clip" + >>> replace_module_cmd_special_flags(command=cmd) + "r.mapcalc -s --overwrite --verbose expression='map = 1' region=clip" + + >>> cmd = "r.mapcalc -s --o --q expression='map = 1' region=clip" + >>> replace_module_cmd_special_flags(command=cmd) + "r.mapcalc -s --overwrite --quiet expression='map = 1' region=clip" + + :param str command: module command string + + :return str: module command string with replaced flags + """ + flags_regex = re.compile( + r"(--o(\s+|$))|(--q(\s+|$))|(--v(\s+|$))|(--h(\s+|$))", + ) + replace = { + "--o": "--overwrite ", + "--q": "--quiet ", + "--v": "--verbose ", + "--h": "--help ", + } + return flags_regex.sub( + lambda flag: replace[flag.group().strip()], + command, + ) + + if __name__ == "__main__": sys.exit(doc_test()) diff --git a/gui/wxpython/gui_core/dialogs.py b/gui/wxpython/gui_core/dialogs.py index d3caa8422c6..387b7ca30c0 100644 --- a/gui/wxpython/gui_core/dialogs.py +++ b/gui/wxpython/gui_core/dialogs.py @@ -1933,6 +1933,7 @@ def __init__( style=wx.DEFAULT_DIALOG_STYLE, **kwargs, ): + img_size = kwargs.pop("img_size", None) self.parent = parent wx.Dialog.__init__(self, parent, id=id, style=style, title=title, **kwargs) @@ -1946,11 +1947,11 @@ def __init__( size = self.parent.GetWindow().GetClientSize() self.width = SpinCtrl(parent=self.panel, id=wx.ID_ANY, style=wx.SP_ARROW_KEYS) self.width.SetRange(20, 1e6) - self.width.SetValue(size.width) + self.width.SetValue(img_size[0] if img_size else size.width) wx.CallAfter(self.width.SetFocus) self.height = SpinCtrl(parent=self.panel, id=wx.ID_ANY, style=wx.SP_ARROW_KEYS) self.height.SetRange(20, 1e6) - self.height.SetValue(size.height) + self.height.SetValue(img_size[1] if img_size else size.height) self.template = wx.Choice( parent=self.panel, id=wx.ID_ANY, diff --git a/gui/wxpython/history/browser.py b/gui/wxpython/history/browser.py index c782767feb6..dd2f12b74a7 100644 --- a/gui/wxpython/history/browser.py +++ b/gui/wxpython/history/browser.py @@ -20,8 +20,14 @@ from core import globalvar from core.gcmd import GError, GException +from core.utils import ( + parse_mapcalc_cmd, + replace_module_cmd_special_flags, + split, +) from gui_core.forms import GUI from gui_core.treeview import CTreeView +from gui_core.wrap import SearchCtrl from history.tree import HistoryBrowserTree from grass.pydispatch.signal import Signal @@ -57,11 +63,23 @@ def __init__( lambda cmd: self.UpdateHistoryModelByCommand(cmd) ) + self.search = SearchCtrl(self) + self.search.SetDescriptiveText(_("Search")) + self.search.ShowCancelButton(True) + self.search.Bind(wx.EVT_TEXT, lambda evt: self.Filter(evt.GetString())) + self.search.Bind(wx.EVT_SEARCHCTRL_CANCEL_BTN, lambda evt: self.Filter("")) + self._layout() def _layout(self): """Dialog layout""" sizer = wx.BoxSizer(wx.VERTICAL) + sizer.Add( + self.search, + proportion=0, + flag=wx.ALL | wx.EXPAND, + border=5, + ) sizer.Add( self._tree, proportion=1, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=5 ) @@ -90,6 +108,18 @@ def _getSelectedNode(self): def _refreshTree(self): self._tree.SetModel(self._model.GetModel()) + def Filter(self, text): + """Filter history + + :param str text: text string + """ + model = self._model.GetModel() + if text: + model = self._model.model.Filtered(key=["command"], value=text) + self._tree.SetModel(model) + else: + self._tree.SetModel(model) + def UpdateHistoryModelFromScratch(self): """Update the model from scratch and refresh the tree""" self._model.CreateModel() @@ -110,15 +140,18 @@ def Run(self, node=None): node = node or self._getSelectedNode() if node: command = node.data["command"] - lst = re.split(r"\s+", command) if ( globalvar.ignoredCmdPattern and re.compile(globalvar.ignoredCmdPattern).search(command) and "--help" not in command and "--ui" not in command ): - self.runIgnoredCmdPattern.emit(cmd=lst) + self.runIgnoredCmdPattern.emit(cmd=split(command)) return + if re.compile(r"^r[3]?\.mapcalc").search(command): + command = parse_mapcalc_cmd(command) + command = replace_module_cmd_special_flags(command) + lst = split(command) try: GUI(parent=self, giface=self._giface).ParseCommand(lst) except GException as e: diff --git a/gui/wxpython/mapdisp/frame.py b/gui/wxpython/mapdisp/frame.py index 6359068aa3d..65a7f358b52 100644 --- a/gui/wxpython/mapdisp/frame.py +++ b/gui/wxpython/mapdisp/frame.py @@ -123,6 +123,9 @@ def __init__( # undock/dock bound method self._docking_callback = None + # Saved Map Display output img size + self._saved_output_img_size = None + # Emitted when switching map notebook tabs (Single-Window) self.onFocus = Signal("MapPanel.onFocus") @@ -757,12 +760,12 @@ def SaveToFile(self, event): return # get size - dlg = ImageSizeDialog(self) + dlg = ImageSizeDialog(self, img_size=self._saved_output_img_size) dlg.CentreOnParent() if dlg.ShowModal() != wx.ID_OK: dlg.Destroy() return - width, height = dlg.GetValues() + self._saved_output_img_size = dlg.GetValues() dlg.Destroy() # get filename @@ -787,7 +790,7 @@ def SaveToFile(self, event): if ext != extType: path = base + "." + extType - self.MapWindow.SaveToFile(path, fileType, width, height) + self.MapWindow.SaveToFile(path, fileType, *self._saved_output_img_size) dlg.Destroy() diff --git a/imagery/i.evapo.time/i.evapo.time.html b/imagery/i.evapo.time/i.evapo.time.html index b1e01dc6281..c0aa1bdac15 100644 --- a/imagery/i.evapo.time/i.evapo.time.html +++ b/imagery/i.evapo.time/i.evapo.time.html @@ -52,7 +52,7 @@

NOTES

Temporal integration from a weather station
-This is an example of a temporal integration from a weather station as done by +This is an example of a temporal integration from a weather station as done by Chemin and Alexandridis (2004)

@@ -60,7 +60,7 @@

References

Chemin and Alexandridis, 2004. Spatial Resolution Improvement of Seasonal -Evapotranspiration for Irrigated Rice, Zhanghe Irrigation District, Hubei Province, China. +Evapotranspiration for Irrigated Rice, Zhanghe Irrigation District, Hubei Province, China. Asian Journal of Geoinformatics, Vol. 5, No. 1, September 2004 (PDF) diff --git a/include/Make/Grass.make b/include/Make/Grass.make index 09657591479..137a45d1955 100644 --- a/include/Make/Grass.make +++ b/include/Make/Grass.make @@ -152,6 +152,7 @@ libs = \ NVIZ:nviz \ OGSF:ogsf \ OPTRI:optri \ + PARSON:parson \ PNGDRIVER:pngdriver \ PSDRIVER:psdriver \ QTREE:qtree \ diff --git a/include/Make/Install.make b/include/Make/Install.make index cef4f810ffe..b9a5e45e0fc 100644 --- a/include/Make/Install.make +++ b/include/Make/Install.make @@ -212,6 +212,7 @@ srclibsdist: distclean -cp -rL utils ./grass-lib-$(GRASS_VERSION_NUMBER) -cp -rL demolocation ./grass-lib-$(GRASS_VERSION_NUMBER) -cp -rL include ./grass-lib-$(GRASS_VERSION_NUMBER) + -cp -rL --parents lib/external/parson ./grass-lib-$(GRASS_VERSION_NUMBER) -cp -rL --parents lib/external/shapelib ./grass-lib-$(GRASS_VERSION_NUMBER) -cp -rL --parents lib/external/bwidget ./grass-lib-$(GRASS_VERSION_NUMBER) -cp -rL --parents lib/datetime ./grass-lib-$(GRASS_VERSION_NUMBER) diff --git a/lib/README b/lib/README index e5f72ec1f74..f694dd267ab 100644 --- a/lib/README +++ b/lib/README @@ -11,6 +11,7 @@ Current directory layout - display: library for CELL driver - edit: curses library - external: external libraries + - external/parson: JSON serialization and deserialization functions - external/shapelib: SHAPE file management functions - fonts: fonts - fonts/fonts diff --git a/lib/cdhc/doc/chd_statlib.md b/lib/cdhc/doc/chd_statlib.md new file mode 100644 index 00000000000..76c0f69c8db --- /dev/null +++ b/lib/cdhc/doc/chd_statlib.md @@ -0,0 +1,1979 @@ +# CDH statlib Source + +Released in 1994 by Paul Johnson. + +Retrieved from the Internet Archive for +[http://sunsite.univie.ac.at/statlib/general/cdh](https://web.archive.org/web/20001006232323/http://sunsite.univie.ac.at/statlib/general/cdh) +-- 2000-10-06 + + +```txt + TESTS OF COMPOSITE DISTRIBUTIONAL HYPOTHESES FOR + THE ANALYSIS OF BIOLOGICAL & ENVIRONMENTAL DATA + +This library contains FORTRAN subroutines for testing the hypothesis +of normality or the hypothesis of exponentiality. Lognormality can be +tested by carrying out the tests of normality on the log transformed +data. Some tests are general goodness-of-fit tests that allow any +distribution to be tested simply by basing the distributional +components of the test statistic on the hypothesized distribution. The +set of subroutines consist of 24 separate tests. All tests or a subset +of tests can be called from the main program. The following step may be +used to access the subroutines. Create a FORTRAN main program +including one or more call statements,such as: + + CALL TEST#(X,Y,N) + + where # is the test number (see Table) + X is the input vector of length N = n sample points + Y is the output vector - test statistic(s), df's + N is an integer value,number of sample points. + + + TEST TEST + # TEST NAME OUTPUT + 1 Omnibus Moments Test for Normality + 2 Geary's Test of Normality + 3 Studentized Range for Testing Normality + 4 D'Agostino's D-Statistic Test of Normality + 5 Kuiper V-Statistic Modified to Test Normality + 6 Watson U^2-Statistic Modified to Test Normality + 7 Durbin's Exact Test (Normal Distribution + 8 Anderson-Darling Statistic Modified to Test Normality + 9 Cramer-Von Mises W^2-Statistic to Test Normality * + 10 Kolmogorov-Smirnov D-Statistic to Test Normality * + 11 Kolmogorov-Smirnov D-Statistic ( Lilliefors Critical Values) + 12 Chi-Square Test of Normality (Equal Probability Classes) + 13 Shapiro-Wilk W Test of Normality for Small Samples + 14 Shapiro-Francia W' Test of Normality for Large Samples + 15 Shapiro-Wilk W Test of Exponentiality + 16 Cramer-Von Mises W^2-Statistic to Test Exponentiality * + 17 Kolmogorov-Smirnov D-Statistic to Test Exponentiality * + 18 Kuiper V-Statistic Modified to Test Exponentiality + 19 Watson U^2-Statistic Modified to Test Exponentiality + 20 Anderson-Darling Statistic Modified to Test Exponentiality + 21 Chi-Square Test for Exponentiality(with E.P.C.) + 22 Modified Maximum Likelihood Ratio Test ** + 23 Coefficient of Variation Test + 24 Kotz Separate-Families Test for Lognormality vs. Normality + +* indicates the test statistic is modified +** indicates the test is for normality vs. lognormality +E.P.C. --- Equal Probability Classes + + These subroutines can be freely distributed and can be freely used for + non-commercial purposes. +``` +```fortran + +C +C FORTRAN SUBROUTINES TO TEST FOR NORMALITY,LOGNORMALITY,EXPONENTIALITY AND +C GENERAL GOODNESS-OF-FIT TESTS THAT ALLOW ANY DISTRIBUTION TO BE TESTED +C**************************************************************************** +C +C TESTS OF COMPOSITE DISTRIBUTIONAL HYPOTHESES +C -------------------------------------------- +C +C INPUT: X [The vector of observed values] +C N [The number of input observations] +C +C OUTPUT: Y [The Composite Distributional Test Statistics] +C +C SUBROUTINES: +C ------------ +C +C ID TEST NAME +C +C TEST1 Omnibus Moments Test for Normality +C TEST2 Geary's Test of Normality +C TEST3 Studentized Range for Testing Normality +C TEST4 D'Agostino's D-Statistic Test of Normality +C TEST5 Kuiper V-Statistic Modified to Test Normality +C TEST6 Watson U^2-Statistic Modified to Test Normality +C TEST7 Durbin's Exact Test (Normal Distribution,Simple Hypothesis) +C TEST8 Anderson-Darling Statistic Modified to Test Normality +C TEST9 Cramer-Von Mises W^2-Statistic Modified to Test Normality +C TEST10 Kolmogorov-Smirnov D-Statistic Modified to Test Normality +C TEST11 Kolmogorov-Smirnov D-Statistic with Lilliefors Critical Values +C TEST12 Chi-Square Test of Normality (with Equal Probability Classes) +C TEST13 Shapiro-Wilk W Test of Normality for Small Samples +C TEST14 Shapiro-Francia W' Test of Normality for Large Samples +C TEST15 Shapiro-Wilk W Test of Exponentiality +C TEST16 Cramer-Von Mises W^2-Statistic Modified to Test Exponentiality +C TEST17 Kolmogorov-Smirnov D-Statistic Modified to Test Exponentiality +C TEST18 Kuiper V-Statistic Modified to Test Exponentiality +C TEST19 Watson U^2-Statistic Modified to Test Exponentiality +C TEST20 Anderson-Darling Statistic Modified to Test Exponentiality +C TEST21 Chi-Square Test of Exponentiality (with Equal Probability Classes) +C TEST22 Modified Maximum Likelihood Ratio Test for Normality vs. Lognormality +C TEST23 Coefficient of Variation Test +C TEST24 Kotz Separate-Families Test for Lognormality vs. Normality +C +C +C USAGE: CALL TEST#(X,Y,N) with # = 1,2,3...........,24 +C +C EXAMPLE: CALL TEST20(X,Y,10) for an input vector X consisting of +C 10 observations results in the output vector Y where +C Y(1) = AD(E).[The Shapiro-Wilk W Test of Exponentiality]. +C +C REFERENCES: +C +C Anderson ,T.W. and D.A. Darling.1954.A Test of Goodness of Fit. +C JASA 49:765-69. +C D'Agostino,R.B. and E.S. Pearson.1973.Tests for Departure from Normality. +C Biometrika 60(3):613-22. +C D'Agostino,R.B. and B. Rosman.1974.The Power of Geary's Test of +C Normality.Biometrika 61(1):181-84. +C Durbin,J.1961.Some Methods of Constructing Exact Tests. +C Biometrika 48(1&2):41-55. +C Durbin,J.1973.Distribution Theory Based on the Sample Distribution +C Function.SIAM.Philadelphia. +C Geary,R.C.1947.Testing for Normality.Biometrika 36:68-97. +C Kotz,S. 1973. Normality vs. Lognormality with Applications. +C Communications in Statistics 1(2):113-32. +C Lehmann,E.L.1986.Testing Statistical Hypotheses.John Wiley & Sons. +C New York. +C Linnet,K.1988.Testing Normality of Transformed Data. +C Applied Statistics 32(2):180-186. +C SAS [Statistical Analysis System] User's Guide:Basics.Version 5.1985. +C SAS User's Guide:Statistics.Version 6.Volumes 1 and 2.1993. +C Shapiro,S.S. and R.S.Francia.1972.An Approximate Analysis of Variance +C Test for Normality.JASA 67(337):215-216. +C Shapiro,S.S.,M.B.Wilk and H.J.Chen.1968.A Comparative Study of Various +C Tests for Normality.JASA 63:1343-72. +C Weiss,M.S. 1978.Modification of the Kolmogorov-Smirnov Statistic for Use +C with Correlated Data.JASA 73(364):872-75. +C +C +C**************************************************************************** +C +C Note if the sample size N >= 150 then increase the DIMENSION statement +C for the TESTS of interest in each subroutine. +C The subroutines can be run as a group or individually e.g., as a group: +C suppose TESTS 4,12 and 21 are required and the input vector consists of +C 20 observations; then in the MAIN program write: +C +C CALL TEST4(X,Y,20) +C CALL TEST12(X,Y,20) +C CALL TEST21(X,Y,20) +C +C**************************************************************************** +C +C +C**************************************************************************** +C +C Subroutines By: Paul Johnson +C 1420 Lake Blvd #29 +C Davis,California 95616 +C EZ006244@ALCOR.UCDAVIS.EDU +C +C These subroutines can be freely used for non-commercial purposes and can +C be freely distributed. +C +C Copyright 1994, Paul Johnson +C +C**************************************************************************** +C +C + SUBROUTINE TEST1(X,Y,N) + DIMENSION X(150),Y(2),Z(150) + REAL MEAN + SUM1=0 + SUM2=0 + SUM3=0 + SUM4=0 + SUM5=0 + DO 10 I = 1,N + 10 SUM1=SUM1+X(I) + MEAN=SUM1/N + DO 20 I = 1,N + 20 SUM2=SUM2+(X(I)-MEAN)**3 + DO 30 I = 1,N + 30 Z(I)=(X(I)-MEAN)**2 + DO 40 I = 1,N + 40 SUM3=SUM3+Z(I) + SUM5=SUM3 + SUM3=SUM3**1.5 + TSSM=(N**0.5)*SUM2/SUM3 + DO 50 I = 1,N + 50 SUM4=SUM4+(X(I)-MEAN)**4 + FSSM=(N*SUM4)/(SUM5*SUM5) + Y(1) = TSSM + Y(2) = FSSM + WRITE(6,300) + 300 FORMAT(1X,' ') + WRITE(6,301) + 301 FORMAT(10X,'TESTS OF COMPOSITE DISTRIBUTIONAL HYPOTHESES') + WRITE(6,302) + 302 FORMAT(1X,' ') + + WRITE(6,100) Y(1),Y(2) + 100 FORMAT(/,2X,'TEST1 TSM =',F10.4,' FSM =',F10.4) + RETURN + END + SUBROUTINE TEST2(X,Y,N) + DIMENSION X(150),Y(2),Z(150) + REAL MEAN + SUM1=0 + SUM2=0 + SUM3=0 + DO 10 I = 1,N + 10 SUM1=SUM1+X(I) + MEAN=SUM1/N + DO 20 I = 1,N + 20 Z(I)=ABS(X(I)-MEAN) + DO 30 I = 1,N + 30 SUM2=SUM2+Z(I) + DO 40 I = 1,N + 40 SUM3=SUM3+(X(I)-MEAN)**2 + S=N*SUM3 + S1=SQRT(S) + Y(1)=SUM2/S1 + Y(2)=(Y(1)-0.7979)*SQRT(N*1.0)/0.2123 + WRITE(6,100) Y(1),Y(2) + 100 FORMAT(/,2X,'TEST2 GTN =',F10.4,' Z(GTN) =',F10.4) + RETURN + END + SUBROUTINE TEST3(X,Y,N) + DIMENSION X(150),Y(2),X1(150) + DO 10 I=1,N + 10 X1(I)=X(I) + CALL SORT(N,X) + XS=X(1) + XM=X(N) + DO 15 I = 1,N + 15 IF (XS .GE. X(I)) XS=X(I) + DO 20 I = 1,N + 20 IF (XM .LE. X(I)) XM=X(I) + SUM1=0 + SUM2=0 + DO 30 I = 1,N + SUM1=SUM1+X(I) + SUM2=SUM2+(X(I)*X(I)) + 30 CONTINUE + XBAR=SUM1/N + S1 =SUM2-((SUM1*SUM1)/N) + S2=S1/(N-1) + S3=SQRT(S2) + Y(1)=(XM-XS)/S3 + DO 40 I=1,N + 40 X(I)=X1(I) + WRITE(6,100) Y(1) + 100 FORMAT(/,2X,'TEST3 U =',F10.4) + RETURN + END + SUBROUTINE TEST4(A,B,K) + DIMENSION A(150),B(2),A1(150) + REAL M2 + REAL MN + S1=0 + T=0 + MN=0 + E=1 + DO 10 I=1,K + 10 A1(I) = A(I) + CALL SORT(K,A1) + DO 20 I =1,K + T = T + (I - .5*(K+1))*A1(I) + 20 CONTINUE + DO 30 I = 1,K + MN=MN+A1(I) + 30 CONTINUE + M2=MN/K + DO 40 I=1,K + S1=S1+(A1(I)-M2)**2 + 40 CONTINUE + S2=S1/K + S =SQRT(S2) + D = T/(K**2*S) + B(1)=(D-1.0/(2*SQRT(3.141592654)))*SQRT(K*1.0)/0.02998598 + WRITE(6,100) B(1) + 100 FORMAT(/,2X,'TEST4 DAGN =',F10.4) + RETURN + END + SUBROUTINE TEST5(X,Y,N) + DIMENSION X(150),Y(2),FX(150),FN1(150),FN2(150) + DIMENSION T(150),Z(150),D(2),FN3(150),X1(150) + REAL MEAN + SUM1=0 + SUM2=0 + SUM3=0 + SUM4=0 + SUM5=0 + DO 10 I = 1,N + 10 X1(I)=X(I) + DO 15 I =1,N + 15 SUM1=SUM1+X(I) + MEAN=SUM1/N + XBAR=MEAN + DO 20 I = 1,N + 20 SUM2=SUM2+(X(I)*X(I)) + S1=((N*SUM2)-(SUM1*SUM1))/(N*(N-1)) + SDX=SQRT(S1) + R=FLOAT(N) + CALL SORT(N,X) + DO 30 I=1,N + X(I)=(X(I)-XBAR)/SDX + FN1(I)=FLOAT(I)/R + FN2(I)=FLOAT((2*I)-1)/FLOAT(2*N) + FN3(I)=FLOAT(I)/R + FX(I)=.5+(ENORMP(X(I)/SQRT(2.0))/2.0) + IF (FX(I) .LE. 0.0) FX(I) = 0.00001 + 30 IF (FX(I) .GE. 1.0) FX(I) = 0.99999 + DO 40 J=1,N + 40 Z(J) = (FN3(J) - FX(J)) + CALL SORT(N,Z) + D1=Z(N) + DO 50 J=1,N + 50 T(J) = FX(J)-((J-1)/R) + CALL SORT(N,T) + D2=T(N) + D(1)=D1 + D(2)=D2 + CALL SORT(2,D) + V=D(1)+D(2) + V=V*(SQRT(R)+(.82/SQRT(R))+0.05) + Y(1)=V + DO 60 I=1,N + 60 X(I)=X1(I) + WRITE(6,100) Y(1) + 100 FORMAT(/,2X,'TEST5 KV(N) =',F10.4) + RETURN + END + SUBROUTINE TEST6(X,Y,N) + DIMENSION X(150),Y(2),FX(150),FN1(150),FN2(150) + DIMENSION X1(150) + REAL MEAN + SUM1=0 + SUM2=0 + SUM3=0 + SUM4=0 + SUM5=0 + DO 10 I = 1,N + 10 X1(I)=X(I) + DO 15 I =1,N + 15 SUM1=SUM1+X(I) + MEAN=SUM1/N + XBAR=MEAN + DO 20 I = 1,N + 20 SUM2=SUM2+(X(I)*X(I)) + S1=((N*SUM2)-(SUM1*SUM1))/(N*(N-1)) + SDX=SQRT(S1) + R=FLOAT(N) + CALL SORT(N,X) + DO 30 I=1,N + X(I)=(X(I)-XBAR)/SDX + FN1(I)=FLOAT(I)/R + FN2(I)=FLOAT((2*I)-1)/FLOAT(2*N) + FX(I)=.5+(ENORMP(X(I)/SQRT(2.0))/2.0) + IF (FX(I) .LE. 0.0) FX(I) = 0.00001 + 30 IF (FX(I) .GE. 1.0) FX(I) = 0.99999 + DO 40 I=1,N + A=((2*I)-1)*ALOG(FX(I)) + B=((2*I)-1)*ALOG(1.0-FX(N+1-I)) + SUM3=SUM3+A+B + SUM4=SUM4+((-FN2(I)+FX(I))**2) + FN1(I)=ABS(FN1(I)-FX(I)) + 40 CONTINUE + CVM=1./FLOAT(12*N)+SUM4 + DO 50 I=1,N + SUM5=SUM5+FX(I) + 50 CONTINUE + ZBAR=SUM5/R + W=CVM-R*(ZBAR-0.5)*(ZBAR-0.5) + W=W*(1.0+0.5/R) + Y(1)=W + DO 60 I=1,N + 60 X(I)=X1(I) + WRITE(6,100) Y(1) + 100 FORMAT(/,2X,'TEST6 WU2(N) =',F10.4) + RETURN + END + SUBROUTINE TEST7(X,Y,N) + DIMENSION X(150),C(150),X1(150),G(150),Z(150),B(150),Y(2) + R=FLOAT(N) + SUMX=0 + SUMX2=0 + DO 10 I=1,N + SUMX=SUMX+X(I) + SUMX2=SUMX2+X(I)**2 + 10 X1(I)=X(I) + S2=(SUMX2-SUMX**2/N)/(N-1) + DO 15 I=1,N + X(I)=(X(I)-SUMX/N)/SQRT(S2) + B(I)=0.5+(ENORMP(X(I)/SQRT(2.0))/2.0) + 15 CONTINUE + CALL SORT(N,B) + DO 20 I=2,N + C(I)=B(I)-B(I-1) + 20 CONTINUE + C(1)=B(1) + C(N+1)=1-B(N) + CALL SORT(N+1,C) + DO 30 J=2,N + G(J)=(N+2-J)*(C(J)-C(J-1)) + 30 CONTINUE + G(1)=(N+1)*C(1) + G(N+1)=C(N+1)-C(N) + DO 60 I=1,N + SUM1=0 + DO 50 J=1,I + SUM1=SUM1+G(J) + 50 CONTINUE + Z(I)=(I/R)-SUM1 + 60 CONTINUE + CALL SORT(N,Z) + R=Z(N) + Y(1)=R + DO 70 I=1,N + 70 X(I)=X1(I) + WRITE(6,100) Y(1) + 100 FORMAT(/,2X,'TEST7 DRB(N) =',F10.4) + RETURN + END + SUBROUTINE TEST8(X,Y,N) + DIMENSION X(150),Y(2),FX(150),FN1(150),FN2(150) + DIMENSION X1(150) + EXTERNAL enormp + REAL MEAN + SUM1=0 + SUM2=0 + SUM3=0 + SUM4=0 + SUM5=0 + DO 10 I = 1,N + 10 X1(I)=X(I) + DO 15 I =1,N + 15 SUM1=SUM1+X(I) + MEAN=SUM1/N + XBAR=MEAN + DO 20 I = 1,N + 20 SUM2=SUM2+(X(I)*X(I)) + S1=((N*SUM2)-(SUM1*SUM1))/(N*(N-1)) + SDX=SQRT(S1) + R=FLOAT(N) + CALL SORT(N,X) + DO 30 I=1,N + X(I)=(X(I)-XBAR)/SDX + FN1(I)=FLOAT(I)/R + FN2(I)=FLOAT((2*I)-1)/FLOAT(2*N) + FX(I)=.5+(enormp(X(I)/SQRT(2.0))/2.0) + IF (FX(I) .LE. 0.0) FX(I) = 0.00001 + 30 IF (FX(I) .GE. 1.0) FX(I) = 0.99999 + DO 40 I=1,N + A=((2*I)-1)*ALOG(FX(I)) + B=((2*I)-1)*ALOG(1.0-FX(N+1-I)) + SUM3=SUM3+A+B + SUM4=SUM4+((-FN2(I)+FX(I))**2) + FN1(I)=ABS(FN1(I)-FX(I)) + 40 CONTINUE + ADB=SUM3/R + ADSTAT=-R-ADB + ADMOD=ADSTAT*(1.0+(.75/R)+(2.25/FLOAT(N**2))) + Y(1)=ADMOD + DO 50 I=1,N + 50 X(I)=X1(I) + WRITE(6,100) Y(1) + 100 FORMAT(/,2X,'TEST8 AD(N) =',F10.4) + RETURN + END + SUBROUTINE TEST9(X,Y,N) + DIMENSION X(150),Y(2),FX(150),FN1(150),FN2(150) + DIMENSION X1(150) + REAL MEAN + SUM1=0 + SUM2=0 + SUM3=0 + SUM4=0 + SUM5=0 + DO 10 I = 1,N + 10 X1(I)=X(I) + DO 15 I =1,N + 15 SUM1=SUM1+X(I) + MEAN=SUM1/N + XBAR=MEAN + DO 20 I = 1,N + 20 SUM2=SUM2+(X(I)*X(I)) + S1=((N*SUM2)-(SUM1*SUM1))/(N*(N-1)) + SDX=SQRT(S1) + R=FLOAT(N) + CALL SORT(N,X) + DO 30 I=1,N + X(I)=(X(I)-XBAR)/SDX + FN1(I)=FLOAT(I)/R + FN2(I)=FLOAT((2*I)-1)/FLOAT(2*N) + FX(I)=.5+(ENORMP(X(I)/SQRT(2.0))/2.0) + IF (FX(I) .LE. 0.0) FX(I) = 0.00001 + 30 IF (FX(I) .GE. 1.0) FX(I) = 0.99999 + DO 40 I=1,N + A=((2*I)-1)*ALOG(FX(I)) + B=((2*I)-1)*ALOG(1.0-FX(N+1-I)) + SUM3=SUM3+A+B + SUM4=SUM4+((-FN2(I)+FX(I))**2) + FN1(I)=ABS(FN1(I)-FX(I)) + 40 CONTINUE + CVM=1./FLOAT(12*N)+SUM4 + CVMOD=CVM*(1.+(.5/R)) + Y(1)=CVMOD + DO 50 I=1,N + 50 X(I)=X1(I) + WRITE(6,100) Y(1) + 100 FORMAT(/,2X,'TEST9 CVM(N) =',F10.4) + RETURN + END + SUBROUTINE TEST10(X,Y,N) + DIMENSION X(150),Y(2),FX(150),FN1(150),FN2(150) + DIMENSION T(150),Z(150),D(2),FN3(150),X1(150) + REAL MEAN + REAL KS + SUM1=0 + SUM2=0 + SUM3=0 + SUM4=0 + SUM5=0 + DO 10 I = 1,N + 10 X1(I)=X(I) + DO 15 I =1,N + 15 SUM1=SUM1+X(I) + MEAN=SUM1/N + XBAR=MEAN + DO 20 I = 1,N + 20 SUM2=SUM2+(X(I)*X(I)) + S1=((N*SUM2)-(SUM1*SUM1))/(N*(N-1)) + SDX=SQRT(S1) + R=FLOAT(N) + CALL SORT(N,X) + DO 30 I=1,N + X(I)=(X(I)-XBAR)/SDX + FN1(I)=FLOAT(I)/R + FN2(I)=FLOAT((2*I)-1)/FLOAT(2*N) + FN3(I)=FLOAT(I)/R + FX(I)=.5+(ENORMP(X(I)/SQRT(2.0))/2.0) + IF (FX(I) .LE. 0.0) FX(I) = 0.00001 + 30 IF (FX(I) .GE. 1.0) FX(I) = 0.99999 + DO 40 J=1,N + 40 Z(J)=(FN3(J)-FX(J)) + CALL SORT(N,Z) + D1=Z(N) + DO 50 J=1,N + 50 T(J)=FX(J)-((J-1)/R) + CALL SORT(N,T) + D2=T(N) + D(1)=D1 + D(2)=D2 + CALL SORT(2,D) + DMAX=D(2) + KS=DMAX*(SQRT(R)+(.85/SQRT(R))-.01) + Y(1)=KS + DO 60 I=1,N + 60 X(I)=X1(I) + WRITE(6,100) Y(1) + 100 FORMAT(/,2X,'TEST10 KSD(N) =',F10.4) + RETURN + END + SUBROUTINE TEST11(X,Y,N) + DIMENSION X(150),Y(2),FX(150),FN1(150),FN2(150) + DIMENSION T(150),Z(150),D(2),FN3(150),X1(150) + REAL MEAN + SUM1=0 + SUM2=0 + DO 10 I = 1,N + 10 X1(I)=X(I) + DO 15 I =1,N + 15 SUM1=SUM1+X(I) + MEAN=SUM1/N + XBAR=MEAN + DO 20 I = 1,N + 20 SUM2=SUM2+(X(I)*X(I)) + S1=((N*SUM2)-(SUM1*SUM1))/(N*(N-1)) + SDX=SQRT(S1) + R=FLOAT(N) + CALL SORT(N,X) + DO 30 I=1,N + X(I)=(X(I)-XBAR)/SDX + FN1(I)=FLOAT(I)/R + FN2(I)=FLOAT((2*I)-1)/FLOAT(2*N) + FN3(I)=FLOAT(I)/R + FX(I)=.5+(ENORMP(X(I)/SQRT(2.0))/2.0) + IF (FX(I) .LE. 0.0) FX(I) = 0.00001 + 30 IF (FX(I) .GE. 1.0) FX(I) = 0.99999 + DO 40 J=1,N + 40 Z(J)=(FN3(J)-FX(J)) + CALL SORT(N,Z) + D1=Z(N) + DO 50 J=1,N + 50 T(J)=FX(J)-((J-1)/R) + CALL SORT(N,T) + D2=T(N) + D(1)=D1 + D(2)=D2 + CALL SORT(2,D) + Y(1)=D(2) + DO 60 I=1,N + 60 X(I)=X1(I) + WRITE(6,100) Y(1) + 100 FORMAT(/,2X,'TEST11 KSD =',F10.4) + RETURN + END + SUBROUTINE TEST12(X,Y,N) + DIMENSION X(150),Y(2),V(150),V2(150),F(150) + DIMENSION P(150),Z(150) + EXTERNAL xinormal + EXTERNAL dl + REAL MEAN + SUM1=0 + SUM2=0 + SUM3=0 + A=4*((0.75*(N-1)*(N-1))**0.2) + K1=A + C=A-K1 + IF(C .GT. 0.5) K1=K1+1 + 10 R=N/K1 + IF(R .LT. 5) K1=K1-1 + IF(R .LT.5) GOTO 10 + K2=K1-1 + DO 15 I=1,K1 + 15 F(I)=0 + DO 20 I=1,N + 20 SUM1=SUM1+X(I) + MEAN=SUM1/N + DO 25 I = 1,N + 25 SUM2=SUM2+(X(I)-MEAN)*(X(I)-MEAN) + S1=SQRT(SUM2/(N-1)) + DO 30 I=1,K2 + 30 P(I)=FLOAT(I)/K1 + DO 40 I=1,K2 + 40 Z(I)=XINORMAL(P(I)) + DO 50 I=1,K2 + 50 V(I)=MEAN+(Z(I)*S1) + DO 51 I=1,K2 + 51 V2(I+1)=V(I)+0.0001 + DO 55 I=1,N + DO 45 J=2,K2 + IF(X(I) .GE. V2(J) .AND. X(I) .LE. V(J)) F(J)=1+F(J) + 45 CONTINUE + IF(X(I) .GE. V2(K2+1)) F(K2+1)=F(K2+1)+1 + IF(X(I) .LE. V(1)) F(1)=F(1)+1 + 55 CONTINUE + DO 65 I=1,K1 + 65 SUM3=SUM3+F(I)*F(I) + Y(1)=SUM3*K1/N-N + Y(2)=FLOAT(K1)-3 + WRITE(6,100) Y(1),Y(2) + 100 FORMAT(/,2X,'TEST12 CS(N) =',F10.4,' DOF =',F10.4) + RETURN + END + SUBROUTINE TEST13(X,Y,N) + DIMENSION X(150),X1(150),Y(2),A(25) + SUMB=0 + SUMX=0 + SUMX2=0 + K=N/2 + IF (N .GT. 50) GO TO 115 + DO 15 I =1,N + SUMX=SUMX+X(I) + SUMX2=SUMX2+X(I)**2 + A(I) = 0.0 + 15 X1(I)=X(I) + S2=SUMX2-SUMX**2/N + CALL SORT(N,X) + IF (N .GE. 20) GO TO 53 + IF (N .GE. 10) GO TO 33 + IF (N .NE. 3) GO TO 21 + A(1)=0.7071 + GO TO 115 + 21 IF (N .NE. 4) GO TO 23 + A(1)=0.6872 + A(2)=0.1677 + GO TO 115 + 23 IF (N .NE. 5) GO TO 25 + A(1)=0.6646 + A(2)=0.2413 + GO TO 115 + 25 IF (N .NE. 6) GO TO 27 + A(1)=0.6431 + A(2)=0.2806 + A(3)=0.0875 + GO TO 115 + 27 IF (N .NE. 7) GO TO 29 + A(1)=0.6233 + A(2)=0.3031 + A(3)=0.1401 + GO TO 115 + 29 IF (N .NE. 8) GO TO 31 + A(1)=0.6052 + A(2)=0.3164 + A(3)=0.1743 + A(4)=0.0561 + GO TO 115 + 31 IF (N .NE. 9) GO TO 33 + A(1)=0.5888 + A(2)=0.3244 + A(3)=0.1976 + A(4)=0.0947 + GO TO 115 + 33 IF (N .NE. 10) GO TO 35 + A(1)=0.5739 + A(2)=0.3291 + A(3)=0.2141 + A(4)=0.1224 + A(5)=0.0399 + GO TO 115 + 35 IF (N .NE. 11) GO TO 37 + A(1)=0.5601 + A(2)=0.3315 + A(3)=0.2260 + A(4)=0.1429 + A(5)=0.0695 + GO TO 115 + 37 IF (N .NE. 12) GO TO 39 + A(1)=0.5475 + A(2)=0.3325 + A(3)=0.2347 + A(4)=0.1586 + A(5)=0.0922 + A(6)=0.0303 + GO TO 115 + 39 IF (N .NE. 13) GO TO 41 + A(1)=0.5359 + A(2)=0.3325 + A(3)=0.2412 + A(4)=0.1707 + A(5)=0.1099 + A(6)=0.0539 + GO TO 115 + 41 IF (N .NE. 14) GO TO 43 + A(1)=0.5251 + A(2)=0.3318 + A(3)=0.2460 + A(4)=0.1802 + A(5)=0.1240 + A(6)=0.0727 + A(7)=0.0240 + GO TO 115 + 43 IF (N .NE. 15) GO TO 45 + A(1)=0.5150 + A(2)=0.3306 + A(3)=0.2495 + A(4)=0.1878 + A(5)=0.1353 + A(6)=0.0880 + A(7)=0.0433 + GO TO 115 + 45 IF (N .NE. 16) GO TO 47 + A(1)=0.5056 + A(2)=0.3290 + A(3)=0.2521 + A(4)=0.1939 + A(5)=0.1447 + A(6)=0.1005 + A(7)=0.0593 + A(8)=0.0196 + GO TO 115 + 47 IF (N .NE. 17) GO TO 49 + A(1)=0.4968 + A(2)=0.3273 + A(3)=0.2540 + A(4)=0.1988 + A(5)=0.1524 + A(6)=0.1109 + A(7)=0.0725 + A(8)=0.0359 + GO TO 115 + 49 IF (N .NE. 18) GO TO 51 + A(1)=0.4886 + A(2)=0.3253 + A(3)=0.2553 + A(4)=0.2027 + A(5)=0.1587 + A(6)=0.1197 + A(7)=0.0837 + A(8)=0.0496 + A(9)=0.0163 + GO TO 115 + 51 IF (N .NE. 19) GO TO 53 + A(1) = 0.4808 + A(2) = 0.3232 + A(3) = 0.2561 + A(4) = 0.2059 + A(5) = 0.1641 + A(6) = 0.1271 + A(7) = 0.0932 + A(8) = 0.0612 + A(9) = 0.0303 + GO TO 115 + 53 IF (N .NE. 20) GO TO 55 + A(1) = 0.4734 + A(2) = 0.3211 + A(3) = 0.2565 + A(4) = 0.2085 + A(5) = 0.1686 + A(6) = 0.1334 + A(7) = 0.1013 + A(8) = 0.0711 + A(9) = 0.0422 + A(10) = 0.0140 + GO TO 115 + 55 IF (N .NE. 21) GO TO 57 + A(1) = 0.4643 + A(2) = 0.3185 + A(3) = 0.2578 + A(4) = 0.2119 + A(5) = 0.1736 + A(6) = 0.1399 + A(7) = 0.1092 + A(8) = 0.0804 + A(9) = 0.0530 + A(10) = 0.0263 + GO TO 115 + 57 IF (N .NE. 22) GO TO 59 + A(1) = 0.4590 + A(2) = 0.3156 + A(3) = 0.2571 + A(4) = 0.2131 + A(5) = 0.1764 + A(6) = 0.1443 + A(7) = 0.1150 + A(8) = 0.0878 + A(9) = 0.0618 + A(10) = 0.0368 + A(11) = 0.0122 + GO TO 115 + 59 IF (N .NE. 23) GO TO 61 + A(1) = 0.4542 + A(2) = 0.3126 + A(3) = 0.2563 + A(4) = 0.2139 + A(5) = 0.1787 + A(6) = 0.1480 + A(7) = 0.1201 + A(8) = 0.0941 + A(9) = 0.0696 + A(10) = 0.0459 + A(11) = 0.0228 + GO TO 115 + 61 IF (N .NE. 24) GO TO 63 + A(1) = 0.4493 + A(2) = 0.3098 + A(3) = 0.2554 + A(4) = 0.2145 + A(5) = 0.1807 + A(6) = 0.1512 + A(7) = 0.1245 + A(8) = 0.0997 + A(9) = 0.0764 + A(10) = 0.0539 + A(11) = 0.0321 + A(12) = 0.0107 + GO TO 115 + 63 IF (N .NE. 25) GO TO 65 + A(1) = 0.4450 + A(2) = 0.3069 + A(3) = 0.2543 + A(4) = 0.2148 + A(5) = 0.1822 + A(6) = 0.1539 + A(7) = 0.1283 + A(8) = 0.1046 + A(9) = 0.0823 + A(10) = 0.0610 + A(11) = 0.0403 + A(12) = 0.0200 + GO TO 115 + 65 IF (N .NE. 26) GO TO 67 + A(1) = 0.4407 + A(2) = 0.3043 + A(3) = 0.2533 + A(4) = 0.2151 + A(5) = 0.1836 + A(6) = 0.1563 + A(7) = 0.1316 + A(8) = 0.1089 + A(9) = 0.0876 + A(10) = 0.0672 + A(11) = 0.0476 + A(12) = 0.0284 + A(13) = 0.0094 + GO TO 115 + 67 IF (N .NE. 27) GO TO 69 + A(1) = 0.4366 + A(2) = 0.3018 + A(3) = 0.2522 + A(4) = 0.2152 + A(5) = 0.1848 + A(6) = 0.1584 + A(7) = 0.1346 + A(8) = 0.1128 + A(9) = 0.0923 + A(10) = 0.0728 + A(11) = 0.0540 + A(12) = 0.0358 + A(13) = 0.0178 + GO TO 115 + 69 IF (N .NE. 28) GO TO 71 + A(1) = 0.4328 + A(2) = 0.2992 + A(3) = 0.2510 + A(4) = 0.2151 + A(5) = 0.1857 + A(6) = 0.1601 + A(7) = 0.1372 + A(8) = 0.1162 + A(9) = 0.0965 + A(10) = 0.0778 + A(11) = 0.0598 + A(12) = 0.0424 + A(13) = 0.0253 + A(14) = 0.0084 + GO TO 115 + 71 IF (N .NE. 29) GO TO 73 + A(1) = 0.4291 + A(2) = 0.2968 + A(3) = 0.2499 + A(4) = 0.2150 + A(5) = 0.1864 + A(6) = 0.1616 + A(7) = 0.1395 + A(8) = 0.1192 + A(9) = 0.1002 + A(10) = 0.0822 + A(11) = 0.0650 + A(12) = 0.0483 + A(13) = 0.0320 + A(14) = 0.0159 + GO TO 115 + 73 IF (N .NE. 30) GO TO 75 + A(1) = 0.4254 + A(2) = 0.2944 + A(3) = 0.2487 + A(4) = 0.2148 + A(5) = 0.1870 + A(6) = 0.1630 + A(7) = 0.1415 + A(8) = 0.1219 + A(9) = 0.1036 + A(10) = 0.0862 + A(11) = 0.0697 + A(12) = 0.0537 + A(13) = 0.0381 + A(14) = 0.0227 + A(15) = 0.0076 + GO TO 115 + 75 IF (N .NE. 31) GO TO 77 + A(1) = 0.4220 + A(2) = 0.2921 + A(3) = 0.2475 + A(4) = 0.2145 + A(5) = 0.1874 + A(6) = 0.1641 + A(7) = 0.1433 + A(8) = 0.1243 + A(9) = 0.1066 + A(10) = 0.0899 + A(11) = 0.0739 + A(12) = 0.0585 + A(13) = 0.0435 + A(14) = 0.0289 + A(15) = 0.0144 + GO TO 115 + 77 IF (N .NE. 32) GO TO 79 + A(1) = 0.4188 + A(2) = 0.2898 + A(3) = 0.2463 + A(4) = 0.2141 + A(5) = 0.1878 + A(6) = 0.1651 + A(7) = 0.1449 + A(8) = 0.1265 + A(9) = 0.1093 + A(10) = 0.0931 + A(11) = 0.0777 + A(12) = 0.0629 + A(13) = 0.0485 + A(14) = 0.0344 + A(15) = 0.0206 + A(16) = 0.0068 + GO TO 115 + 79 IF (N .NE. 33) GO TO 81 + A(1) = 0.4156 + A(2) = 0.2876 + A(3) = 0.2451 + A(4) = 0.2137 + A(5) = 0.1880 + A(6) = 0.1660 + A(7) = 0.1463 + A(8) = 0.1284 + A(9) = 0.1118 + A(10) = 0.0961 + A(11) = 0.0812 + A(12) = 0.0669 + A(13) = 0.0530 + A(14) = 0.0395 + A(15) = 0.0262 + A(16) = 0.0131 + GO TO 115 + 81 IF (N .NE. 34) GO TO 83 + A(1) = 0.4127 + A(2) = 0.2854 + A(3) = 0.2439 + A(4) = 0.2132 + A(5) = 0.1882 + A(6) = 0.1667 + A(7) = 0.1475 + A(8) = 0.1301 + A(9) = 0.1140 + A(10) = 0.0988 + A(11) = 0.0844 + A(12) = 0.0706 + A(13) = 0.0572 + A(14) = 0.0441 + A(15) = 0.0314 + A(16) = 0.0187 + A(17) = 0.0062 + GO TO 115 + 83 IF (N .NE. 35) GO TO 85 + A(1) = 0.4096 + A(2) = 0.2834 + A(3) = 0.2427 + A(4) = 0.2127 + A(5) = 0.1883 + A(6) = 0.1673 + A(7) = 0.1487 + A(8) = 0.1317 + A(9) = 0.1160 + A(10) = 0.1013 + A(11) = 0.0873 + A(12) = 0.0739 + A(13) = 0.0610 + A(14) = 0.0484 + A(15) = 0.0361 + A(16) = 0.0239 + A(17) = 0.0119 + GO TO 115 + 85 IF (N .NE. 36) GO TO 87 + A(1) = 0.4068 + A(2) = 0.2813 + A(3) = 0.2415 + A(4) = 0.2121 + A(5) = 0.1883 + A(6) = 0.1678 + A(7) = 0.1496 + A(8) = 0.1331 + A(9) = 0.1179 + A(10) = 0.1036 + A(11) = 0.0900 + A(12) = 0.0770 + A(13) = 0.0645 + A(14) = 0.0523 + A(15) = 0.0404 + A(16) = 0.0287 + A(17) = 0.0172 + A(18) = 0.0057 + GO TO 115 + 87 IF (N .NE. 37) GO TO 89 + A(1) = 0.4040 + A(2) = 0.2794 + A(3) = 0.2403 + A(4) = 0.2116 + A(5) = 0.1883 + A(6) = 0.1683 + A(7) = 0.1505 + A(8) = 0.1344 + A(9) = 0.1196 + A(10) = 0.1056 + A(11) = 0.0924 + A(12) = 0.0798 + A(13) = 0.0677 + A(14) = 0.0559 + A(15) = 0.0444 + A(16) = 0.0331 + A(17) = 0.0220 + A(18) = 0.0110 + GO TO 115 + 89 IF (N .NE. 38) GO TO 91 + A(1) = 0.4015 + A(2) = 0.2774 + A(3) = 0.2391 + A(4) = 0.2110 + A(5) = 0.1881 + A(6) = 0.1686 + A(7) = 0.1513 + A(8) = 0.1356 + A(9) = 0.1211 + A(10) = 0.1075 + A(11) = 0.0947 + A(12) = 0.0824 + A(13) = 0.0706 + A(14) = 0.0592 + A(15) = 0.0481 + A(16) = 0.0372 + A(17) = 0.0264 + A(18) = 0.0158 + A(19) = 0.0053 + GO TO 115 + 91 IF (N .NE. 39) GO TO 93 + A(1) = 0.3989 + A(2) = 0.2755 + A(3) = 0.2380 + A(4) = 0.2104 + A(5) = 0.1880 + A(6) = 0.1689 + A(7) = 0.1520 + A(8) = 0.1366 + A(9) = 0.1225 + A(10) = 0.1092 + A(11) = 0.0967 + A(12) = 0.0848 + A(13) = 0.0733 + A(14) = 0.0622 + A(15) = 0.0515 + A(16) = 0.0409 + A(17) = 0.0305 + A(18) = 0.0203 + A(19) = 0.0101 + GO TO 115 + 93 IF (N .NE. 40) GO TO 95 + A(1) = 0.3964 + A(2) = 0.2737 + A(3) = 0.2368 + A(4) = 0.2098 + A(5) = 0.1878 + A(6) = 0.1691 + A(7) = 0.1526 + A(8) = 0.1376 + A(9) = 0.1237 + A(10) = 0.1108 + A(11) = 0.0986 + A(12) = 0.0870 + A(13) = 0.0759 + A(14) = 0.0651 + A(15) = 0.0546 + A(16) = 0.0444 + A(17) = 0.0343 + A(18) = 0.0244 + A(19) = 0.0146 + A(20) = 0.0049 + GO TO 115 + 95 IF (N .NE. 41) GO TO 97 + A(1) = 0.3940 + A(2) = 0.2719 + A(3) = 0.2357 + A(4) = 0.2091 + A(5) = 0.1876 + A(6) = 0.1693 + A(7) = 0.1531 + A(8) = 0.1384 + A(9) = 0.1249 + A(10) = 0.1123 + A(11) = 0.1004 + A(12) = 0.0891 + A(13) = 0.0782 + A(14) = 0.0677 + A(15) = 0.0575 + A(16) = 0.0476 + A(17) = 0.0379 + A(18) = 0.0283 + A(19) = 0.0188 + A(20) = 0.0094 + GO TO 115 + 97 IF (N .NE. 42) GO TO 99 + A(1) = 0.3917 + A(2) = 0.2701 + A(3) = 0.2345 + A(4) = 0.2085 + A(5) = 0.1874 + A(6) = 0.1694 + A(7) = 0.1535 + A(8) = 0.1392 + A(9) = 0.1259 + A(10) = 0.1136 + A(11) = 0.1020 + A(12) = 0.0909 + A(13) = 0.0804 + A(14) = 0.0701 + A(15) = 0.0602 + A(16) = 0.0506 + A(17) = 0.0411 + A(18) = 0.0318 + A(19) = 0.0227 + A(20) = 0.0136 + A(21) = 0.0045 + GO TO 115 + 99 IF (N .NE. 43) GO TO 101 + A(1) = 0.3894 + A(2) = 0.2684 + A(3) = 0.2334 + A(4) = 0.2078 + A(5) = 0.1871 + A(6) = 0.1695 + A(7) = 0.1539 + A(8) = 0.1398 + A(9) = 0.1269 + A(10) = 0.1149 + A(11) = 0.1035 + A(12) = 0.0927 + A(13) = 0.0824 + A(14) = 0.0724 + A(15) = 0.0628 + A(16) = 0.0534 + A(17) = 0.0442 + A(18) = 0.0352 + A(19) = 0.0263 + A(20) = 0.0175 + A(21) = 0.0087 + GO TO 115 + 101 IF (N .NE. 44) GO TO 103 + A(1) = 0.3872 + A(2) = 0.2667 + A(3) = 0.2323 + A(4) = 0.2072 + A(5) = 0.1868 + A(6) = 0.1695 + A(7) = 0.1542 + A(8) = 0.1405 + A(9) = 0.1278 + A(10) = 0.1160 + A(11) = 0.1049 + A(12) = 0.0943 + A(13) = 0.0842 + A(14) = 0.0745 + A(15) = 0.0651 + A(16) = 0.0560 + A(17) = 0.0471 + A(18) = 0.0383 + A(19) = 0.0296 + A(20) = 0.0211 + A(21) = 0.0126 + A(22) = 0.0042 + GO TO 115 + 103 IF (N .NE. 45) GO TO 105 + A(1) = 0.3850 + A(2) = 0.2651 + A(3) = 0.2313 + A(4) = 0.2065 + A(5) = 0.1865 + A(6) = 0.1695 + A(7) = 0.1545 + A(8) = 0.1410 + A(9) = 0.1286 + A(10) = 0.1170 + A(11) = 0.1062 + A(12) = 0.0959 + A(13) = 0.0860 + A(14) = 0.0765 + A(15) = 0.0673 + A(16) = 0.0584 + A(17) = 0.0497 + A(18) = 0.0412 + A(19) = 0.0328 + A(20) = 0.0245 + A(21) = 0.0163 + A(22) = 0.0081 + GO TO 115 + 105 IF (N .NE. 46) GO TO 107 + A(1) = 0.3830 + A(2) = 0.2635 + A(3) = 0.2302 + A(4) = 0.2058 + A(5) = 0.1862 + A(6) = 0.1695 + A(7) = 0.1548 + A(8) = 0.1415 + A(9) = 0.1293 + A(10) = 0.1180 + A(11) = 0.1073 + A(12) = 0.0972 + A(13) = 0.0876 + A(14) = 0.0783 + A(15) = 0.0694 + A(16) = 0.0607 + A(17) = 0.0522 + A(18) = 0.0439 + A(19) = 0.0357 + A(20) = 0.0277 + A(21) = 0.0197 + A(22) = 0.0118 + A(23) = 0.0039 + GO TO 115 + 107 IF (N .NE. 47) GO TO 109 + A(1) = 0.3808 + A(2) = 0.2620 + A(3) = 0.2291 + A(4) = 0.2052 + A(5) = 0.1859 + A(6) = 0.1695 + A(7) = 0.1550 + A(8) = 0.1420 + A(9) = 0.1300 + A(10) = 0.1189 + A(11) = 0.1085 + A(12) = 0.0986 + A(13) = 0.0892 + A(14) = 0.0801 + A(15) = 0.0713 + A(16) = 0.0628 + A(17) = 0.0546 + A(18) = 0.0465 + A(19) = 0.0385 + A(20) = 0.0307 + A(21) = 0.0229 + A(22) = 0.0153 + A(23) = 0.0076 + GO TO 115 + 109 IF (N .NE. 48) GO TO 111 + A(1) = 0.3789 + A(2) = 0.2604 + A(3) = 0.2281 + A(4) = 0.2045 + A(5) = 0.1855 + A(6) = 0.1693 + A(7) = 0.1551 + A(8) = 0.1423 + A(9) = 0.1306 + A(10) = 0.1197 + A(11) = 0.1095 + A(12) = 0.0998 + A(13) = 0.0906 + A(14) = 0.0817 + A(15) = 0.0731 + A(16) = 0.0648 + A(17) = 0.0568 + A(18) = 0.0489 + A(19) = 0.0411 + A(20) = 0.0335 + A(21) = 0.0259 + A(22) = 0.0185 + A(23) = 0.0111 + A(24) = 0.0037 + GO TO 115 + 111 IF (N .NE. 49) GO TO 113 + A(1) = 0.3770 + A(2) = 0.2589 + A(3) = 0.2271 + A(4) = 0.2038 + A(5) = 0.1851 + A(6) = 0.1692 + A(7) = 0.1553 + A(8) = 0.1427 + A(9) = 0.1312 + A(10) = 0.1205 + A(11) = 0.1105 + A(12) = 0.1010 + A(13) = 0.0919 + A(14) = 0.0832 + A(15) = 0.0748 + A(16) = 0.0667 + A(17) = 0.0588 + A(18) = 0.0511 + A(19) = 0.0436 + A(20) = 0.0361 + A(21) = 0.0288 + A(22) = 0.0215 + A(23) = 0.0143 + A(24) = 0.0071 + GO TO 115 + 113 IF (N .NE. 50) GO TO 115 + A(1) = 0.3751 + A(2) = 0.2574 + A(3) = 0.2260 + A(4) = 0.2032 + A(5) = 0.1847 + A(6) = 0.1691 + A(7) = 0.1554 + A(8) = 0.1430 + A(9) = 0.1317 + A(10) = 0.1212 + A(11) = 0.1113 + A(12) = 0.1020 + A(13) = 0.0932 + A(14) = 0.0846 + A(15) = 0.0764 + A(16) = 0.0685 + A(17) = 0.0608 + A(18) = 0.0532 + A(19) = 0.0459 + A(20) = 0.0386 + A(21) = 0.0314 + A(22) = 0.0244 + A(23) = 0.0174 + A(24) = 0.0104 + A(25) = 0.0035 + 115 IF (N .GT. 50) WRITE(6,202) + 202 FORMAT(1X,' ') + IF (N .GT. 50) WRITE(6,203) + 203 FORMAT(1X,'THIS IS THE SHAPIRO-WILK TEST FOR SMALL SAMPLES') + IF (N .GT. 50) WRITE (6,204) + 204 FORMAT(1X,'THE SAMPLE SIZE MUST BE LESS THAN OR EQUAL TO 50') + IF (N .GT. 50) GO TO 160 + DO 140 I=1,K + J=N-I+1 + 140 SUMB=SUMB+A(I)*(X(J)-X(I)) + Y(1)=SUMB**2/S2 + DO 150 I=1,N + 150 X(I)=X1(I) + WRITE(6,205) Y(1) + 205 FORMAT(/,2X,'TEST13 SW(N) =',F10.4) + 160 CONTINUE + RETURN + END + SUBROUTINE TEST14(X,Y,N) + DIMENSION X(150),X1(150),Y(2),Z(150),P(150) + EXTERNAL xinormal + EXTERNAL dl + SUMA=0 + SUMB=0 + SUMC=0 + SUMD=0 + DO 10 I =1,N + 10 X1(I)=X(I) + CALL SORT(N,X) + DO 20 I = 1,N + 20 P(I)=(FLOAT(I)-.375)/(0.25+N) + DO 25 I=1,N + 25 Z(I)=XINORMAL(P(I)) + DO 30 I=1,N + SUMA=SUMA+(Z(I)*X(I)) + SUMB=SUMB+(Z(I)**2) + SUMC=SUMC+X(I) + 30 SUMD=SUMD+X(I)**2 + Y(1)=(SUMA**2/SUMB)/(SUMD-SUMC**2/N) + DO 40 I=1,N + 40 X(I)=X1(I) + WRITE(6,100) Y(1) + 100 FORMAT(/,2X,'TEST14 SF(N) =',F10.4) + RETURN + END + SUBROUTINE TEST15(X,Y,N) + DIMENSION X(150),Y(2),X1(150) + REAL MEAN + DO 10 I = 1,N + 10 X1(I)=X(I) + R=FLOAT(N) + CALL SORT(N,X) + XS=X(1) + SUM1=0 + SUM2=0 + DO 15 I =1,N + 15 SUM1=SUM1+X(I) + MEAN=SUM1/N + XBAR=MEAN + DO 20 I = 1,N + 20 SUM2=SUM2+(X(I)*X(I)) + S1=SUM2-((SUM1*SUM1)/N) + B1=SQRT(R/(R-1)) + B=(XBAR-XS)*B1 + Y(1)=(B*B)/S1 + DO 30 I=1,N + 30 X(I)=X1(I) + WRITE(6,100) Y(1) + 100 FORMAT(/,2X,'TEST15 SW(E) =',F10.4) + RETURN + END + SUBROUTINE TEST16(X,Y,N) + DIMENSION X(150),Y(2),FX(150),FN1(150),FN2(150) + DIMENSION X1(150) + REAL MEAN + SUM1=0 + SUM2=0 + SUM3=0 + SUM4=0 + SUM5=0 + DO 10 I = 1,N + 10 X1(I)=X(I) + DO 15 I =1,N + 15 SUM1=SUM1+X(I) + MEAN=SUM1/N + XBAR=MEAN + DO 20 I = 1,N + 20 SUM2=SUM2+(X(I)*X(I)) + S1=((N*SUM2)-(SUM1*SUM1))/(N*(N-1)) + SDX=SQRT(S1) + R=FLOAT(N) + CALL SORT(N,X) + DO 30 I=1,N + FN2(I)=FLOAT((2*I)-1)/FLOAT(2*N) + FX(I)=1-EXP(X(I)*(-1.0)/XBAR) + 30 CONTINUE + DO 40 I=1,N + A=((2*I)-1)*ALOG(FX(I)) + B=((2*I)-1)*ALOG(1.0-FX(N+1-I)) + SUM3=SUM3+A+B + SUM4=SUM4+((-FN2(I)+FX(I))**2) + FN1(I)=ABS(FN1(I)-FX(I)) + 40 CONTINUE + CVM=1./FLOAT(12*N)+SUM4 + CVMOD=CVM*(1.+(.16/R)) + Y(1)=CVMOD + DO 50 I=1,N + 50 X(I)=X1(I) + WRITE(6,100) Y(1) + 100 FORMAT(/,2X,'TEST16 CVM(E) =',F10.4) + RETURN + END + SUBROUTINE TEST17(X,Y,N) + DIMENSION X(150),Y(2),FX(150) + DIMENSION T(150),Z(150),D(2),FN3(150),X1(150) + REAL MEAN + REAL KS + SUM1=0 + SUM2=0 + SUM3=0 + SUM4=0 + SUM5=0 + DO 10 I = 1,N + 10 X1(I)=X(I) + DO 15 I =1,N + 15 SUM1=SUM1+X(I) + MEAN=SUM1/N + XBAR=MEAN + DO 20 I = 1,N + 20 SUM2=SUM2+(X(I)*X(I)) + S1=((N*SUM2)-(SUM1*SUM1))/(N*(N-1)) + SDX=SQRT(S1) + R=FLOAT(N) + CALL SORT(N,X) + DO 30 I=1,N + FN3(I)=FLOAT(I)/R + FX(I)=1-EXP(X(I)*(-1.0)/XBAR) + 30 CONTINUE + DO 40 J=1,N + 40 Z(J)=(FN3(J)-FX(J)) + CALL SORT(N,Z) + D1=Z(N) + DO 50 J=1,N + 50 T(J)=FX(J)-((J-1)/R) + CALL SORT(N,T) + D2=T(N) + D(1)=D1 + D(2)=D2 + CALL SORT(2,D) + DMAX=D(2) + KS=(DMAX-0.2/R)*(SQRT(R)+(.5/SQRT(R))+0.26) + Y(1)=KS + DO 60 I=1,N + 60 X(I)=X1(I) + WRITE(6,100) Y(1) + 100 FORMAT(/,2X,'TEST17 KSD(E) =',F10.4) + RETURN + END + SUBROUTINE TEST18(X,Y,N) + DIMENSION X(150),Y(2),FX(150) + DIMENSION T(150),Z(150),D(2),FN3(150),X1(150) + REAL MEAN + SUM1=0 + SUM2=0 + SUM3=0 + SUM4=0 + SUM5=0 + DO 10 I = 1,N + 10 X1(I)=X(I) + DO 15 I =1,N + 15 SUM1=SUM1+X(I) + MEAN=SUM1/N + XBAR=MEAN + DO 20 I = 1,N + 20 SUM2=SUM2+(X(I)*X(I)) + S1=((N*SUM2)-(SUM1*SUM1))/(N*(N-1)) + SDX=SQRT(S1) + R=FLOAT(N) + CALL SORT(N,X) + DO 30 I=1,N + FN3(I)=FLOAT(I)/R + FX(I)=1-EXP(X(I)*(-1.0)/XBAR) + 30 CONTINUE + DO 40 J=1,N + 40 Z(J)=(FN3(J)-FX(J)) + CALL SORT(N,Z) + D1=Z(N) + DO 50 J=1,N + 50 T(J)=FX(J)-((J-1)/R) + CALL SORT(N,T) + D2=T(N) + D(1)=D1 + D(2)=D2 + V=D(1)+D(2) + V=(V-0.2/R)*(SQRT(R)+(.35/SQRT(R))+0.24) + Y(1)=V + DO 60 I=1,N + 60 X(I)=X1(I) + WRITE(6,100) Y(1) + 100 FORMAT(/,2X,'TEST18 KV(E) =',F10.4) + RETURN + END + SUBROUTINE TEST19(X,Y,N) + DIMENSION X(150),Y(2),FX(150),FN1(150),FN2(150) + DIMENSION X1(150) + REAL MEAN + SUM1=0 + SUM2=0 + SUM3=0 + SUM4=0 + SUM5=0 + DO 10 I = 1,N + 10 X1(I)=X(I) + DO 15 I =1,N + 15 SUM1=SUM1+X(I) + MEAN=SUM1/N + XBAR=MEAN + DO 20 I = 1,N + 20 SUM2=SUM2+(X(I)*X(I)) + S1=((N*SUM2)-(SUM1*SUM1))/(N*(N-1)) + SDX=SQRT(S1) + R=FLOAT(N) + CALL SORT(N,X) + DO 30 I=1,N + FN2(I)=FLOAT((2*I)-1)/FLOAT(2*N) + FX(I)=1-EXP(X(I)*(-1.0)/XBAR) + 30 CONTINUE + DO 40 I=1,N + A=((2*I)-1)*ALOG(FX(I)) + B=((2*I)-1)*ALOG(1.0-FX(N+1-I)) + SUM3=SUM3+A+B + SUM4=SUM4+((-FN2(I)+FX(I))**2) + FN1(I)=ABS(FN1(I)-FX(I)) + 40 CONTINUE + CVM=1./FLOAT(12*N)+SUM4 + DO 50 I=1,N + SUM5=SUM5+FX(I) + 50 CONTINUE + ZBAR=SUM5/R + W=CVM-R*(ZBAR-0.5)*(ZBAR-0.5) + W=W*(1.0+0.16/R) + Y(1)=W + DO 60 I=1,N + 60 X(I)=X1(I) + WRITE(6,100) Y(1) + 100 FORMAT(/,2X,'TEST19 WU2(E) =',F10.4) + RETURN + END + SUBROUTINE TEST20(X,Y,N) + DIMENSION X(150),Y(2),FX(150),FN1(150),FN2(150) + DIMENSION X1(150) + REAL MEAN + SUM1=0 + SUM2=0 + SUM3=0 + SUM4=0 + SUM5=0 + DO 10 I = 1,N + 10 X1(I)=X(I) + DO 15 I =1,N + 15 SUM1=SUM1+X(I) + MEAN=SUM1/N + XBAR=MEAN + DO 20 I = 1,N + 20 SUM2=SUM2+(X(I)*X(I)) + S1=((N*SUM2)-(SUM1*SUM1))/(N*(N-1)) + SDX=SQRT(S1) + R=FLOAT(N) + CALL SORT(N,X) + DO 30 I=1,N + FN2(I)=FLOAT((2*I)-1)/FLOAT(2*N) + FX(I)=1-EXP(X(I)*(-1.0)/XBAR) + 30 CONTINUE + DO 40 I=1,N + A=((2*I)-1)*ALOG(FX(I)) + B=((2*I)-1)*ALOG(1.0-FX(N+1-I)) + SUM3=SUM3+A+B + SUM4=SUM4+((-FN2(I)+FX(I))**2) + FN1(I)=ABS(FN1(I)-FX(I)) + 40 CONTINUE + ADB=SUM3/R + ADSTAT=-R-ADB + ADEXP=ADSTAT*(1.0+(0.3/R)) + Y(1)=ADEXP + DO 50 I=1,N + 50 X(I)=X1(I) + WRITE(6,100) Y(1) + 100 FORMAT(/,2X,'TEST20 AD(E) =',F10.4) + RETURN + END + SUBROUTINE TEST21(X,Y,N) + DIMENSION X(150),Y(2),V(150),V2(150),F(150),P(150) + REAL MEAN + SUM1=0 + SUM3=0 + A=4*((0.75*(N-1)*(N-1))**2) + K1=A + C=A-K1 + IF(C .GT. 0.5) K1=K1+1 + 10 R=N/K1 + IF(R .LT. 5) K1=K1-1 + IF(R .LT.5) GOTO 10 + K2=K1-1 + DO 15 I=1,K1 + 15 F(I)=0 + DO 20 I=1,N + 20 SUM1=SUM1+X(I) + MEAN=N/SUM1 + DO 30 I=1,K2 + 30 P(I)=FLOAT(I)/K1 + DO 40 I=1,K2 + 40 V(I)=(-1.0/MEAN)*ALOG(1-P(I)) + DO 41 I=1,K2 + 41 V2(I+1)=V(I)+0.0001 + DO 55 I=1,N + DO 45 J=2,K2 + IF(X(I) .GE. V2(J) .AND. X(I) .LE. V(J)) F(J)=1+F(J) + 45 CONTINUE + IF(X(I) .GE. V2(K2+1)) F(K2+1)=F(K2+1)+1 + IF(X(I) .LE. V(1)) F(1)=F(1)+1 + 55 CONTINUE + DO 65 I=1,K1 + 65 SUM3=SUM3+F(I)*F(I) + Y(1)=SUM3*K1/N-N + Y(2)=FLOAT(K1)-2 + WRITE(6,100) Y(1),Y(2) + 100 FORMAT(/,2X,'TEST21 CS(E) =',F10.4,' DOF =',F10.4) + RETURN + END + SUBROUTINE TEST22(X,Y,N) + DIMENSION X(150),Y(2) + REAL MEAN + REAL M1 + SUM1=0 + SUM2=0 + SUM3=0 + SUM4=0 + SUM5=0 + DO 15 I =1,N + 15 SUM1=SUM1+X(I) + MEAN=SUM1/N + DO 20 I = 1,N + 20 SUM2=SUM2+ALOG(X(I)) + S1=((N*SUM2)-(SUM1*SUM1))/(N*(N-1)) + M1=SUM2/N + DO 25 I=1,N + 25 SUM4=SUM4+(ALOG(X(I))-M1)*(ALOG(X(I))-M1) + S2=SUM4/N + DO 30 I=1,N + 30 SUM3=SUM3+(X(I)-MEAN)**3 + IF(SUM3 .GE. 0) GO TO 40 + WRITE(6,100) (X(I),I=1,N) + 100 FORMAT(4(1X,F10.4,2X)) + WRITE(6,200) + 200 FORMAT(1X,'THIRD SAMPLE MOMENT ABOUT THE MEAN IS LESS THAN ZERO') + WRITE(6,300) + 300 FORMAT(1X,'HENCE WE ACCEPT THE NULL HYPOTHESIS OF NORMALITY') + Y(1)=0 + GO TO 50 + 40 CONTINUE + DO 45 I=1,N + 45 SUM5=SUM5+(X(I)-MEAN)*(X(I)-MEAN) + S1=SQRT(SUM5/N) + S3=SQRT(S2) + E1=EXP(M1) + Y(1)=S1/(S3*E1) + 50 WRITE(6,400) Y(1) + 400 FORMAT(/,2X,'TEST22 LR(NL) =',F10.4) + RETURN + END + SUBROUTINE TEST23(X,Y,N) + DIMENSION X(150),Y(2) + SUM2=0 + SUM4=0 + DO 20 I = 1,N + 20 SUM2=SUM2+ALOG(X(I)) + DO 25 I=1,N + 25 SUM4=SUM4+(ALOG(X(I))-(SUM2/N))*(ALOG(X(I))-(SUM2/N)) + S2=SUM4/(N-1) + S3=EXP(S2)-1 + S4=SQRT(S3) + Y(1)=S4 + WRITE(6,100) Y(1) + 100 FORMAT(/,2X,'TEST23 CV(L) =',F10.4) + RETURN + END + SUBROUTINE TEST24(X,Y,N) + DIMENSION X(150),Y(2) + SUM1=0 + SUM2=0 + SUM3=0 + SUM4=0 + SUM5=0 + R=FLOAT(N) + DO 15 I =1,N + 15 SUM1=SUM1+X(I) + B1=SUM1/N + DO 20 I = 1,N + 20 SUM2=SUM2+ALOG(X(I)) + A1=SUM2/N + DO 25 I=1,N + 25 SUM4=SUM4+(ALOG(X(I))-A1)*(ALOG(X(I))-A1) + A2=SUM4/N + B3=EXP(2*A1+A2)*(EXP(A2)-1) + C1=ALOG(A2/B3) + C2=(EXP(4*A2)+(2*EXP(3*A2))-4)/4-A2+(0.75*EXP(A2)) + C3=A2*(2*EXP(A2)-1)*(2*EXP(A2)-1) + C4=2*(EXP(A2)-1)*(EXP(A2)-1) + C5=C3/C4 + IF(C2 .LT. C5) GO TO 30 + C6=SQRT(C2-C5)*2*(SQRT(R)) + Y(1)=C1/C6 + GO TO 40 + 30 WRITE(6,35) + Y(1)=999999999 + 35 FORMAT(/,2X,'WARNING!!! STATISTICS FOR THE NEXT TEST WILL', + +' NOT BE CALCULATED DUE TO SMALL LOGVARIANCE') + 40 WRITE(6,100) Y(1) + 100 FORMAT(/,2X,'TEST24 KT(LN) =',F10.4) + RETURN + END + SUBROUTINE SORT(NN,X) + INTEGER I,J,K,L,IJ,NN,M,IU(16),IL(16) + REAL Y,YY,X(500) + M=1 + I=1 + J=NN + 150 IF (I .GE. J) GO TO 220 + 160 K=I + IJ=(J+I)/2 + Y=X(IJ) + IF (X(I) .LE. Y) GO TO 170 + X(IJ)=X(I) + X(I)=Y + Y=X(IJ) + 170 L=J + IF (X(J) .GE. Y) GO TO 190 + X(IJ)=X(J) + X(J)=Y + Y=X(IJ) + IF (X(I) .LE. Y) GO TO 190 + X(IJ)=X(I) + X(I)=Y + Y=X(IJ) + GO TO 190 + 180 X(L)=X(K) + X(K)=YY + 190 L=L-1 + IF (X(L) .GT. Y) GO TO 190 + YY=X(L) + 200 K=K+1 + IF (X(K) .LT. Y) GO TO 200 + IF (K .LE. L) GO TO 180 + IF (L-I .LE. J-K) GO TO 210 + IL(M)=I + IU(M)=L + I=K + M=M+1 + GO TO 230 + 210 IL(M)=K + IU(M)=J + J=L + M=M+1 + GO TO 230 + 220 M=M-1 + IF (M .EQ. 0) GO TO 260 + I=IL(M) + J=IU(M) + 230 IF (J-I .GE. 1) GO TO 160 + I=I-1 + 240 I=I+1 + IF (I .EQ. J) GO TO 220 + Y=X(I+1) + IF (X(I) .LE. Y) GO TO 240 + K=I + 250 X(K+I)=X(K) + K=K-1 + IF (Y .LT. X(K)) GO TO 250 + X(K+1)=Y + GO TO 240 + 260 CONTINUE + RETURN + END + FUNCTION enormp(x) + REAL x + DOUBLE PRECISION x1,x2,x3,x4,yy1,yy2 + xp1 = 0.771058495001320D-04 + xp2 = -0.00133733772997339D0 + xp3 = 0.0323076579225834D0 + xp4 = 0.0479137145607681D0 + xp5 = 0.128379167095513D0 + xq1 = 0.00301048631703895D0 + xq2 = 0.0538971687740286D0 + xq3 = 0.375795757275549D0 + xr1 = -1.36864857382717D-07 + xr2 = 0.564195517478974D0 + xr3 = 7.21175825088309D0 + xr4 = 43.1622272220567D0 + xr5 = 152.989285046940D0 + xr6 = 339.320816734344D0 + xr7 = 451.918953711873D0 + xr8 = 300.459261020162D0 + xs1 = 1.0D0 + xs2 = 12.7827273196294D0 + xs3 = 77.0001529352295D0 + xs4 = 277.585444743988D0 + xs5 = 638.980264465631D0 + xs6 = 931.354094850610D0 + xs7 = 790.950925327898D0 + xs8 = 300.459260956983D0 + xt1 = 2.10144126479064D0 + xt2 = 26.2370141675169D0 + xt3 = 21.3688200555087D0 + xt4 = 4.65807828718470D0 + xt5 = 0.282094791773523D0 + xu1 = 94.1537750555460D0 + xu2 = 187.114811799590D0 + xu3 = 99.0191814623914D0 + xu4 = 18.0124575948747D0 + x3 = 0.564189583547756D0 + x1 = abs(x) + IF (x1.GT.0.5D0) GO TO 10 + x4 = x*x + yy1 = ((((xp1*x4+xp2)*x4+xp3)*x4+xp4)*x4+xp5) +1.0D0 + yy2 = (((xq1*x4+xq2)*x4+xq3)*x4) + 1.0D0 + enormp = x* (yy1/yy2) + RETURN + 10 IF (x1.GT.4.0D0) GO TO 20 + yy1 = ((((((xr1*x1+xr2)*x1+xr3)*x1+xr4)*x1+xr5)*x1 + + + xr6)*x1+xr7)*x1+xr8 + yy2 = ((((((xs1*x1+xs2)*x1+xs3)*x1+xs4)*x1+xs5)*x1 + + + xs6)*x1+xs7)*x1+xs8 + enormp = 1.0D0-exp(-x*x)*yy1/yy2 + IF (x .LT. 0.0D0) enormp = -enormp + RETURN + 20 x2 = x*x + x4=1.0D0*x4 + yy1 = ((((xt1*x4+xt2)*x4+xt3)*x4+xt4)*x4) + xt5 + yy2 = ((((xu1*x4+xu2)*x4+xu3)*x4+xu4)*x4) + 1.0D0 + enormp = (x3/x1)-(yy1*x1)/(x2*yy2) + enormp = 1.0D0-exp(-x2)*enormp + IF (x .LT. 0.0D0) enormp = -enormp + RETURN + END + FUNCTION xinormal(p) + DOUBLE PRECISION px,pw,f0 + p0=-0.322232431088D0 + p1=-1.0D0 + p2=-0.342242088547D0 + p3=-0.0204231210245D0 + p4=-0.0000453642210148D0 + q0=0.099348462606D0 + q1=0.588581570495D0 + q2=0.531103462366D0 + q3=0.10353775285D0 + q4=0.0038560700634D0 + pind=p + IF (p .LT. 1.0E-10) GO TO 10 + GO TO 20 + 10 xinormal = -10 + RETURN + 20 IF (p .GE. 1.0) GO TO 30 + GO TO 40 + 30 xinormal = 10 + RETURN + IF (p .EQ. 0.5D0) GO TO 35 + GO TO 40 + 35 xinormal = 0.5 + RETURN + 40 IF (p .GT. 0.5D0) p=p-1 + pw=SQRT(ALOG(1/(p*p))) + f0=(((pw*q4+q3)*pw+q2)*pw+q1)*pw+q0 + px=pw+((((pw*p4+p3)*pw+p2)*pw+p1)*pw+p0)/f0 + if (pind .LT. 0.5D0) px=-px + xinormal = px + RETURN + END +``` + diff --git a/lib/external/Makefile b/lib/external/Makefile index 5453bb931f0..6c28110144c 100644 --- a/lib/external/Makefile +++ b/lib/external/Makefile @@ -3,6 +3,7 @@ MODULE_TOPDIR = ../.. SUBDIRS = \ ccmath \ + parson \ shapelib include $(MODULE_TOPDIR)/include/Make/Dir.make diff --git a/lib/external/README.license b/lib/external/README.license index d36c4b5b03e..071160af0cc 100644 --- a/lib/external/README.license +++ b/lib/external/README.license @@ -17,3 +17,7 @@ comments carefully. * shapelib/ (MIT/X) Shapefile read/write functions. Copyright (c) 2007, Frank Warmerdam + + * parson/ (MIT/X) + JSON parsing and encoding functions version 1.5.2. + Copyright (c) 2022, Krzysztof Gabis diff --git a/lib/external/README.md b/lib/external/README.md new file mode 100644 index 00000000000..ef26276acff --- /dev/null +++ b/lib/external/README.md @@ -0,0 +1,120 @@ +# How to add a new external library + +The following procedure will walk you through adding a new third-party +library to GRASS GIS. + +## License + +Before you start, make sure that the library you want to include is in +compliance with the GRASS GIS licence. + +### Approved Third-Party Licenses + +* MIT/X +* GPLv2 +* LGPL 2.1 +* MPL 2.0 +* BSD-3-Clause +* Public Domain + +## Procedure + +* Create a directory that will contain the new library's files +in `lib/external/` + +* Add the new libraries header and source files, license, and README to the newly +created directory. + +* Add a new `Makefile` to the directory. + +```make +MODULE_TOPDIR = ../../.. + +# replace SHAPE with new library name +LIB = SHAPE + +include $(MODULE_TOPDIR)/include/Make/Lib.make + +default: headers + $(MAKE) lib + +# Update header file reference to the new library +headers: $(ARCH_INCDIR)/shapefil.h + +$(ARCH_INCDIR)/%.h: %.h + $(INSTALL_DATA) $< $@ +``` + +* Update `lib/external/Makefile` to include the new subdirectory. + +```make +MODULE_TOPDIR = ../.. + +SUBDIRS = \ + ccmath \ + parson \ + shapelib \ + # Add new directory here + +include $(MODULE_TOPDIR)/include/Make/Dir.make + +default: parsubdirs +``` + +* Update the `lib/external/README.license` with a new entry containing + the *library name*, *license*, *description*, *version*, *copyright*, + and *authors name*. + +```txt +* parson/ (MIT/X) + JSON parsing and encoding functions version 1.5.2. + Copyright (c) 2022, Krzysztof Gabis +``` + +* Update `lib/README` with a new entry under external libraries. + +```md + external: external libraries + - external/parson: JSON serialization and deserialization functions + - external/shapelib: SHAPE file management functions + +``` + +* Update `include/Make/Install.make` + +```make +-cp -rL --parents lib/external/ ./grass-lib-$(GRASS_VERSION_NUMBER) +``` + +* Add reference to library in `include/Make/Grass.make` in alphabetical order. + +```make +: + +# example for the parson library +PARSON:parson \ +``` + +* Add reference to library in the root `Makefile`. + +```make +LIBDIRS = \ + lib/external/parson \ + lib/external/shapelib \ + # New reference + ... +``` + +* The library should now be able to successfully compile. + +To test run the `make` command. + +```bash +make +``` + +* If no errors are found the library should now be able to be used in development. + +```c +#include +``` diff --git a/lib/external/parson/LICENSE b/lib/external/parson/LICENSE new file mode 100644 index 00000000000..4431a611d24 --- /dev/null +++ b/lib/external/parson/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2012 - 2022 Krzysztof Gabis + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/lib/external/parson/Makefile b/lib/external/parson/Makefile new file mode 100644 index 00000000000..43aca3bc351 --- /dev/null +++ b/lib/external/parson/Makefile @@ -0,0 +1,13 @@ +MODULE_TOPDIR = ../../.. + +LIB = PARSON + +include $(MODULE_TOPDIR)/include/Make/Lib.make + +default: headers + $(MAKE) lib + +headers: $(ARCH_INCDIR)/parson.h + +$(ARCH_INCDIR)/%.h: %.h + $(INSTALL_DATA) $< $@ diff --git a/lib/external/parson/README.md b/lib/external/parson/README.md new file mode 100644 index 00000000000..2557a8f7943 --- /dev/null +++ b/lib/external/parson/README.md @@ -0,0 +1,189 @@ +# Parson + +## About + +Parson is a lightweight [json](http://json.org) library written in C. + +## Features + +* Lightweight (only 2 files) +* Simple API +* Addressing json values with dot notation (similar to C structs or objects +in most OO languages, e.g. "objectA.objectB.value") +* C89 compatible +* Test suites + +## Installation + +Run: + +```bash +git clone https://github.com/kgabis/parson.git +``` + +and copy parson.h and parson.c to you source code tree. + +Run ```bash make test``` to compile and run tests. + +## Examples + +### Parsing JSON + +Here is a function, which prints basic commit info (date, sha and author) from +a github repository. + +```c +void print_commits_info(const char *username, const char *repo) { + JSON_Value *root_value; + JSON_Array *commits; + JSON_Object *commit; + size_t i; + + char curl_command[512]; + char cleanup_command[256]; + char output_filename[] = "commits.json"; + + /* it ain't pretty, but it's not a libcurl tutorial */ + sprintf(curl_command, + "curl -s \"https://api.github.com/repos/%s/%s/commits\" > %s", + username, repo, output_filename); + sprintf(cleanup_command, "rm -f %s", output_filename); + system(curl_command); + + /* parsing json and validating output */ + root_value = json_parse_file(output_filename); + if (json_value_get_type(root_value) != JSONArray) { + system(cleanup_command); + return; + } + + /* getting array from root value and printing commit info */ + commits = json_value_get_array(root_value); + printf("%-10.10s %-10.10s %s\n", "Date", "SHA", "Author"); + for (i = 0; i < json_array_get_count(commits); i++) { + commit = json_array_get_object(commits, i); + printf("%.10s %.10s %s\n", + json_object_dotget_string(commit, "commit.author.date"), + json_object_get_string(commit, "sha"), + json_object_dotget_string(commit, "commit.author.name")); + } + + /* cleanup code */ + json_value_free(root_value); + system(cleanup_command); +} +``` + +Calling + +```c +print_commits_info("torvalds", "linux"); + +``` + +prints: + +```csv +Date SHA Author +2012-10-15 dd8e8c4a2c David Rientjes +2012-10-15 3ce9e53e78 Michal Marek +2012-10-14 29bb4cc5e0 Randy Dunlap +2012-10-15 325adeb55e Ralf Baechle +2012-10-14 68687c842c Russell King +2012-10-14 ddffeb8c4d Linus Torvalds +... +``` + +### Persistence + +In this example I'm using parson to save user information to a file and then +load it and validate later. + +```c +void persistence_example(void) { + JSON_Value *schema = json_parse_string("{\"name\":\"\"}"); + JSON_Value *user_data = json_parse_file("user_data.json"); + char buf[256]; + const char *name = NULL; + if (user_data == NULL || json_validate(schema, user_data) != JSONSuccess) { + puts("Enter your name:"); + scanf("%s", buf); + user_data = json_value_init_object(); + json_object_set_string(json_object(user_data), "name", buf); + json_serialize_to_file(user_data, "user_data.json"); + } + name = json_object_get_string(json_object(user_data), "name"); + printf("Hello, %s.", name); + json_value_free(schema); + json_value_free(user_data); + return; +} +``` + +### Serialization + +Creating JSON values is very simple thanks to the dot notation. +Object hierarchy is automatically created when addressing specific fields. +In the following example I create a simple JSON value containing basic +information about a person. + +```c +void serialization_example(void) { + JSON_Value *root_value = json_value_init_object(); + JSON_Object *root_object = json_value_get_object(root_value); + char *serialized_string = NULL; + json_object_set_string(root_object, "name", "John Smith"); + json_object_set_number(root_object, "age", 25); + json_object_dotset_string(root_object, "address.city", "Cupertino"); + json_object_dotset_value( + root_object, + "contact.emails", + json_parse_string( + "[\"email@example.com\", + \"email2@example.com\"]" + ) + ); + serialized_string = json_serialize_to_string_pretty(root_value); + puts(serialized_string); + json_free_serialized_string(serialized_string); + json_value_free(root_value); +} + +``` + +Output: + +```json +{ + "name": "John Smith", + "age": 25, + "address": { + "city": "Cupertino" + }, + "contact": { + "emails": [ + "email@example.com", + "email2@example.com" + ] + } +} +``` + +## Contributing + +I will always merge *working* bug fixes. However, if you want to add something +new to the API, please create an "issue" on github for this first so we can +discuss if it should end up in the library before you start implementing it. +Remember to follow parson's code style and write appropriate tests. + +## My other projects + +* [ape](https://github.com/kgabis/ape) - simple programming language +implemented in C library +* [kgflags](https://github.com/kgabis/kgflags) - easy to use command-line flag +parsing library +* [agnes](https://github.com/kgabis/agnes) - header-only NES emulation library + +## License + +[The MIT License (MIT)](http://opensource.org/licenses/mit-license.php) diff --git a/lib/external/parson/parson.c b/lib/external/parson/parson.c new file mode 100644 index 00000000000..21490938bb7 --- /dev/null +++ b/lib/external/parson/parson.c @@ -0,0 +1,2855 @@ +/* + SPDX-License-Identifier: MIT + + Parson 1.5.3 (https://github.com/kgabis/parson) + Copyright (c) 2012 - 2023 Krzysztof Gabis + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS +#endif /* _CRT_SECURE_NO_WARNINGS */ +#endif /* _MSC_VER */ + +#include "parson.h" + +#define PARSON_IMPL_VERSION_MAJOR 1 +#define PARSON_IMPL_VERSION_MINOR 5 +#define PARSON_IMPL_VERSION_PATCH 3 + +#if (PARSON_VERSION_MAJOR != PARSON_IMPL_VERSION_MAJOR) || \ + (PARSON_VERSION_MINOR != PARSON_IMPL_VERSION_MINOR) || \ + (PARSON_VERSION_PATCH != PARSON_IMPL_VERSION_PATCH) +#error "parson version mismatch between parson.c and parson.h" +#endif + +#include +#include +#include +#include +#include +#include +#include + +/* Apparently sscanf is not implemented in some "standard" libraries, so don't + * use it, if you don't have to. */ +#ifdef sscanf +#undef sscanf +#define sscanf THINK_TWICE_ABOUT_USING_SSCANF +#endif + +/* strcpy is unsafe */ +#ifdef strcpy +#undef strcpy +#endif +#define strcpy USE_MEMCPY_INSTEAD_OF_STRCPY + +#define STARTING_CAPACITY 16 +#define MAX_NESTING 2048 + +#ifndef PARSON_DEFAULT_FLOAT_FORMAT +#define PARSON_DEFAULT_FLOAT_FORMAT \ + "%1.17g" /* do not increase precision without incresing NUM_BUF_SIZE */ +#endif + +#ifndef PARSON_NUM_BUF_SIZE +#define PARSON_NUM_BUF_SIZE \ + 64 /* double printed with "%1.17g" shouldn't be longer than 25 bytes so \ + let's be paranoid and use 64 */ +#endif + +#ifndef PARSON_INDENT_STR +#define PARSON_INDENT_STR " " +#endif + +#define SIZEOF_TOKEN(a) (sizeof(a) - 1) +#define SKIP_CHAR(str) ((*str)++) +#define SKIP_WHITESPACES(str) \ + while (isspace((unsigned char)(**str))) { \ + SKIP_CHAR(str); \ + } +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +#undef malloc +#undef free + +#if defined(isnan) && defined(isinf) +#define IS_NUMBER_INVALID(x) (isnan((x)) || isinf((x))) +#else +#define IS_NUMBER_INVALID(x) (((x)*0.0) != 0.0) +#endif + +#define OBJECT_INVALID_IX ((size_t)-1) + +static JSON_Malloc_Function parson_malloc = malloc; +static JSON_Free_Function parson_free = free; + +static int parson_escape_slashes = 1; + +static char *parson_float_format = NULL; + +static JSON_Number_Serialization_Function parson_number_serialization_function = + NULL; + +#define IS_CONT(b) \ + (((unsigned char)(b)&0xC0) == 0x80) /* is utf-8 continuation byte */ + +typedef int parson_bool_t; + +#define PARSON_TRUE 1 +#define PARSON_FALSE 0 + +typedef struct json_string { + char *chars; + size_t length; +} JSON_String; + +/* Type definitions */ +typedef union json_value_value { + JSON_String string; + double number; + JSON_Object *object; + JSON_Array *array; + int boolean; + int null; +} JSON_Value_Value; + +struct json_value_t { + JSON_Value *parent; + JSON_Value_Type type; + JSON_Value_Value value; +}; + +struct json_object_t { + JSON_Value *wrapping_value; + size_t *cells; + unsigned long *hashes; + char **names; + JSON_Value **values; + size_t *cell_ixs; + size_t count; + size_t item_capacity; + size_t cell_capacity; +}; + +struct json_array_t { + JSON_Value *wrapping_value; + JSON_Value **items; + size_t count; + size_t capacity; +}; + +/* Various */ +static char *read_file(const char *filename); +static void remove_comments(char *string, const char *start_token, + const char *end_token); +static char *parson_strndup(const char *string, size_t n); +static char *parson_strdup(const char *string); +static int parson_sprintf(char *s, const char *format, ...); +static int hex_char_to_int(char c); +static JSON_Status parse_utf16_hex(const char *string, unsigned int *result); +static int num_bytes_in_utf8_sequence(unsigned char c); +static JSON_Status verify_utf8_sequence(const unsigned char *string, int *len); +static parson_bool_t is_valid_utf8(const char *string, size_t string_len); +static parson_bool_t is_decimal(const char *string, size_t length); +static unsigned long hash_string(const char *string, size_t n); + +/* JSON Object */ +static JSON_Object *json_object_make(JSON_Value *wrapping_value); +static JSON_Status json_object_init(JSON_Object *object, size_t capacity); +static void json_object_deinit(JSON_Object *object, parson_bool_t free_keys, + parson_bool_t free_values); +static JSON_Status json_object_grow_and_rehash(JSON_Object *object); +static size_t json_object_get_cell_ix(const JSON_Object *object, + const char *key, size_t key_len, + unsigned long hash, + parson_bool_t *out_found); +static JSON_Status json_object_add(JSON_Object *object, char *name, + JSON_Value *value); +static JSON_Value *json_object_getn_value(const JSON_Object *object, + const char *name, size_t name_len); +static JSON_Status json_object_remove_internal(JSON_Object *object, + const char *name, + parson_bool_t free_value); +static JSON_Status json_object_dotremove_internal(JSON_Object *object, + const char *name, + parson_bool_t free_value); +static void json_object_free(JSON_Object *object); + +/* JSON Array */ +static JSON_Array *json_array_make(JSON_Value *wrapping_value); +static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value); +static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity); +static void json_array_free(JSON_Array *array); + +/* JSON Value */ +static JSON_Value *json_value_init_string_no_copy(char *string, size_t length); +static const JSON_String *json_value_get_string_desc(const JSON_Value *value); + +/* Parser */ +static JSON_Status skip_quotes(const char **string); +static JSON_Status parse_utf16(const char **unprocessed, char **processed); +static char *process_string(const char *input, size_t input_len, + size_t *output_len); +static char *get_quoted_string(const char **string, size_t *output_string_len); +static JSON_Value *parse_object_value(const char **string, size_t nesting); +static JSON_Value *parse_array_value(const char **string, size_t nesting); +static JSON_Value *parse_string_value(const char **string); +static JSON_Value *parse_boolean_value(const char **string); +static JSON_Value *parse_number_value(const char **string); +static JSON_Value *parse_null_value(const char **string); +static JSON_Value *parse_value(const char **string, size_t nesting); + +/* Serialization */ +static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, + int level, parson_bool_t is_pretty, + char *num_buf); +static int json_serialize_string(const char *string, size_t len, char *buf); + +/* Various */ +static char *read_file(const char *filename) +{ + FILE *fp = fopen(filename, "r"); + size_t size_to_read = 0; + size_t size_read = 0; + long pos; + char *file_contents; + if (!fp) { + return NULL; + } + fseek(fp, 0L, SEEK_END); + pos = ftell(fp); + if (pos < 0) { + fclose(fp); + return NULL; + } + size_to_read = pos; + rewind(fp); + file_contents = (char *)parson_malloc(sizeof(char) * (size_to_read + 1)); + if (!file_contents) { + fclose(fp); + return NULL; + } + size_read = fread(file_contents, 1, size_to_read, fp); + if (size_read == 0 || ferror(fp)) { + fclose(fp); + parson_free(file_contents); + return NULL; + } + fclose(fp); + file_contents[size_read] = '\0'; + return file_contents; +} + +static void remove_comments(char *string, const char *start_token, + const char *end_token) +{ + parson_bool_t in_string = PARSON_FALSE, escaped = PARSON_FALSE; + size_t i; + char *ptr = NULL, current_char; + size_t start_token_len = strlen(start_token); + size_t end_token_len = strlen(end_token); + if (start_token_len == 0 || end_token_len == 0) { + return; + } + while ((current_char = *string) != '\0') { + if (current_char == '\\' && !escaped) { + escaped = PARSON_TRUE; + string++; + continue; + } + else if (current_char == '\"' && !escaped) { + in_string = !in_string; + } + else if (!in_string && + strncmp(string, start_token, start_token_len) == 0) { + for (i = 0; i < start_token_len; i++) { + string[i] = ' '; + } + string = string + start_token_len; + ptr = strstr(string, end_token); + if (!ptr) { + return; + } + for (i = 0; i < (ptr - string) + end_token_len; i++) { + string[i] = ' '; + } + string = ptr + end_token_len - 1; + } + escaped = PARSON_FALSE; + string++; + } +} + +static char *parson_strndup(const char *string, size_t n) +{ + /* We expect the caller has validated that 'n' fits within the input buffer. + */ + char *output_string = (char *)parson_malloc(n + 1); + if (!output_string) { + return NULL; + } + output_string[n] = '\0'; + memcpy(output_string, string, n); + return output_string; +} + +static char *parson_strdup(const char *string) +{ + return parson_strndup(string, strlen(string)); +} + +static int parson_sprintf(char *s, const char *format, ...) +{ + int result; + va_list args; + va_start(args, format); + +#if defined(__APPLE__) && defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" +#endif + result = vsprintf(s, format, args); +#if defined(__APPLE__) && defined(__clang__) +#pragma clang diagnostic pop +#endif + + va_end(args); + return result; +} + +static int hex_char_to_int(char c) +{ + if (c >= '0' && c <= '9') { + return c - '0'; + } + else if (c >= 'a' && c <= 'f') { + return c - 'a' + 10; + } + else if (c >= 'A' && c <= 'F') { + return c - 'A' + 10; + } + return -1; +} + +static JSON_Status parse_utf16_hex(const char *s, unsigned int *result) +{ + int x1, x2, x3, x4; + if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0' || s[3] == '\0') { + return JSONFailure; + } + x1 = hex_char_to_int(s[0]); + x2 = hex_char_to_int(s[1]); + x3 = hex_char_to_int(s[2]); + x4 = hex_char_to_int(s[3]); + if (x1 == -1 || x2 == -1 || x3 == -1 || x4 == -1) { + return JSONFailure; + } + *result = (unsigned int)((x1 << 12) | (x2 << 8) | (x3 << 4) | x4); + return JSONSuccess; +} + +static int num_bytes_in_utf8_sequence(unsigned char c) +{ + if (c == 0xC0 || c == 0xC1 || c > 0xF4 || IS_CONT(c)) { + return 0; + } + else if ((c & 0x80) == 0) { /* 0xxxxxxx */ + return 1; + } + else if ((c & 0xE0) == 0xC0) { /* 110xxxxx */ + return 2; + } + else if ((c & 0xF0) == 0xE0) { /* 1110xxxx */ + return 3; + } + else if ((c & 0xF8) == 0xF0) { /* 11110xxx */ + return 4; + } + return 0; /* won't happen */ +} + +static JSON_Status verify_utf8_sequence(const unsigned char *string, int *len) +{ + unsigned int cp = 0; + *len = num_bytes_in_utf8_sequence(string[0]); + + if (*len == 1) { + cp = string[0]; + } + else if (*len == 2 && IS_CONT(string[1])) { + cp = string[0] & 0x1F; + cp = (cp << 6) | (string[1] & 0x3F); + } + else if (*len == 3 && IS_CONT(string[1]) && IS_CONT(string[2])) { + cp = ((unsigned char)string[0]) & 0xF; + cp = (cp << 6) | (string[1] & 0x3F); + cp = (cp << 6) | (string[2] & 0x3F); + } + else if (*len == 4 && IS_CONT(string[1]) && IS_CONT(string[2]) && + IS_CONT(string[3])) { + cp = string[0] & 0x7; + cp = (cp << 6) | (string[1] & 0x3F); + cp = (cp << 6) | (string[2] & 0x3F); + cp = (cp << 6) | (string[3] & 0x3F); + } + else { + return JSONFailure; + } + + /* overlong encodings */ + if ((cp < 0x80 && *len > 1) || (cp < 0x800 && *len > 2) || + (cp < 0x10000 && *len > 3)) { + return JSONFailure; + } + + /* invalid unicode */ + if (cp > 0x10FFFF) { + return JSONFailure; + } + + /* surrogate halves */ + if (cp >= 0xD800 && cp <= 0xDFFF) { + return JSONFailure; + } + + return JSONSuccess; +} + +static int is_valid_utf8(const char *string, size_t string_len) +{ + int len = 0; + const char *string_end = string + string_len; + while (string < string_end) { + if (verify_utf8_sequence((const unsigned char *)string, &len) != + JSONSuccess) { + return PARSON_FALSE; + } + string += len; + } + return PARSON_TRUE; +} + +static parson_bool_t is_decimal(const char *string, size_t length) +{ + if (length > 1 && string[0] == '0' && string[1] != '.') { + return PARSON_FALSE; + } + if (length > 2 && !strncmp(string, "-0", 2) && string[2] != '.') { + return PARSON_FALSE; + } + while (length--) { + if (strchr("xX", string[length])) { + return PARSON_FALSE; + } + } + return PARSON_TRUE; +} + +static unsigned long hash_string(const char *string, size_t n) +{ +#ifdef PARSON_FORCE_HASH_COLLISIONS + (void)string; + (void)n; + return 0; +#else + unsigned long hash = 5381; + unsigned char c; + size_t i = 0; + for (i = 0; i < n; i++) { + c = string[i]; + if (c == '\0') { + break; + } + hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ + } + return hash; +#endif +} + +/* JSON Object */ +static JSON_Object *json_object_make(JSON_Value *wrapping_value) +{ + JSON_Status res = JSONFailure; + JSON_Object *new_obj = (JSON_Object *)parson_malloc(sizeof(JSON_Object)); + if (new_obj == NULL) { + return NULL; + } + new_obj->wrapping_value = wrapping_value; + res = json_object_init(new_obj, 0); + if (res != JSONSuccess) { + parson_free(new_obj); + return NULL; + } + return new_obj; +} + +static JSON_Status json_object_init(JSON_Object *object, size_t capacity) +{ + unsigned int i = 0; + + object->cells = NULL; + object->names = NULL; + object->values = NULL; + object->cell_ixs = NULL; + object->hashes = NULL; + + object->count = 0; + object->cell_capacity = capacity; + object->item_capacity = (unsigned int)(capacity * 7 / 10); + + if (capacity == 0) { + return JSONSuccess; + } + + object->cells = + (size_t *)parson_malloc(object->cell_capacity * sizeof(*object->cells)); + object->names = + (char **)parson_malloc(object->item_capacity * sizeof(*object->names)); + object->values = (JSON_Value **)parson_malloc(object->item_capacity * + sizeof(*object->values)); + object->cell_ixs = (size_t *)parson_malloc(object->item_capacity * + sizeof(*object->cell_ixs)); + object->hashes = (unsigned long *)parson_malloc(object->item_capacity * + sizeof(*object->hashes)); + if (object->cells == NULL || object->names == NULL || + object->values == NULL || object->cell_ixs == NULL || + object->hashes == NULL) { + goto error; + } + for (i = 0; i < object->cell_capacity; i++) { + object->cells[i] = OBJECT_INVALID_IX; + } + return JSONSuccess; +error: + parson_free(object->cells); + parson_free(object->names); + parson_free(object->values); + parson_free(object->cell_ixs); + parson_free(object->hashes); + return JSONFailure; +} + +static void json_object_deinit(JSON_Object *object, parson_bool_t free_keys, + parson_bool_t free_values) +{ + unsigned int i = 0; + for (i = 0; i < object->count; i++) { + if (free_keys) { + parson_free(object->names[i]); + } + if (free_values) { + json_value_free(object->values[i]); + } + } + + object->count = 0; + object->item_capacity = 0; + object->cell_capacity = 0; + + parson_free(object->cells); + parson_free(object->names); + parson_free(object->values); + parson_free(object->cell_ixs); + parson_free(object->hashes); + + object->cells = NULL; + object->names = NULL; + object->values = NULL; + object->cell_ixs = NULL; + object->hashes = NULL; +} + +static JSON_Status json_object_grow_and_rehash(JSON_Object *object) +{ + JSON_Value *wrapping_value = NULL; + JSON_Object new_object; + char *key = NULL; + JSON_Value *value = NULL; + unsigned int i = 0; + size_t new_capacity = MAX(object->cell_capacity * 2, STARTING_CAPACITY); + JSON_Status res = json_object_init(&new_object, new_capacity); + if (res != JSONSuccess) { + return JSONFailure; + } + + wrapping_value = json_object_get_wrapping_value(object); + new_object.wrapping_value = wrapping_value; + + for (i = 0; i < object->count; i++) { + key = object->names[i]; + value = object->values[i]; + res = json_object_add(&new_object, key, value); + if (res != JSONSuccess) { + json_object_deinit(&new_object, PARSON_FALSE, PARSON_FALSE); + return JSONFailure; + } + value->parent = wrapping_value; + } + json_object_deinit(object, PARSON_FALSE, PARSON_FALSE); + *object = new_object; + return JSONSuccess; +} + +static size_t json_object_get_cell_ix(const JSON_Object *object, + const char *key, size_t key_len, + unsigned long hash, + parson_bool_t *out_found) +{ + size_t cell_ix = hash & (object->cell_capacity - 1); + size_t cell = 0; + size_t ix = 0; + unsigned int i = 0; + unsigned long hash_to_check = 0; + const char *key_to_check = NULL; + size_t key_to_check_len = 0; + + *out_found = PARSON_FALSE; + + for (i = 0; i < object->cell_capacity; i++) { + ix = (cell_ix + i) & (object->cell_capacity - 1); + cell = object->cells[ix]; + if (cell == OBJECT_INVALID_IX) { + return ix; + } + hash_to_check = object->hashes[cell]; + if (hash != hash_to_check) { + continue; + } + key_to_check = object->names[cell]; + key_to_check_len = strlen(key_to_check); + if (key_to_check_len == key_len && + strncmp(key, key_to_check, key_len) == 0) { + *out_found = PARSON_TRUE; + return ix; + } + } + return OBJECT_INVALID_IX; +} + +static JSON_Status json_object_add(JSON_Object *object, char *name, + JSON_Value *value) +{ + unsigned long hash = 0; + parson_bool_t found = PARSON_FALSE; + size_t cell_ix = 0; + JSON_Status res = JSONFailure; + + if (!object || !name || !value) { + return JSONFailure; + } + + hash = hash_string(name, strlen(name)); + found = PARSON_FALSE; + cell_ix = json_object_get_cell_ix(object, name, strlen(name), hash, &found); + if (found) { + return JSONFailure; + } + + if (object->count >= object->item_capacity) { + res = json_object_grow_and_rehash(object); + if (res != JSONSuccess) { + return JSONFailure; + } + cell_ix = + json_object_get_cell_ix(object, name, strlen(name), hash, &found); + } + + object->names[object->count] = name; + object->cells[cell_ix] = object->count; + object->values[object->count] = value; + object->cell_ixs[object->count] = cell_ix; + object->hashes[object->count] = hash; + object->count++; + value->parent = json_object_get_wrapping_value(object); + + return JSONSuccess; +} + +static JSON_Value *json_object_getn_value(const JSON_Object *object, + const char *name, size_t name_len) +{ + unsigned long hash = 0; + parson_bool_t found = PARSON_FALSE; + size_t cell_ix = 0; + size_t item_ix = 0; + if (!object || !name) { + return NULL; + } + hash = hash_string(name, name_len); + found = PARSON_FALSE; + cell_ix = json_object_get_cell_ix(object, name, name_len, hash, &found); + if (!found) { + return NULL; + } + item_ix = object->cells[cell_ix]; + return object->values[item_ix]; +} + +static JSON_Status json_object_remove_internal(JSON_Object *object, + const char *name, + parson_bool_t free_value) +{ + unsigned long hash = 0; + parson_bool_t found = PARSON_FALSE; + size_t cell = 0; + size_t item_ix = 0; + size_t last_item_ix = 0; + size_t i = 0; + size_t j = 0; + size_t x = 0; + size_t k = 0; + JSON_Value *val = NULL; + + if (object == NULL) { + return JSONFailure; + } + + hash = hash_string(name, strlen(name)); + found = PARSON_FALSE; + cell = json_object_get_cell_ix(object, name, strlen(name), hash, &found); + if (!found) { + return JSONFailure; + } + + item_ix = object->cells[cell]; + if (free_value) { + val = object->values[item_ix]; + json_value_free(val); + val = NULL; + } + + parson_free(object->names[item_ix]); + last_item_ix = object->count - 1; + if (item_ix < last_item_ix) { + object->names[item_ix] = object->names[last_item_ix]; + object->values[item_ix] = object->values[last_item_ix]; + object->cell_ixs[item_ix] = object->cell_ixs[last_item_ix]; + object->hashes[item_ix] = object->hashes[last_item_ix]; + object->cells[object->cell_ixs[item_ix]] = item_ix; + } + object->count--; + + i = cell; + j = i; + for (x = 0; x < (object->cell_capacity - 1); x++) { + j = (j + 1) & (object->cell_capacity - 1); + if (object->cells[j] == OBJECT_INVALID_IX) { + break; + } + k = object->hashes[object->cells[j]] & (object->cell_capacity - 1); + if ((j > i && (k <= i || k > j)) || (j < i && (k <= i && k > j))) { + object->cell_ixs[object->cells[j]] = i; + object->cells[i] = object->cells[j]; + i = j; + } + } + object->cells[i] = OBJECT_INVALID_IX; + return JSONSuccess; +} + +static JSON_Status json_object_dotremove_internal(JSON_Object *object, + const char *name, + parson_bool_t free_value) +{ + JSON_Value *temp_value = NULL; + JSON_Object *temp_object = NULL; + const char *dot_pos = strchr(name, '.'); + if (!dot_pos) { + return json_object_remove_internal(object, name, free_value); + } + temp_value = json_object_getn_value(object, name, dot_pos - name); + if (json_value_get_type(temp_value) != JSONObject) { + return JSONFailure; + } + temp_object = json_value_get_object(temp_value); + return json_object_dotremove_internal(temp_object, dot_pos + 1, free_value); +} + +static void json_object_free(JSON_Object *object) +{ + json_object_deinit(object, PARSON_TRUE, PARSON_TRUE); + parson_free(object); +} + +/* JSON Array */ +static JSON_Array *json_array_make(JSON_Value *wrapping_value) +{ + JSON_Array *new_array = (JSON_Array *)parson_malloc(sizeof(JSON_Array)); + if (new_array == NULL) { + return NULL; + } + new_array->wrapping_value = wrapping_value; + new_array->items = (JSON_Value **)NULL; + new_array->capacity = 0; + new_array->count = 0; + return new_array; +} + +static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value) +{ + if (array->count >= array->capacity) { + size_t new_capacity = MAX(array->capacity * 2, STARTING_CAPACITY); + if (json_array_resize(array, new_capacity) != JSONSuccess) { + return JSONFailure; + } + } + value->parent = json_array_get_wrapping_value(array); + array->items[array->count] = value; + array->count++; + return JSONSuccess; +} + +static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity) +{ + JSON_Value **new_items = NULL; + if (new_capacity == 0) { + return JSONFailure; + } + new_items = + (JSON_Value **)parson_malloc(new_capacity * sizeof(JSON_Value *)); + if (new_items == NULL) { + return JSONFailure; + } + if (array->items != NULL && array->count > 0) { + memcpy(new_items, array->items, array->count * sizeof(JSON_Value *)); + } + parson_free(array->items); + array->items = new_items; + array->capacity = new_capacity; + return JSONSuccess; +} + +static void json_array_free(JSON_Array *array) +{ + size_t i; + for (i = 0; i < array->count; i++) { + json_value_free(array->items[i]); + } + parson_free(array->items); + parson_free(array); +} + +/* JSON Value */ +static JSON_Value *json_value_init_string_no_copy(char *string, size_t length) +{ + JSON_Value *new_value = (JSON_Value *)parson_malloc(sizeof(JSON_Value)); + if (!new_value) { + return NULL; + } + new_value->parent = NULL; + new_value->type = JSONString; + new_value->value.string.chars = string; + new_value->value.string.length = length; + return new_value; +} + +/* Parser */ +static JSON_Status skip_quotes(const char **string) +{ + if (**string != '\"') { + return JSONFailure; + } + SKIP_CHAR(string); + while (**string != '\"') { + if (**string == '\0') { + return JSONFailure; + } + else if (**string == '\\') { + SKIP_CHAR(string); + if (**string == '\0') { + return JSONFailure; + } + } + SKIP_CHAR(string); + } + SKIP_CHAR(string); + return JSONSuccess; +} + +static JSON_Status parse_utf16(const char **unprocessed, char **processed) +{ + unsigned int cp, lead, trail; + char *processed_ptr = *processed; + const char *unprocessed_ptr = *unprocessed; + JSON_Status status = JSONFailure; + unprocessed_ptr++; /* skips u */ + status = parse_utf16_hex(unprocessed_ptr, &cp); + if (status != JSONSuccess) { + return JSONFailure; + } + if (cp < 0x80) { + processed_ptr[0] = (char)cp; /* 0xxxxxxx */ + } + else if (cp < 0x800) { + processed_ptr[0] = ((cp >> 6) & 0x1F) | 0xC0; /* 110xxxxx */ + processed_ptr[1] = ((cp)&0x3F) | 0x80; /* 10xxxxxx */ + processed_ptr += 1; + } + else if (cp < 0xD800 || cp > 0xDFFF) { + processed_ptr[0] = ((cp >> 12) & 0x0F) | 0xE0; /* 1110xxxx */ + processed_ptr[1] = ((cp >> 6) & 0x3F) | 0x80; /* 10xxxxxx */ + processed_ptr[2] = ((cp)&0x3F) | 0x80; /* 10xxxxxx */ + processed_ptr += 2; + } + else if (cp >= 0xD800 && + cp <= 0xDBFF) { /* lead surrogate (0xD800..0xDBFF) */ + lead = cp; + unprocessed_ptr += 4; /* should always be within the buffer, otherwise + previous sscanf would fail */ + if (*unprocessed_ptr++ != '\\' || *unprocessed_ptr++ != 'u') { + return JSONFailure; + } + status = parse_utf16_hex(unprocessed_ptr, &trail); + if (status != JSONSuccess || trail < 0xDC00 || + trail > 0xDFFF) { /* valid trail surrogate? (0xDC00..0xDFFF) */ + return JSONFailure; + } + cp = ((((lead - 0xD800) & 0x3FF) << 10) | ((trail - 0xDC00) & 0x3FF)) + + 0x010000; + processed_ptr[0] = (((cp >> 18) & 0x07) | 0xF0); /* 11110xxx */ + processed_ptr[1] = (((cp >> 12) & 0x3F) | 0x80); /* 10xxxxxx */ + processed_ptr[2] = (((cp >> 6) & 0x3F) | 0x80); /* 10xxxxxx */ + processed_ptr[3] = (((cp)&0x3F) | 0x80); /* 10xxxxxx */ + processed_ptr += 3; + } + else { /* trail surrogate before lead surrogate */ + return JSONFailure; + } + unprocessed_ptr += 3; + *processed = processed_ptr; + *unprocessed = unprocessed_ptr; + return JSONSuccess; +} + +/* Copies and processes passed string up to supplied length. +Example: "\u006Corem ipsum" -> lorem ipsum */ +static char *process_string(const char *input, size_t input_len, + size_t *output_len) +{ + const char *input_ptr = input; + size_t initial_size = (input_len + 1) * sizeof(char); + size_t final_size = 0; + char *output = NULL, *output_ptr = NULL, *resized_output = NULL; + output = (char *)parson_malloc(initial_size); + if (output == NULL) { + goto error; + } + output_ptr = output; + while ((*input_ptr != '\0') && (size_t)(input_ptr - input) < input_len) { + if (*input_ptr == '\\') { + input_ptr++; + switch (*input_ptr) { + case '\"': + *output_ptr = '\"'; + break; + case '\\': + *output_ptr = '\\'; + break; + case '/': + *output_ptr = '/'; + break; + case 'b': + *output_ptr = '\b'; + break; + case 'f': + *output_ptr = '\f'; + break; + case 'n': + *output_ptr = '\n'; + break; + case 'r': + *output_ptr = '\r'; + break; + case 't': + *output_ptr = '\t'; + break; + case 'u': + if (parse_utf16(&input_ptr, &output_ptr) != JSONSuccess) { + goto error; + } + break; + default: + goto error; + } + } + else if ((unsigned char)*input_ptr < 0x20) { + goto error; /* 0x00-0x19 are invalid characters for json string + (http://www.ietf.org/rfc/rfc4627.txt) */ + } + else { + *output_ptr = *input_ptr; + } + output_ptr++; + input_ptr++; + } + *output_ptr = '\0'; + /* resize to new length */ + final_size = (size_t)(output_ptr - output) + 1; + /* todo: don't resize if final_size == initial_size */ + resized_output = (char *)parson_malloc(final_size); + if (resized_output == NULL) { + goto error; + } + memcpy(resized_output, output, final_size); + *output_len = final_size - 1; + parson_free(output); + return resized_output; +error: + parson_free(output); + return NULL; +} + +/* Return processed contents of a string between quotes and + skips passed argument to a matching quote. */ +static char *get_quoted_string(const char **string, size_t *output_string_len) +{ + const char *string_start = *string; + size_t input_string_len = 0; + JSON_Status status = skip_quotes(string); + if (status != JSONSuccess) { + return NULL; + } + input_string_len = *string - string_start - 2; /* length without quotes */ + return process_string(string_start + 1, input_string_len, + output_string_len); +} + +static JSON_Value *parse_value(const char **string, size_t nesting) +{ + if (nesting > MAX_NESTING) { + return NULL; + } + SKIP_WHITESPACES(string); + switch (**string) { + case '{': + return parse_object_value(string, nesting + 1); + case '[': + return parse_array_value(string, nesting + 1); + case '\"': + return parse_string_value(string); + case 'f': + case 't': + return parse_boolean_value(string); + case '-': + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + return parse_number_value(string); + case 'n': + return parse_null_value(string); + default: + return NULL; + } +} + +static JSON_Value *parse_object_value(const char **string, size_t nesting) +{ + JSON_Status status = JSONFailure; + JSON_Value *output_value = NULL, *new_value = NULL; + JSON_Object *output_object = NULL; + char *new_key = NULL; + + output_value = json_value_init_object(); + if (output_value == NULL) { + return NULL; + } + if (**string != '{') { + json_value_free(output_value); + return NULL; + } + output_object = json_value_get_object(output_value); + SKIP_CHAR(string); + SKIP_WHITESPACES(string); + if (**string == '}') { /* empty object */ + SKIP_CHAR(string); + return output_value; + } + while (**string != '\0') { + size_t key_len = 0; + new_key = get_quoted_string(string, &key_len); + /* We do not support key names with embedded \0 chars */ + if (!new_key) { + json_value_free(output_value); + return NULL; + } + if (key_len != strlen(new_key)) { + parson_free(new_key); + json_value_free(output_value); + return NULL; + } + SKIP_WHITESPACES(string); + if (**string != ':') { + parson_free(new_key); + json_value_free(output_value); + return NULL; + } + SKIP_CHAR(string); + new_value = parse_value(string, nesting); + if (new_value == NULL) { + parson_free(new_key); + json_value_free(output_value); + return NULL; + } + status = json_object_add(output_object, new_key, new_value); + if (status != JSONSuccess) { + parson_free(new_key); + json_value_free(new_value); + json_value_free(output_value); + return NULL; + } + SKIP_WHITESPACES(string); + if (**string != ',') { + break; + } + SKIP_CHAR(string); + SKIP_WHITESPACES(string); + if (**string == '}') { + break; + } + } + SKIP_WHITESPACES(string); + if (**string != '}') { + json_value_free(output_value); + return NULL; + } + SKIP_CHAR(string); + return output_value; +} + +static JSON_Value *parse_array_value(const char **string, size_t nesting) +{ + JSON_Value *output_value = NULL, *new_array_value = NULL; + JSON_Array *output_array = NULL; + output_value = json_value_init_array(); + if (output_value == NULL) { + return NULL; + } + if (**string != '[') { + json_value_free(output_value); + return NULL; + } + output_array = json_value_get_array(output_value); + SKIP_CHAR(string); + SKIP_WHITESPACES(string); + if (**string == ']') { /* empty array */ + SKIP_CHAR(string); + return output_value; + } + while (**string != '\0') { + new_array_value = parse_value(string, nesting); + if (new_array_value == NULL) { + json_value_free(output_value); + return NULL; + } + if (json_array_add(output_array, new_array_value) != JSONSuccess) { + json_value_free(new_array_value); + json_value_free(output_value); + return NULL; + } + SKIP_WHITESPACES(string); + if (**string != ',') { + break; + } + SKIP_CHAR(string); + SKIP_WHITESPACES(string); + if (**string == ']') { + break; + } + } + SKIP_WHITESPACES(string); + if (**string != ']' || /* Trim array after parsing is over */ + json_array_resize(output_array, json_array_get_count(output_array)) != + JSONSuccess) { + json_value_free(output_value); + return NULL; + } + SKIP_CHAR(string); + return output_value; +} + +static JSON_Value *parse_string_value(const char **string) +{ + JSON_Value *value = NULL; + size_t new_string_len = 0; + char *new_string = get_quoted_string(string, &new_string_len); + if (new_string == NULL) { + return NULL; + } + value = json_value_init_string_no_copy(new_string, new_string_len); + if (value == NULL) { + parson_free(new_string); + return NULL; + } + return value; +} + +static JSON_Value *parse_boolean_value(const char **string) +{ + size_t true_token_size = SIZEOF_TOKEN("true"); + size_t false_token_size = SIZEOF_TOKEN("false"); + if (strncmp("true", *string, true_token_size) == 0) { + *string += true_token_size; + return json_value_init_boolean(1); + } + else if (strncmp("false", *string, false_token_size) == 0) { + *string += false_token_size; + return json_value_init_boolean(0); + } + return NULL; +} + +static JSON_Value *parse_number_value(const char **string) +{ + char *end; + double number = 0; + errno = 0; + number = strtod(*string, &end); + if (errno == ERANGE && (number <= -HUGE_VAL || number >= HUGE_VAL)) { + return NULL; + } + if ((errno && errno != ERANGE) || !is_decimal(*string, end - *string)) { + return NULL; + } + *string = end; + return json_value_init_number(number); +} + +static JSON_Value *parse_null_value(const char **string) +{ + size_t token_size = SIZEOF_TOKEN("null"); + if (strncmp("null", *string, token_size) == 0) { + *string += token_size; + return json_value_init_null(); + } + return NULL; +} + +/* Serialization */ + +/* APPEND_STRING() is only called on string literals. + It's a bit hacky because it makes plenty of assumptions about the external + state and should eventually be tidied up into a function (same goes for + APPEND_INDENT) + */ +#define APPEND_STRING(str) \ + do { \ + written = SIZEOF_TOKEN((str)); \ + if (buf != NULL) { \ + memcpy(buf, (str), written); \ + buf[written] = '\0'; \ + buf += written; \ + } \ + written_total += written; \ + } while (0) + +#define APPEND_INDENT(level) \ + do { \ + int level_i = 0; \ + for (level_i = 0; level_i < (level); level_i++) { \ + APPEND_STRING(PARSON_INDENT_STR); \ + } \ + } while (0) + +static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, + int level, parson_bool_t is_pretty, + char *num_buf) +{ + const char *key = NULL, *string = NULL; + JSON_Value *temp_value = NULL; + JSON_Array *array = NULL; + JSON_Object *object = NULL; + size_t i = 0, count = 0; + double num = 0.0; + int written = -1, written_total = 0; + size_t len = 0; + + switch (json_value_get_type(value)) { + case JSONArray: + array = json_value_get_array(value); + count = json_array_get_count(array); + APPEND_STRING("["); + if (count > 0 && is_pretty) { + APPEND_STRING("\n"); + } + for (i = 0; i < count; i++) { + if (is_pretty) { + APPEND_INDENT(level + 1); + } + temp_value = json_array_get_value(array, i); + written = json_serialize_to_buffer_r(temp_value, buf, level + 1, + is_pretty, num_buf); + if (written < 0) { + return -1; + } + if (buf != NULL) { + buf += written; + } + written_total += written; + if (i < (count - 1)) { + APPEND_STRING(","); + } + if (is_pretty) { + APPEND_STRING("\n"); + } + } + if (count > 0 && is_pretty) { + APPEND_INDENT(level); + } + APPEND_STRING("]"); + return written_total; + case JSONObject: + object = json_value_get_object(value); + count = json_object_get_count(object); + APPEND_STRING("{"); + if (count > 0 && is_pretty) { + APPEND_STRING("\n"); + } + for (i = 0; i < count; i++) { + key = json_object_get_name(object, i); + if (key == NULL) { + return -1; + } + if (is_pretty) { + APPEND_INDENT(level + 1); + } + /* We do not support key names with embedded \0 chars */ + written = json_serialize_string(key, strlen(key), buf); + if (written < 0) { + return -1; + } + if (buf != NULL) { + buf += written; + } + written_total += written; + APPEND_STRING(":"); + if (is_pretty) { + APPEND_STRING(" "); + } + temp_value = json_object_get_value_at(object, i); + written = json_serialize_to_buffer_r(temp_value, buf, level + 1, + is_pretty, num_buf); + if (written < 0) { + return -1; + } + if (buf != NULL) { + buf += written; + } + written_total += written; + if (i < (count - 1)) { + APPEND_STRING(","); + } + if (is_pretty) { + APPEND_STRING("\n"); + } + } + if (count > 0 && is_pretty) { + APPEND_INDENT(level); + } + APPEND_STRING("}"); + return written_total; + case JSONString: + string = json_value_get_string(value); + if (string == NULL) { + return -1; + } + len = json_value_get_string_len(value); + written = json_serialize_string(string, len, buf); + if (written < 0) { + return -1; + } + if (buf != NULL) { + buf += written; + } + written_total += written; + return written_total; + case JSONBoolean: + if (json_value_get_boolean(value)) { + APPEND_STRING("true"); + } + else { + APPEND_STRING("false"); + } + return written_total; + case JSONNumber: + num = json_value_get_number(value); + if (buf != NULL) { + num_buf = buf; + } + if (parson_number_serialization_function) { + written = parson_number_serialization_function(num, num_buf); + } + else { + const char *float_format = parson_float_format + ? parson_float_format + : PARSON_DEFAULT_FLOAT_FORMAT; + written = parson_sprintf(num_buf, float_format, num); + } + if (written < 0) { + return -1; + } + if (buf != NULL) { + buf += written; + } + written_total += written; + return written_total; + case JSONNull: + APPEND_STRING("null"); + return written_total; + case JSONError: + return -1; + default: + return -1; + } +} + +static int json_serialize_string(const char *string, size_t len, char *buf) +{ + size_t i = 0; + char c = '\0'; + int written = -1, written_total = 0; + APPEND_STRING("\""); + for (i = 0; i < len; i++) { + c = string[i]; + switch (c) { + case '\"': + APPEND_STRING("\\\""); + break; + case '\\': + APPEND_STRING("\\\\"); + break; + case '\b': + APPEND_STRING("\\b"); + break; + case '\f': + APPEND_STRING("\\f"); + break; + case '\n': + APPEND_STRING("\\n"); + break; + case '\r': + APPEND_STRING("\\r"); + break; + case '\t': + APPEND_STRING("\\t"); + break; + case '\x00': + APPEND_STRING("\\u0000"); + break; + case '\x01': + APPEND_STRING("\\u0001"); + break; + case '\x02': + APPEND_STRING("\\u0002"); + break; + case '\x03': + APPEND_STRING("\\u0003"); + break; + case '\x04': + APPEND_STRING("\\u0004"); + break; + case '\x05': + APPEND_STRING("\\u0005"); + break; + case '\x06': + APPEND_STRING("\\u0006"); + break; + case '\x07': + APPEND_STRING("\\u0007"); + break; + /* '\x08' duplicate: '\b' */ + /* '\x09' duplicate: '\t' */ + /* '\x0a' duplicate: '\n' */ + case '\x0b': + APPEND_STRING("\\u000b"); + break; + /* '\x0c' duplicate: '\f' */ + /* '\x0d' duplicate: '\r' */ + case '\x0e': + APPEND_STRING("\\u000e"); + break; + case '\x0f': + APPEND_STRING("\\u000f"); + break; + case '\x10': + APPEND_STRING("\\u0010"); + break; + case '\x11': + APPEND_STRING("\\u0011"); + break; + case '\x12': + APPEND_STRING("\\u0012"); + break; + case '\x13': + APPEND_STRING("\\u0013"); + break; + case '\x14': + APPEND_STRING("\\u0014"); + break; + case '\x15': + APPEND_STRING("\\u0015"); + break; + case '\x16': + APPEND_STRING("\\u0016"); + break; + case '\x17': + APPEND_STRING("\\u0017"); + break; + case '\x18': + APPEND_STRING("\\u0018"); + break; + case '\x19': + APPEND_STRING("\\u0019"); + break; + case '\x1a': + APPEND_STRING("\\u001a"); + break; + case '\x1b': + APPEND_STRING("\\u001b"); + break; + case '\x1c': + APPEND_STRING("\\u001c"); + break; + case '\x1d': + APPEND_STRING("\\u001d"); + break; + case '\x1e': + APPEND_STRING("\\u001e"); + break; + case '\x1f': + APPEND_STRING("\\u001f"); + break; + case '/': + if (parson_escape_slashes) { + APPEND_STRING("\\/"); /* to make json embeddable in xml\/html */ + } + else { + APPEND_STRING("/"); + } + break; + default: + if (buf != NULL) { + buf[0] = c; + buf += 1; + } + written_total += 1; + break; + } + } + APPEND_STRING("\""); + return written_total; +} + +#undef APPEND_STRING +#undef APPEND_INDENT + +/* Parser API */ +JSON_Value *json_parse_file(const char *filename) +{ + char *file_contents = read_file(filename); + JSON_Value *output_value = NULL; + if (file_contents == NULL) { + return NULL; + } + output_value = json_parse_string(file_contents); + parson_free(file_contents); + return output_value; +} + +JSON_Value *json_parse_file_with_comments(const char *filename) +{ + char *file_contents = read_file(filename); + JSON_Value *output_value = NULL; + if (file_contents == NULL) { + return NULL; + } + output_value = json_parse_string_with_comments(file_contents); + parson_free(file_contents); + return output_value; +} + +JSON_Value *json_parse_string(const char *string) +{ + if (string == NULL) { + return NULL; + } + if (string[0] == '\xEF' && string[1] == '\xBB' && string[2] == '\xBF') { + string = string + 3; /* Support for UTF-8 BOM */ + } + return parse_value((const char **)&string, 0); +} + +JSON_Value *json_parse_string_with_comments(const char *string) +{ + JSON_Value *result = NULL; + char *string_mutable_copy = NULL, *string_mutable_copy_ptr = NULL; + string_mutable_copy = parson_strdup(string); + if (string_mutable_copy == NULL) { + return NULL; + } + remove_comments(string_mutable_copy, "/*", "*/"); + remove_comments(string_mutable_copy, "//", "\n"); + string_mutable_copy_ptr = string_mutable_copy; + result = parse_value((const char **)&string_mutable_copy_ptr, 0); + parson_free(string_mutable_copy); + return result; +} + +/* JSON Object API */ + +JSON_Value *json_object_get_value(const JSON_Object *object, const char *name) +{ + if (object == NULL || name == NULL) { + return NULL; + } + return json_object_getn_value(object, name, strlen(name)); +} + +const char *json_object_get_string(const JSON_Object *object, const char *name) +{ + return json_value_get_string(json_object_get_value(object, name)); +} + +size_t json_object_get_string_len(const JSON_Object *object, const char *name) +{ + return json_value_get_string_len(json_object_get_value(object, name)); +} + +double json_object_get_number(const JSON_Object *object, const char *name) +{ + return json_value_get_number(json_object_get_value(object, name)); +} + +JSON_Object *json_object_get_object(const JSON_Object *object, const char *name) +{ + return json_value_get_object(json_object_get_value(object, name)); +} + +JSON_Array *json_object_get_array(const JSON_Object *object, const char *name) +{ + return json_value_get_array(json_object_get_value(object, name)); +} + +int json_object_get_boolean(const JSON_Object *object, const char *name) +{ + return json_value_get_boolean(json_object_get_value(object, name)); +} + +JSON_Value *json_object_dotget_value(const JSON_Object *object, + const char *name) +{ + const char *dot_position = strchr(name, '.'); + if (!dot_position) { + return json_object_get_value(object, name); + } + object = json_value_get_object( + json_object_getn_value(object, name, dot_position - name)); + return json_object_dotget_value(object, dot_position + 1); +} + +const char *json_object_dotget_string(const JSON_Object *object, + const char *name) +{ + return json_value_get_string(json_object_dotget_value(object, name)); +} + +size_t json_object_dotget_string_len(const JSON_Object *object, + const char *name) +{ + return json_value_get_string_len(json_object_dotget_value(object, name)); +} + +double json_object_dotget_number(const JSON_Object *object, const char *name) +{ + return json_value_get_number(json_object_dotget_value(object, name)); +} + +JSON_Object *json_object_dotget_object(const JSON_Object *object, + const char *name) +{ + return json_value_get_object(json_object_dotget_value(object, name)); +} + +JSON_Array *json_object_dotget_array(const JSON_Object *object, + const char *name) +{ + return json_value_get_array(json_object_dotget_value(object, name)); +} + +int json_object_dotget_boolean(const JSON_Object *object, const char *name) +{ + return json_value_get_boolean(json_object_dotget_value(object, name)); +} + +size_t json_object_get_count(const JSON_Object *object) +{ + return object ? object->count : 0; +} + +const char *json_object_get_name(const JSON_Object *object, size_t index) +{ + if (object == NULL || index >= json_object_get_count(object)) { + return NULL; + } + return object->names[index]; +} + +JSON_Value *json_object_get_value_at(const JSON_Object *object, size_t index) +{ + if (object == NULL || index >= json_object_get_count(object)) { + return NULL; + } + return object->values[index]; +} + +JSON_Value *json_object_get_wrapping_value(const JSON_Object *object) +{ + if (!object) { + return NULL; + } + return object->wrapping_value; +} + +int json_object_has_value(const JSON_Object *object, const char *name) +{ + return json_object_get_value(object, name) != NULL; +} + +int json_object_has_value_of_type(const JSON_Object *object, const char *name, + JSON_Value_Type type) +{ + JSON_Value *val = json_object_get_value(object, name); + return val != NULL && json_value_get_type(val) == type; +} + +int json_object_dothas_value(const JSON_Object *object, const char *name) +{ + return json_object_dotget_value(object, name) != NULL; +} + +int json_object_dothas_value_of_type(const JSON_Object *object, + const char *name, JSON_Value_Type type) +{ + JSON_Value *val = json_object_dotget_value(object, name); + return val != NULL && json_value_get_type(val) == type; +} + +/* JSON Array API */ +JSON_Value *json_array_get_value(const JSON_Array *array, size_t index) +{ + if (array == NULL || index >= json_array_get_count(array)) { + return NULL; + } + return array->items[index]; +} + +const char *json_array_get_string(const JSON_Array *array, size_t index) +{ + return json_value_get_string(json_array_get_value(array, index)); +} + +size_t json_array_get_string_len(const JSON_Array *array, size_t index) +{ + return json_value_get_string_len(json_array_get_value(array, index)); +} + +double json_array_get_number(const JSON_Array *array, size_t index) +{ + return json_value_get_number(json_array_get_value(array, index)); +} + +JSON_Object *json_array_get_object(const JSON_Array *array, size_t index) +{ + return json_value_get_object(json_array_get_value(array, index)); +} + +JSON_Array *json_array_get_array(const JSON_Array *array, size_t index) +{ + return json_value_get_array(json_array_get_value(array, index)); +} + +int json_array_get_boolean(const JSON_Array *array, size_t index) +{ + return json_value_get_boolean(json_array_get_value(array, index)); +} + +size_t json_array_get_count(const JSON_Array *array) +{ + return array ? array->count : 0; +} + +JSON_Value *json_array_get_wrapping_value(const JSON_Array *array) +{ + if (!array) { + return NULL; + } + return array->wrapping_value; +} + +/* JSON Value API */ +JSON_Value_Type json_value_get_type(const JSON_Value *value) +{ + return value ? value->type : JSONError; +} + +JSON_Object *json_value_get_object(const JSON_Value *value) +{ + return json_value_get_type(value) == JSONObject ? value->value.object + : NULL; +} + +JSON_Array *json_value_get_array(const JSON_Value *value) +{ + return json_value_get_type(value) == JSONArray ? value->value.array : NULL; +} + +static const JSON_String *json_value_get_string_desc(const JSON_Value *value) +{ + return json_value_get_type(value) == JSONString ? &value->value.string + : NULL; +} + +const char *json_value_get_string(const JSON_Value *value) +{ + const JSON_String *str = json_value_get_string_desc(value); + return str ? str->chars : NULL; +} + +size_t json_value_get_string_len(const JSON_Value *value) +{ + const JSON_String *str = json_value_get_string_desc(value); + return str ? str->length : 0; +} + +double json_value_get_number(const JSON_Value *value) +{ + return json_value_get_type(value) == JSONNumber ? value->value.number : 0; +} + +int json_value_get_boolean(const JSON_Value *value) +{ + return json_value_get_type(value) == JSONBoolean ? value->value.boolean + : -1; +} + +JSON_Value *json_value_get_parent(const JSON_Value *value) +{ + return value ? value->parent : NULL; +} + +void json_value_free(JSON_Value *value) +{ + switch (json_value_get_type(value)) { + case JSONObject: + json_object_free(value->value.object); + break; + case JSONString: + parson_free(value->value.string.chars); + break; + case JSONArray: + json_array_free(value->value.array); + break; + default: + break; + } + parson_free(value); +} + +JSON_Value *json_value_init_object(void) +{ + JSON_Value *new_value = (JSON_Value *)parson_malloc(sizeof(JSON_Value)); + if (!new_value) { + return NULL; + } + new_value->parent = NULL; + new_value->type = JSONObject; + new_value->value.object = json_object_make(new_value); + if (!new_value->value.object) { + parson_free(new_value); + return NULL; + } + return new_value; +} + +JSON_Value *json_value_init_array(void) +{ + JSON_Value *new_value = (JSON_Value *)parson_malloc(sizeof(JSON_Value)); + if (!new_value) { + return NULL; + } + new_value->parent = NULL; + new_value->type = JSONArray; + new_value->value.array = json_array_make(new_value); + if (!new_value->value.array) { + parson_free(new_value); + return NULL; + } + return new_value; +} + +JSON_Value *json_value_init_string(const char *string) +{ + if (string == NULL) { + return NULL; + } + return json_value_init_string_with_len(string, strlen(string)); +} + +JSON_Value *json_value_init_string_with_len(const char *string, size_t length) +{ + char *copy = NULL; + JSON_Value *value; + if (string == NULL) { + return NULL; + } + if (!is_valid_utf8(string, length)) { + return NULL; + } + copy = parson_strndup(string, length); + if (copy == NULL) { + return NULL; + } + value = json_value_init_string_no_copy(copy, length); + if (value == NULL) { + parson_free(copy); + } + return value; +} + +JSON_Value *json_value_init_number(double number) +{ + JSON_Value *new_value = NULL; + if (IS_NUMBER_INVALID(number)) { + return NULL; + } + new_value = (JSON_Value *)parson_malloc(sizeof(JSON_Value)); + if (new_value == NULL) { + return NULL; + } + new_value->parent = NULL; + new_value->type = JSONNumber; + new_value->value.number = number; + return new_value; +} + +JSON_Value *json_value_init_boolean(int boolean) +{ + JSON_Value *new_value = (JSON_Value *)parson_malloc(sizeof(JSON_Value)); + if (!new_value) { + return NULL; + } + new_value->parent = NULL; + new_value->type = JSONBoolean; + new_value->value.boolean = boolean ? 1 : 0; + return new_value; +} + +JSON_Value *json_value_init_null(void) +{ + JSON_Value *new_value = (JSON_Value *)parson_malloc(sizeof(JSON_Value)); + if (!new_value) { + return NULL; + } + new_value->parent = NULL; + new_value->type = JSONNull; + return new_value; +} + +JSON_Value *json_value_deep_copy(const JSON_Value *value) +{ + size_t i = 0; + JSON_Value *return_value = NULL, *temp_value_copy = NULL, + *temp_value = NULL; + const JSON_String *temp_string = NULL; + const char *temp_key = NULL; + char *temp_string_copy = NULL; + JSON_Array *temp_array = NULL, *temp_array_copy = NULL; + JSON_Object *temp_object = NULL, *temp_object_copy = NULL; + JSON_Status res = JSONFailure; + char *key_copy = NULL; + + switch (json_value_get_type(value)) { + case JSONArray: + temp_array = json_value_get_array(value); + return_value = json_value_init_array(); + if (return_value == NULL) { + return NULL; + } + temp_array_copy = json_value_get_array(return_value); + for (i = 0; i < json_array_get_count(temp_array); i++) { + temp_value = json_array_get_value(temp_array, i); + temp_value_copy = json_value_deep_copy(temp_value); + if (temp_value_copy == NULL) { + json_value_free(return_value); + return NULL; + } + if (json_array_add(temp_array_copy, temp_value_copy) != + JSONSuccess) { + json_value_free(return_value); + json_value_free(temp_value_copy); + return NULL; + } + } + return return_value; + case JSONObject: + temp_object = json_value_get_object(value); + return_value = json_value_init_object(); + if (!return_value) { + return NULL; + } + temp_object_copy = json_value_get_object(return_value); + for (i = 0; i < json_object_get_count(temp_object); i++) { + temp_key = json_object_get_name(temp_object, i); + temp_value = json_object_get_value(temp_object, temp_key); + temp_value_copy = json_value_deep_copy(temp_value); + if (!temp_value_copy) { + json_value_free(return_value); + return NULL; + } + key_copy = parson_strdup(temp_key); + if (!key_copy) { + json_value_free(temp_value_copy); + json_value_free(return_value); + return NULL; + } + res = json_object_add(temp_object_copy, key_copy, temp_value_copy); + if (res != JSONSuccess) { + parson_free(key_copy); + json_value_free(temp_value_copy); + json_value_free(return_value); + return NULL; + } + } + return return_value; + case JSONBoolean: + return json_value_init_boolean(json_value_get_boolean(value)); + case JSONNumber: + return json_value_init_number(json_value_get_number(value)); + case JSONString: + temp_string = json_value_get_string_desc(value); + if (temp_string == NULL) { + return NULL; + } + temp_string_copy = + parson_strndup(temp_string->chars, temp_string->length); + if (temp_string_copy == NULL) { + return NULL; + } + return_value = json_value_init_string_no_copy(temp_string_copy, + temp_string->length); + if (return_value == NULL) { + parson_free(temp_string_copy); + } + return return_value; + case JSONNull: + return json_value_init_null(); + case JSONError: + return NULL; + default: + return NULL; + } +} + +size_t json_serialization_size(const JSON_Value *value) +{ + char num_buf[PARSON_NUM_BUF_SIZE]; /* recursively allocating buffer on stack + is a bad idea, so let's do it only + once */ + int res = json_serialize_to_buffer_r(value, NULL, 0, PARSON_FALSE, num_buf); + return res < 0 ? 0 : (size_t)(res) + 1; +} + +JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, + size_t buf_size_in_bytes) +{ + int written = -1; + size_t needed_size_in_bytes = json_serialization_size(value); + if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) { + return JSONFailure; + } + written = json_serialize_to_buffer_r(value, buf, 0, PARSON_FALSE, NULL); + if (written < 0) { + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_serialize_to_file(const JSON_Value *value, + const char *filename) +{ + JSON_Status return_code = JSONSuccess; + FILE *fp = NULL; + char *serialized_string = json_serialize_to_string(value); + if (serialized_string == NULL) { + return JSONFailure; + } + fp = fopen(filename, "w"); + if (fp == NULL) { + json_free_serialized_string(serialized_string); + return JSONFailure; + } + if (fputs(serialized_string, fp) == EOF) { + return_code = JSONFailure; + } + if (fclose(fp) == EOF) { + return_code = JSONFailure; + } + json_free_serialized_string(serialized_string); + return return_code; +} + +char *json_serialize_to_string(const JSON_Value *value) +{ + JSON_Status serialization_result = JSONFailure; + size_t buf_size_bytes = json_serialization_size(value); + char *buf = NULL; + if (buf_size_bytes == 0) { + return NULL; + } + buf = (char *)parson_malloc(buf_size_bytes); + if (buf == NULL) { + return NULL; + } + serialization_result = json_serialize_to_buffer(value, buf, buf_size_bytes); + if (serialization_result != JSONSuccess) { + json_free_serialized_string(buf); + return NULL; + } + return buf; +} + +size_t json_serialization_size_pretty(const JSON_Value *value) +{ + char num_buf[PARSON_NUM_BUF_SIZE]; /* recursively allocating buffer on stack + is a bad idea, so let's do it only + once */ + int res = json_serialize_to_buffer_r(value, NULL, 0, PARSON_TRUE, num_buf); + return res < 0 ? 0 : (size_t)(res) + 1; +} + +JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, + size_t buf_size_in_bytes) +{ + int written = -1; + size_t needed_size_in_bytes = json_serialization_size_pretty(value); + if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) { + return JSONFailure; + } + written = json_serialize_to_buffer_r(value, buf, 0, PARSON_TRUE, NULL); + if (written < 0) { + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, + const char *filename) +{ + JSON_Status return_code = JSONSuccess; + FILE *fp = NULL; + char *serialized_string = json_serialize_to_string_pretty(value); + if (serialized_string == NULL) { + return JSONFailure; + } + fp = fopen(filename, "w"); + if (fp == NULL) { + json_free_serialized_string(serialized_string); + return JSONFailure; + } + if (fputs(serialized_string, fp) == EOF) { + return_code = JSONFailure; + } + if (fclose(fp) == EOF) { + return_code = JSONFailure; + } + json_free_serialized_string(serialized_string); + return return_code; +} + +char *json_serialize_to_string_pretty(const JSON_Value *value) +{ + JSON_Status serialization_result = JSONFailure; + size_t buf_size_bytes = json_serialization_size_pretty(value); + char *buf = NULL; + if (buf_size_bytes == 0) { + return NULL; + } + buf = (char *)parson_malloc(buf_size_bytes); + if (buf == NULL) { + return NULL; + } + serialization_result = + json_serialize_to_buffer_pretty(value, buf, buf_size_bytes); + if (serialization_result != JSONSuccess) { + json_free_serialized_string(buf); + return NULL; + } + return buf; +} + +void json_free_serialized_string(char *string) +{ + parson_free(string); +} + +JSON_Status json_array_remove(JSON_Array *array, size_t ix) +{ + size_t to_move_bytes = 0; + if (array == NULL || ix >= json_array_get_count(array)) { + return JSONFailure; + } + json_value_free(json_array_get_value(array, ix)); + to_move_bytes = + (json_array_get_count(array) - 1 - ix) * sizeof(JSON_Value *); + memmove(array->items + ix, array->items + ix + 1, to_move_bytes); + array->count -= 1; + return JSONSuccess; +} + +JSON_Status json_array_replace_value(JSON_Array *array, size_t ix, + JSON_Value *value) +{ + if (array == NULL || value == NULL || value->parent != NULL || + ix >= json_array_get_count(array)) { + return JSONFailure; + } + json_value_free(json_array_get_value(array, ix)); + value->parent = json_array_get_wrapping_value(array); + array->items[ix] = value; + return JSONSuccess; +} + +JSON_Status json_array_replace_string(JSON_Array *array, size_t i, + const char *string) +{ + JSON_Value *value = json_value_init_string(string); + if (value == NULL) { + return JSONFailure; + } + if (json_array_replace_value(array, i, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_array_replace_string_with_len(JSON_Array *array, size_t i, + const char *string, size_t len) +{ + JSON_Value *value = json_value_init_string_with_len(string, len); + if (value == NULL) { + return JSONFailure; + } + if (json_array_replace_value(array, i, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_array_replace_number(JSON_Array *array, size_t i, + double number) +{ + JSON_Value *value = json_value_init_number(number); + if (value == NULL) { + return JSONFailure; + } + if (json_array_replace_value(array, i, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean) +{ + JSON_Value *value = json_value_init_boolean(boolean); + if (value == NULL) { + return JSONFailure; + } + if (json_array_replace_value(array, i, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_array_replace_null(JSON_Array *array, size_t i) +{ + JSON_Value *value = json_value_init_null(); + if (value == NULL) { + return JSONFailure; + } + if (json_array_replace_value(array, i, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_array_clear(JSON_Array *array) +{ + size_t i = 0; + if (array == NULL) { + return JSONFailure; + } + for (i = 0; i < json_array_get_count(array); i++) { + json_value_free(json_array_get_value(array, i)); + } + array->count = 0; + return JSONSuccess; +} + +JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value) +{ + if (array == NULL || value == NULL || value->parent != NULL) { + return JSONFailure; + } + return json_array_add(array, value); +} + +JSON_Status json_array_append_string(JSON_Array *array, const char *string) +{ + JSON_Value *value = json_value_init_string(string); + if (value == NULL) { + return JSONFailure; + } + if (json_array_append_value(array, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_array_append_string_with_len(JSON_Array *array, + const char *string, size_t len) +{ + JSON_Value *value = json_value_init_string_with_len(string, len); + if (value == NULL) { + return JSONFailure; + } + if (json_array_append_value(array, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_array_append_number(JSON_Array *array, double number) +{ + JSON_Value *value = json_value_init_number(number); + if (value == NULL) { + return JSONFailure; + } + if (json_array_append_value(array, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_array_append_boolean(JSON_Array *array, int boolean) +{ + JSON_Value *value = json_value_init_boolean(boolean); + if (value == NULL) { + return JSONFailure; + } + if (json_array_append_value(array, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_array_append_null(JSON_Array *array) +{ + JSON_Value *value = json_value_init_null(); + if (value == NULL) { + return JSONFailure; + } + if (json_array_append_value(array, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_object_set_value(JSON_Object *object, const char *name, + JSON_Value *value) +{ + unsigned long hash = 0; + parson_bool_t found = PARSON_FALSE; + size_t cell_ix = 0; + size_t item_ix = 0; + JSON_Value *old_value = NULL; + char *key_copy = NULL; + + if (!object || !name || !value || value->parent) { + return JSONFailure; + } + hash = hash_string(name, strlen(name)); + found = PARSON_FALSE; + cell_ix = json_object_get_cell_ix(object, name, strlen(name), hash, &found); + if (found) { + item_ix = object->cells[cell_ix]; + old_value = object->values[item_ix]; + json_value_free(old_value); + object->values[item_ix] = value; + value->parent = json_object_get_wrapping_value(object); + return JSONSuccess; + } + if (object->count >= object->item_capacity) { + JSON_Status res = json_object_grow_and_rehash(object); + if (res != JSONSuccess) { + return JSONFailure; + } + cell_ix = + json_object_get_cell_ix(object, name, strlen(name), hash, &found); + } + key_copy = parson_strdup(name); + if (!key_copy) { + return JSONFailure; + } + object->names[object->count] = key_copy; + object->cells[cell_ix] = object->count; + object->values[object->count] = value; + object->cell_ixs[object->count] = cell_ix; + object->hashes[object->count] = hash; + object->count++; + value->parent = json_object_get_wrapping_value(object); + return JSONSuccess; +} + +JSON_Status json_object_set_string(JSON_Object *object, const char *name, + const char *string) +{ + JSON_Value *value = json_value_init_string(string); + JSON_Status status = json_object_set_value(object, name, value); + if (status != JSONSuccess) { + json_value_free(value); + } + return status; +} + +JSON_Status json_object_set_string_with_len(JSON_Object *object, + const char *name, + const char *string, size_t len) +{ + JSON_Value *value = json_value_init_string_with_len(string, len); + JSON_Status status = json_object_set_value(object, name, value); + if (status != JSONSuccess) { + json_value_free(value); + } + return status; +} + +JSON_Status json_object_set_number(JSON_Object *object, const char *name, + double number) +{ + JSON_Value *value = json_value_init_number(number); + JSON_Status status = json_object_set_value(object, name, value); + if (status != JSONSuccess) { + json_value_free(value); + } + return status; +} + +JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, + int boolean) +{ + JSON_Value *value = json_value_init_boolean(boolean); + JSON_Status status = json_object_set_value(object, name, value); + if (status != JSONSuccess) { + json_value_free(value); + } + return status; +} + +JSON_Status json_object_set_null(JSON_Object *object, const char *name) +{ + JSON_Value *value = json_value_init_null(); + JSON_Status status = json_object_set_value(object, name, value); + if (status != JSONSuccess) { + json_value_free(value); + } + return status; +} + +JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, + JSON_Value *value) +{ + const char *dot_pos = NULL; + JSON_Value *temp_value = NULL, *new_value = NULL; + JSON_Object *temp_object = NULL, *new_object = NULL; + JSON_Status status = JSONFailure; + size_t name_len = 0; + char *name_copy = NULL; + + if (object == NULL || name == NULL || value == NULL) { + return JSONFailure; + } + dot_pos = strchr(name, '.'); + if (dot_pos == NULL) { + return json_object_set_value(object, name, value); + } + name_len = dot_pos - name; + temp_value = json_object_getn_value(object, name, name_len); + if (temp_value) { + /* Don't overwrite existing non-object (unlike json_object_set_value, + * but it shouldn't be changed at this point) */ + if (json_value_get_type(temp_value) != JSONObject) { + return JSONFailure; + } + temp_object = json_value_get_object(temp_value); + return json_object_dotset_value(temp_object, dot_pos + 1, value); + } + new_value = json_value_init_object(); + if (new_value == NULL) { + return JSONFailure; + } + new_object = json_value_get_object(new_value); + status = json_object_dotset_value(new_object, dot_pos + 1, value); + if (status != JSONSuccess) { + json_value_free(new_value); + return JSONFailure; + } + name_copy = parson_strndup(name, name_len); + if (!name_copy) { + json_object_dotremove_internal(new_object, dot_pos + 1, 0); + json_value_free(new_value); + return JSONFailure; + } + status = json_object_add(object, name_copy, new_value); + if (status != JSONSuccess) { + parson_free(name_copy); + json_object_dotremove_internal(new_object, dot_pos + 1, 0); + json_value_free(new_value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, + const char *string) +{ + JSON_Value *value = json_value_init_string(string); + if (value == NULL) { + return JSONFailure; + } + if (json_object_dotset_value(object, name, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_object_dotset_string_with_len(JSON_Object *object, + const char *name, + const char *string, size_t len) +{ + JSON_Value *value = json_value_init_string_with_len(string, len); + if (value == NULL) { + return JSONFailure; + } + if (json_object_dotset_value(object, name, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, + double number) +{ + JSON_Value *value = json_value_init_number(number); + if (value == NULL) { + return JSONFailure; + } + if (json_object_dotset_value(object, name, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, + int boolean) +{ + JSON_Value *value = json_value_init_boolean(boolean); + if (value == NULL) { + return JSONFailure; + } + if (json_object_dotset_value(object, name, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_object_dotset_null(JSON_Object *object, const char *name) +{ + JSON_Value *value = json_value_init_null(); + if (value == NULL) { + return JSONFailure; + } + if (json_object_dotset_value(object, name, value) != JSONSuccess) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_object_remove(JSON_Object *object, const char *name) +{ + return json_object_remove_internal(object, name, PARSON_TRUE); +} + +JSON_Status json_object_dotremove(JSON_Object *object, const char *name) +{ + return json_object_dotremove_internal(object, name, PARSON_TRUE); +} + +JSON_Status json_object_clear(JSON_Object *object) +{ + size_t i = 0; + if (object == NULL) { + return JSONFailure; + } + for (i = 0; i < json_object_get_count(object); i++) { + parson_free(object->names[i]); + object->names[i] = NULL; + + json_value_free(object->values[i]); + object->values[i] = NULL; + } + object->count = 0; + for (i = 0; i < object->cell_capacity; i++) { + object->cells[i] = OBJECT_INVALID_IX; + } + return JSONSuccess; +} + +JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value) +{ + JSON_Value *temp_schema_value = NULL, *temp_value = NULL; + JSON_Array *schema_array = NULL, *value_array = NULL; + JSON_Object *schema_object = NULL, *value_object = NULL; + JSON_Value_Type schema_type = JSONError, value_type = JSONError; + const char *key = NULL; + size_t i = 0, count = 0; + if (schema == NULL || value == NULL) { + return JSONFailure; + } + schema_type = json_value_get_type(schema); + value_type = json_value_get_type(value); + if (schema_type != value_type && + schema_type != JSONNull) { /* null represents all values */ + return JSONFailure; + } + switch (schema_type) { + case JSONArray: + schema_array = json_value_get_array(schema); + value_array = json_value_get_array(value); + count = json_array_get_count(schema_array); + if (count == 0) { + return JSONSuccess; /* Empty array allows all types */ + } + /* Get first value from array, rest is ignored */ + temp_schema_value = json_array_get_value(schema_array, 0); + for (i = 0; i < json_array_get_count(value_array); i++) { + temp_value = json_array_get_value(value_array, i); + if (json_validate(temp_schema_value, temp_value) != JSONSuccess) { + return JSONFailure; + } + } + return JSONSuccess; + case JSONObject: + schema_object = json_value_get_object(schema); + value_object = json_value_get_object(value); + count = json_object_get_count(schema_object); + if (count == 0) { + return JSONSuccess; /* Empty object allows all objects */ + } + else if (json_object_get_count(value_object) < count) { + return JSONFailure; /* Tested object mustn't have less name-value + pairs than schema */ + } + for (i = 0; i < count; i++) { + key = json_object_get_name(schema_object, i); + temp_schema_value = json_object_get_value(schema_object, key); + temp_value = json_object_get_value(value_object, key); + if (temp_value == NULL) { + return JSONFailure; + } + if (json_validate(temp_schema_value, temp_value) != JSONSuccess) { + return JSONFailure; + } + } + return JSONSuccess; + case JSONString: + case JSONNumber: + case JSONBoolean: + case JSONNull: + return JSONSuccess; /* equality already tested before switch */ + case JSONError: + default: + return JSONFailure; + } +} + +int json_value_equals(const JSON_Value *a, const JSON_Value *b) +{ + JSON_Object *a_object = NULL, *b_object = NULL; + JSON_Array *a_array = NULL, *b_array = NULL; + const JSON_String *a_string = NULL, *b_string = NULL; + const char *key = NULL; + size_t a_count = 0, b_count = 0, i = 0; + JSON_Value_Type a_type, b_type; + a_type = json_value_get_type(a); + b_type = json_value_get_type(b); + if (a_type != b_type) { + return PARSON_FALSE; + } + switch (a_type) { + case JSONArray: + a_array = json_value_get_array(a); + b_array = json_value_get_array(b); + a_count = json_array_get_count(a_array); + b_count = json_array_get_count(b_array); + if (a_count != b_count) { + return PARSON_FALSE; + } + for (i = 0; i < a_count; i++) { + if (!json_value_equals(json_array_get_value(a_array, i), + json_array_get_value(b_array, i))) { + return PARSON_FALSE; + } + } + return PARSON_TRUE; + case JSONObject: + a_object = json_value_get_object(a); + b_object = json_value_get_object(b); + a_count = json_object_get_count(a_object); + b_count = json_object_get_count(b_object); + if (a_count != b_count) { + return PARSON_FALSE; + } + for (i = 0; i < a_count; i++) { + key = json_object_get_name(a_object, i); + if (!json_value_equals(json_object_get_value(a_object, key), + json_object_get_value(b_object, key))) { + return PARSON_FALSE; + } + } + return PARSON_TRUE; + case JSONString: + a_string = json_value_get_string_desc(a); + b_string = json_value_get_string_desc(b); + if (a_string == NULL || b_string == NULL) { + return PARSON_FALSE; /* shouldn't happen */ + } + return a_string->length == b_string->length && + memcmp(a_string->chars, b_string->chars, a_string->length) == 0; + case JSONBoolean: + return json_value_get_boolean(a) == json_value_get_boolean(b); + case JSONNumber: + return fabs(json_value_get_number(a) - json_value_get_number(b)) < + 0.000001; /* EPSILON */ + case JSONError: + return PARSON_TRUE; + case JSONNull: + return PARSON_TRUE; + default: + return PARSON_TRUE; + } +} + +JSON_Value_Type json_type(const JSON_Value *value) +{ + return json_value_get_type(value); +} + +JSON_Object *json_object(const JSON_Value *value) +{ + return json_value_get_object(value); +} + +JSON_Array *json_array(const JSON_Value *value) +{ + return json_value_get_array(value); +} + +const char *json_string(const JSON_Value *value) +{ + return json_value_get_string(value); +} + +size_t json_string_len(const JSON_Value *value) +{ + return json_value_get_string_len(value); +} + +double json_number(const JSON_Value *value) +{ + return json_value_get_number(value); +} + +int json_boolean(const JSON_Value *value) +{ + return json_value_get_boolean(value); +} + +void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, + JSON_Free_Function free_fun) +{ + parson_malloc = malloc_fun; + parson_free = free_fun; +} + +void json_set_escape_slashes(int escape_slashes) +{ + parson_escape_slashes = escape_slashes; +} + +void json_set_float_serialization_format(const char *format) +{ + if (parson_float_format) { + parson_free(parson_float_format); + parson_float_format = NULL; + } + if (!format) { + parson_float_format = NULL; + return; + } + parson_float_format = parson_strdup(format); +} + +void json_set_number_serialization_function( + JSON_Number_Serialization_Function func) +{ + parson_number_serialization_function = func; +} diff --git a/lib/external/parson/parson.h b/lib/external/parson/parson.h new file mode 100644 index 00000000000..5799e0346ef --- /dev/null +++ b/lib/external/parson/parson.h @@ -0,0 +1,333 @@ +/* + SPDX-License-Identifier: MIT + + Parson 1.5.3 (https://github.com/kgabis/parson) + Copyright (c) 2012 - 2023 Krzysztof Gabis + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef parson_parson_h +#define parson_parson_h + +#ifdef __cplusplus +extern "C" { +#endif +#if 0 +} /* unconfuse xcode */ +#endif + +#define PARSON_VERSION_MAJOR 1 +#define PARSON_VERSION_MINOR 5 +#define PARSON_VERSION_PATCH 3 + +#define PARSON_VERSION_STRING "1.5.3" + +#include /* size_t */ + +/* Types and enums */ +typedef struct json_object_t JSON_Object; +typedef struct json_array_t JSON_Array; +typedef struct json_value_t JSON_Value; + +enum json_value_type { + JSONError = -1, + JSONNull = 1, + JSONString = 2, + JSONNumber = 3, + JSONObject = 4, + JSONArray = 5, + JSONBoolean = 6 +}; +typedef int JSON_Value_Type; + +enum json_result_t { JSONSuccess = 0, JSONFailure = -1 }; +typedef int JSON_Status; + +typedef void *(*JSON_Malloc_Function)(size_t); +typedef void (*JSON_Free_Function)(void *); + +/* A function used for serializing numbers (see + json_set_number_serialization_function). If 'buf' is null then it should + return number of bytes that would've been written (but not more than + PARSON_NUM_BUF_SIZE). +*/ +typedef int (*JSON_Number_Serialization_Function)(double num, char *buf); + +/* Call only once, before calling any other function from parson API. If not + called, malloc and free from stdlib will be used for all allocations */ +void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, + JSON_Free_Function free_fun); + +/* Sets if slashes should be escaped or not when serializing JSON. By default + slashes are escaped. This function sets a global setting and is not thread + safe. */ +void json_set_escape_slashes(int escape_slashes); + +/* Sets float format used for serialization of numbers. + Make sure it can't serialize to a string longer than PARSON_NUM_BUF_SIZE. + If format is null then the default format is used. */ +void json_set_float_serialization_format(const char *format); + +/* Sets a function that will be used for serialization of numbers. + If function is null then the default serialization function is used. */ +void json_set_number_serialization_function( + JSON_Number_Serialization_Function fun); + +/* Parses first JSON value in a file, returns NULL in case of error */ +JSON_Value *json_parse_file(const char *filename); + +/* Parses first JSON value in a file and ignores comments (/ * * / and //), + returns NULL in case of error */ +JSON_Value *json_parse_file_with_comments(const char *filename); + +/* Parses first JSON value in a string, returns NULL in case of error */ +JSON_Value *json_parse_string(const char *string); + +/* Parses first JSON value in a string and ignores comments (/ * * / and //), + returns NULL in case of error */ +JSON_Value *json_parse_string_with_comments(const char *string); + +/* Serialization */ +size_t json_serialization_size(const JSON_Value *value); /* returns 0 on fail */ +JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, + size_t buf_size_in_bytes); +JSON_Status json_serialize_to_file(const JSON_Value *value, + const char *filename); +char *json_serialize_to_string(const JSON_Value *value); + +/* Pretty serialization */ +size_t +json_serialization_size_pretty(const JSON_Value *value); /* returns 0 on fail */ +JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, + size_t buf_size_in_bytes); +JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, + const char *filename); +char *json_serialize_to_string_pretty(const JSON_Value *value); + +void json_free_serialized_string( + char *string); /* frees string from json_serialize_to_string and + json_serialize_to_string_pretty */ + +/* Comparing */ +int json_value_equals(const JSON_Value *a, const JSON_Value *b); + +/* Validation + This is *NOT* JSON Schema. It validates json by checking if object have + identically named fields with matching types. For example schema {"name":"", + "age":0} will validate + {"name":"Joe", "age":25} and {"name":"Joe", "age":25, "gender":"m"}, + but not {"name":"Joe"} or {"name":"Joe", "age":"Cucumber"}. + In case of arrays, only first value in schema is checked against all values + in tested array. Empty objects ({}) validate all objects, empty arrays ([]) + validate all arrays, null validates values of every type. + */ +JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value); + +/* + * JSON Object + */ +JSON_Value *json_object_get_value(const JSON_Object *object, const char *name); +const char *json_object_get_string(const JSON_Object *object, const char *name); +size_t json_object_get_string_len( + const JSON_Object *object, + const char *name); /* doesn't account for last null character */ +JSON_Object *json_object_get_object(const JSON_Object *object, + const char *name); +JSON_Array *json_object_get_array(const JSON_Object *object, const char *name); +double json_object_get_number(const JSON_Object *object, + const char *name); /* returns 0 on fail */ +int json_object_get_boolean(const JSON_Object *object, + const char *name); /* returns -1 on fail */ + +/* dotget functions enable addressing values with dot notation in nested + objects, just like in structs or c++/java/c# objects (e.g. + objectA.objectB.value). Because valid names in JSON can contain dots, some + values may be inaccessible this way. */ +JSON_Value *json_object_dotget_value(const JSON_Object *object, + const char *name); +const char *json_object_dotget_string(const JSON_Object *object, + const char *name); +size_t json_object_dotget_string_len( + const JSON_Object *object, + const char *name); /* doesn't account for last null character */ +JSON_Object *json_object_dotget_object(const JSON_Object *object, + const char *name); +JSON_Array *json_object_dotget_array(const JSON_Object *object, + const char *name); +double json_object_dotget_number(const JSON_Object *object, + const char *name); /* returns 0 on fail */ +int json_object_dotget_boolean(const JSON_Object *object, + const char *name); /* returns -1 on fail */ + +/* Functions to get available names */ +size_t json_object_get_count(const JSON_Object *object); +const char *json_object_get_name(const JSON_Object *object, size_t index); +JSON_Value *json_object_get_value_at(const JSON_Object *object, size_t index); +JSON_Value *json_object_get_wrapping_value(const JSON_Object *object); + +/* Functions to check if object has a value with a specific name. Returned value + * is 1 if object has a value and 0 if it doesn't. dothas functions behave + * exactly like dotget functions. */ +int json_object_has_value(const JSON_Object *object, const char *name); +int json_object_has_value_of_type(const JSON_Object *object, const char *name, + JSON_Value_Type type); + +int json_object_dothas_value(const JSON_Object *object, const char *name); +int json_object_dothas_value_of_type(const JSON_Object *object, + const char *name, JSON_Value_Type type); + +/* Creates new name-value pair or frees and replaces old value with a new one. + * json_object_set_value does not copy passed value so it shouldn't be freed + * afterwards. */ +JSON_Status json_object_set_value(JSON_Object *object, const char *name, + JSON_Value *value); +JSON_Status json_object_set_string(JSON_Object *object, const char *name, + const char *string); +JSON_Status json_object_set_string_with_len( + JSON_Object *object, const char *name, const char *string, + size_t len); /* length shouldn't include last null character */ +JSON_Status json_object_set_number(JSON_Object *object, const char *name, + double number); +JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, + int boolean); +JSON_Status json_object_set_null(JSON_Object *object, const char *name); + +/* Works like dotget functions, but creates whole hierarchy if necessary. + * json_object_dotset_value does not copy passed value so it shouldn't be freed + * afterwards. */ +JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, + JSON_Value *value); +JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, + const char *string); +JSON_Status json_object_dotset_string_with_len( + JSON_Object *object, const char *name, const char *string, + size_t len); /* length shouldn't include last null character */ +JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, + double number); +JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, + int boolean); +JSON_Status json_object_dotset_null(JSON_Object *object, const char *name); + +/* Frees and removes name-value pair */ +JSON_Status json_object_remove(JSON_Object *object, const char *name); + +/* Works like dotget function, but removes name-value pair only on exact match. + */ +JSON_Status json_object_dotremove(JSON_Object *object, const char *key); + +/* Removes all name-value pairs in object */ +JSON_Status json_object_clear(JSON_Object *object); + +/* + *JSON Array + */ +JSON_Value *json_array_get_value(const JSON_Array *array, size_t index); +const char *json_array_get_string(const JSON_Array *array, size_t index); +size_t json_array_get_string_len( + const JSON_Array *array, + size_t index); /* doesn't account for last null character */ +JSON_Object *json_array_get_object(const JSON_Array *array, size_t index); +JSON_Array *json_array_get_array(const JSON_Array *array, size_t index); +double json_array_get_number(const JSON_Array *array, + size_t index); /* returns 0 on fail */ +int json_array_get_boolean(const JSON_Array *array, + size_t index); /* returns -1 on fail */ +size_t json_array_get_count(const JSON_Array *array); +JSON_Value *json_array_get_wrapping_value(const JSON_Array *array); + +/* Frees and removes value at given index, does nothing and returns JSONFailure + * if index doesn't exist. Order of values in array may change during execution. + */ +JSON_Status json_array_remove(JSON_Array *array, size_t i); + +/* Frees and removes from array value at given index and replaces it with given + * one. Does nothing and returns JSONFailure if index doesn't exist. + * json_array_replace_value does not copy passed value so it shouldn't be freed + * afterwards. */ +JSON_Status json_array_replace_value(JSON_Array *array, size_t i, + JSON_Value *value); +JSON_Status json_array_replace_string(JSON_Array *array, size_t i, + const char *string); +JSON_Status json_array_replace_string_with_len( + JSON_Array *array, size_t i, const char *string, + size_t len); /* length shouldn't include last null character */ +JSON_Status json_array_replace_number(JSON_Array *array, size_t i, + double number); +JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, + int boolean); +JSON_Status json_array_replace_null(JSON_Array *array, size_t i); + +/* Frees and removes all values from array */ +JSON_Status json_array_clear(JSON_Array *array); + +/* Appends new value at the end of array. + * json_array_append_value does not copy passed value so it shouldn't be freed + * afterwards. */ +JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value); +JSON_Status json_array_append_string(JSON_Array *array, const char *string); +JSON_Status json_array_append_string_with_len( + JSON_Array *array, const char *string, + size_t len); /* length shouldn't include last null character */ +JSON_Status json_array_append_number(JSON_Array *array, double number); +JSON_Status json_array_append_boolean(JSON_Array *array, int boolean); +JSON_Status json_array_append_null(JSON_Array *array); + +/* + *JSON Value + */ +JSON_Value *json_value_init_object(void); +JSON_Value *json_value_init_array(void); +JSON_Value * +json_value_init_string(const char *string); /* copies passed string */ +JSON_Value *json_value_init_string_with_len( + const char *string, + size_t length); /* copies passed string, length shouldn't include last null + character */ +JSON_Value *json_value_init_number(double number); +JSON_Value *json_value_init_boolean(int boolean); +JSON_Value *json_value_init_null(void); +JSON_Value *json_value_deep_copy(const JSON_Value *value); +void json_value_free(JSON_Value *value); + +JSON_Value_Type json_value_get_type(const JSON_Value *value); +JSON_Object *json_value_get_object(const JSON_Value *value); +JSON_Array *json_value_get_array(const JSON_Value *value); +const char *json_value_get_string(const JSON_Value *value); +size_t json_value_get_string_len( + const JSON_Value *value); /* doesn't account for last null character */ +double json_value_get_number(const JSON_Value *value); +int json_value_get_boolean(const JSON_Value *value); +JSON_Value *json_value_get_parent(const JSON_Value *value); + +/* Same as above, but shorter */ +JSON_Value_Type json_type(const JSON_Value *value); +JSON_Object *json_object(const JSON_Value *value); +JSON_Array *json_array(const JSON_Value *value); +const char *json_string(const JSON_Value *value); +size_t json_string_len( + const JSON_Value *value); /* doesn't account for last null character */ +double json_number(const JSON_Value *value); +int json_boolean(const JSON_Value *value); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/macosx/ReadMe.md b/macosx/ReadMe.md index dc68ea85acc..ac19f4c57de 100644 --- a/macosx/ReadMe.md +++ b/macosx/ReadMe.md @@ -654,6 +654,6 @@ This program is free software under the GNU General Public License (>=v2). \- William Kyngesburye -kyngchaos@kyngchaos.com + diff --git a/python/grass/docs/src/gunittest_running_tests.rst b/python/grass/docs/src/gunittest_running_tests.rst index 2e6e402835a..93f99cc93fb 100644 --- a/python/grass/docs/src/gunittest_running_tests.rst +++ b/python/grass/docs/src/gunittest_running_tests.rst @@ -100,7 +100,7 @@ you would use:: [gunittest] exclude = vector/* - raster/r.contour/testsuite/testrc.py + raster/r.contour/testsuite/test_broken.py Running tests and creating report diff --git a/raster/r.contour/testsuite/testrc.py b/raster/r.contour/testsuite/testrc.py deleted file mode 100644 index 89e1b685c67..00000000000 --- a/raster/r.contour/testsuite/testrc.py +++ /dev/null @@ -1,120 +0,0 @@ -""" -Name: r.contour test -Purpose: Tests r.contour and its flags/options. - -Author: Sunveer Singh, Google Code-in 2018 -Copyright: (C) 2018 by Sunveer Singh and the GRASS Development Team -Licence: This program is free software under the GNU General Public - License (>=v2). Read the file COPYING that comes with GRASS - for details. -""" -from grass.gunittest.case import TestCase -from grass.gunittest.main import test -from grass.gunittest.gmodules import SimpleModule - -out_where = """cat|level -1|56 -2|58 -3|60 -4|62 -5|64 -6|66 -7|68 -8|70 -9|72 -10|74 -11|76 -12|78 -13|80 -14|82 -15|84 -16|86 -17|88 -18|90 -19|92 -20|94 -21|96 -22|98 -23|100 -24|102 -25|104 -26|106 -27|108 -28|110 -29|112 -30|114 -31|116 -32|118 -33|120 -34|122 -35|124 -36|126 -37|128 -38|130 -39|132 -40|134 -41|136 -42|138 -43|140 -44|142 -45|144 -46|146 -47|148 -48|150 -49|152 -50|154 -51|156 -""" - - -class Testrr(TestCase): - input = "elevation" - output = "towns" - - @classmethod - def setUpClass(cls): - cls.use_temp_region() - cls.runModule("g.region", raster=cls.input) - - @classmethod - def tearDownClass(cls): - cls.del_temp_region() - - def tearDown(cls): - cls.runModule("g.remove", type="vector", flags="f", name=cls.output) - - def test_flag_t(self): - """Testing flag t""" - string = """min=1 - max=6""" - self.assertModule( - "r.contour", - input=self.input, - output=self.output, - levels=1, - step=1, - flags="t", - ) - self.assertRasterFitsUnivar(self.output, reference=string, precision=2) - - def test_vector(self): - """Testing vector output""" - self.assertModule( - "r.contour", input=self.input, output=self.output, step=5, flags="t" - ) - self.assertModule("v.info", map=self.output, flags="t") - topology = dict(points=0, lines=2222, areas=0) - self.assertVectorFitsTopoInfo(self.output, topology) - - def test_v_db_select(self): - """Testing attribute values of contours with v.db.select""" - self.assertModule("r.contour", input=self.input, output=self.output, step=2) - v_db_select = SimpleModule("v.db.select", map=self.output) - v_db_select.run() - self.assertLooksLike(reference=out_where, actual=v_db_select.outputs.stdout) - - -if __name__ == "__main__": - from grass.gunittest.main import test - - test() diff --git a/raster/r.horizon/main.c b/raster/r.horizon/main.c index 55643901f2f..06946584498 100644 --- a/raster/r.horizon/main.c +++ b/raster/r.horizon/main.c @@ -959,7 +959,7 @@ int test_low_res(void) else if (sinangle < 0.) { sy = yy0 * invstepy + offsety; dely = floor( - fabs((floor(jp / 100.) - (sy / 100.)) * distsinangle)); + fabs((floor(sy / 100.) - (sy / 100.)) * distsinangle)); } mindel = min(delx, dely); diff --git a/raster/r.walk/r.walk.html b/raster/r.walk/r.walk.html index 1ab039a4a2f..2523b7fc0b6 100644 --- a/raster/r.walk/r.walk.html +++ b/raster/r.walk/r.walk.html @@ -64,7 +64,7 @@

NOTES

The friction cost parameter represents a time penalty in seconds of additional walking time to cross 1 meter distance. -Friction cost can be any floating point value ≥ 0. +Friction cost can be any floating point value ≥ 0. A friction map is a required parameter; if no friction costs are desired, a friction map should be a raster in which all cells have a value of 0.

The lambda parameter is a dimensionless scaling factor of the friction cost: