Compare commits
18 Commits
f1700302e4
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 1893df8ca0 | |||
| 0a46003741 | |||
| bde893a905 | |||
| a9b258ceb3 | |||
| 9e666a892d | |||
| 823e4c83ef | |||
| fa1be9fb42 | |||
| 969251661c | |||
| effb6385c0 | |||
| ba0e8b81fa | |||
| d757710f5d | |||
| c10755ad48 | |||
| b6e557757b | |||
| 54f612e785 | |||
| 3e68a2959f | |||
| 7cad0f8daf | |||
| 0aa3be643e | |||
| 5621f19feb |
25
JsonService.cpp
Normal file
25
JsonService.cpp
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#include "JsonService.h"
|
||||||
|
#include "thirdparty/json.hpp"
|
||||||
|
#include <fstream>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
using nlohmann::json;
|
||||||
|
|
||||||
|
Weather JsonService::getWeather(std::string s) {
|
||||||
|
std::ifstream fin(s);
|
||||||
|
if (!fin)
|
||||||
|
throw std::runtime_error("error"); // Linux support
|
||||||
|
|
||||||
|
json j;
|
||||||
|
j = json::parse(fin);
|
||||||
|
|
||||||
|
std::string city = j["name"]; // Киров
|
||||||
|
double lon = j["coord"]["lon"]; // 49.6601
|
||||||
|
double lat = j["coord"]["lat"]; // 58.5966
|
||||||
|
double temperature = j["main"]["temp"]; // 5.69
|
||||||
|
std::string weather = j["weather"][0]["description"]; // дождь
|
||||||
|
double windSpeed = j["wind"]["speed"]; // 4.27
|
||||||
|
int clouds = j["clouds"]["all"]; // 100
|
||||||
|
|
||||||
|
return Weather(city, lon, lat, temperature, weather, windSpeed, clouds);
|
||||||
|
}
|
||||||
8
JsonService.h
Normal file
8
JsonService.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#include "Service.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class JsonService : public Service {
|
||||||
|
public:
|
||||||
|
virtual Weather getWeather(std::string s) override;
|
||||||
|
virtual ~JsonService() {}
|
||||||
|
};
|
||||||
10
Service.h
Normal file
10
Service.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <string>
|
||||||
|
#include "Weather.h"
|
||||||
|
|
||||||
|
class Service {
|
||||||
|
public:
|
||||||
|
virtual ~Service() = default;
|
||||||
|
|
||||||
|
virtual Weather getWeather(std::string s) = 0;
|
||||||
|
};
|
||||||
15
Weather.cpp
Normal file
15
Weather.cpp
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#include "Weather.h"
|
||||||
|
|
||||||
|
Weather::Weather(const std::string& city, double lon, double lat, double temperature,
|
||||||
|
const std::string& weather, double windSpeed, int clouds)
|
||||||
|
: city(city), lon(lon), lat(lat), temperature(temperature),
|
||||||
|
weather(weather), windSpeed(windSpeed), clouds(clouds) {
|
||||||
|
}
|
||||||
|
|
||||||
|
void Weather::printWeather() const {
|
||||||
|
std::cout << "City: " << city << " (Lat: " << lat << ", Lon: " << lon << ")\n"
|
||||||
|
<< "Temperature: " << temperature << " deg. C\n"
|
||||||
|
<< "Weather: " << weather << "\n"
|
||||||
|
<< "Wind Speed: " << windSpeed << " m/s\n"
|
||||||
|
<< "Cloudness: " << clouds << " %\n";
|
||||||
|
}
|
||||||
20
Weather.h
Normal file
20
Weather.h
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
class Weather {
|
||||||
|
private:
|
||||||
|
std::string city; // Киров
|
||||||
|
double lon; // 49.6601
|
||||||
|
double lat; // 58.5966
|
||||||
|
double temperature; // 5.69
|
||||||
|
std::string weather; // дождь
|
||||||
|
double windSpeed; // 4.27
|
||||||
|
int clouds; // 100
|
||||||
|
|
||||||
|
public:
|
||||||
|
Weather(const std::string& city, double lon, double lat, double temperature,
|
||||||
|
const std::string& weather, double windSpeed, int clouds);
|
||||||
|
|
||||||
|
void printWeather() const;
|
||||||
|
};
|
||||||
29
XmlService.cpp
Normal file
29
XmlService.cpp
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#include "XmlService.h"
|
||||||
|
#include "thirdparty/pugixml-1.15/pugixml.hpp"
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
using namespace pugi;
|
||||||
|
|
||||||
|
Weather XmlService::getWeather(std::string s) {
|
||||||
|
xml_document doc;
|
||||||
|
xml_parse_result result = doc.load_file(s.c_str());
|
||||||
|
if (!result)
|
||||||
|
throw std::runtime_error("error");
|
||||||
|
|
||||||
|
xml_node node = doc.child("current");
|
||||||
|
|
||||||
|
std::string city = node.child("city").attribute("name").as_string(); // Киров
|
||||||
|
|
||||||
|
double lon = node.child("city").child("coord").attribute("lon").as_double(); // 49.6601
|
||||||
|
double lat = node.child("city").child("coord").attribute("lat").as_double(); // 58.5966
|
||||||
|
|
||||||
|
double temperature = node.child("temperature").attribute("value").as_double(); // 5.69
|
||||||
|
|
||||||
|
std::string weather = node.child("weather").attribute("value").as_string(); // дождь
|
||||||
|
|
||||||
|
double windSpeed = node.child("wind").child("speed").attribute("value").as_double(); // 4.27
|
||||||
|
|
||||||
|
int clouds = node.child("clouds").attribute("value").as_int(); // 100
|
||||||
|
|
||||||
|
return Weather(city, lon, lat, temperature, weather, windSpeed, clouds);
|
||||||
|
}
|
||||||
8
XmlService.h
Normal file
8
XmlService.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#include "Service.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class XmlService : public Service {
|
||||||
|
public:
|
||||||
|
virtual Weather getWeather(std::string s) override;
|
||||||
|
virtual ~XmlService() {}
|
||||||
|
};
|
||||||
47
endpoints/weather.json
Normal file
47
endpoints/weather.json
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
{
|
||||||
|
"coord":{
|
||||||
|
"lon":49.6601,
|
||||||
|
"lat":58.5966
|
||||||
|
},
|
||||||
|
"weather":[
|
||||||
|
{
|
||||||
|
"id":501,
|
||||||
|
"main":"Rain",
|
||||||
|
"description":"дождь",
|
||||||
|
"icon":"10d"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"base":"stations",
|
||||||
|
"main":{
|
||||||
|
"temp":5.69,
|
||||||
|
"feels_like":2.56,
|
||||||
|
"temp_min":5.69,
|
||||||
|
"temp_max":5.69,
|
||||||
|
"pressure":998,
|
||||||
|
"humidity":100,
|
||||||
|
"sea_level":998,
|
||||||
|
"grnd_level":977
|
||||||
|
},
|
||||||
|
"visibility":133,
|
||||||
|
"wind":{
|
||||||
|
"speed":4.27,
|
||||||
|
"deg":171,
|
||||||
|
"gust":11.57
|
||||||
|
},
|
||||||
|
"rain":{
|
||||||
|
"1h":1.15
|
||||||
|
},
|
||||||
|
"clouds":{
|
||||||
|
"all":100
|
||||||
|
},
|
||||||
|
"dt":1679907593,
|
||||||
|
"sys":{
|
||||||
|
"country":"RU",
|
||||||
|
"sunrise":1679883870,
|
||||||
|
"sunset":1679929745
|
||||||
|
},
|
||||||
|
"timezone":10800,
|
||||||
|
"id":548408,
|
||||||
|
"name":"Киров",
|
||||||
|
"cod":200
|
||||||
|
}
|
||||||
22
endpoints/weather.xml
Normal file
22
endpoints/weather.xml
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<current>
|
||||||
|
<city id="548408" name="Киров">
|
||||||
|
<coord lon="49.6601" lat="58.5966"/>
|
||||||
|
<country>RU</country>
|
||||||
|
<timezone>10800</timezone>
|
||||||
|
<sun rise="2023-03-27T02:24:30" set="2023-03-27T15:09:05"/>
|
||||||
|
</city>
|
||||||
|
<temperature value="5.69" min="5.69" max="5.69" unit="celsius"/>
|
||||||
|
<feels_like value="2.56" unit="celsius"/>
|
||||||
|
<humidity value="100" unit="%"/>
|
||||||
|
<pressure value="998" unit="hPa"/>
|
||||||
|
<wind>
|
||||||
|
<speed value="4.27" unit="m/s" name="Gentle Breeze"/>
|
||||||
|
<gusts value="11.57"/>
|
||||||
|
<direction value="171" code="S" name="South"/>
|
||||||
|
</wind>
|
||||||
|
<clouds value="100" name="пасмурно"/>
|
||||||
|
<visibility value="133"/>
|
||||||
|
<precipitation value="0.45" mode="rain" unit="1h"/>
|
||||||
|
<weather number="500" value="небольшой дождь" icon="10d"/>
|
||||||
|
<lastupdate value="2023-03-27T08:55:52"/>
|
||||||
|
</current>
|
||||||
27
main.cpp
Normal file
27
main.cpp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <exception>
|
||||||
|
#include "JsonService.h"
|
||||||
|
#include "XmlService.h"
|
||||||
|
#include "Weather.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
try {
|
||||||
|
std::cout << "Testing JSON Service" << std::endl;
|
||||||
|
JsonService js;
|
||||||
|
Weather w1 = js.getWeather("endpoints/weather.json");
|
||||||
|
std::cout << "Weather object created (JSON)" << std::endl;
|
||||||
|
w1.printWeather();
|
||||||
|
|
||||||
|
std::cout << "\nTesting XML Service" << std::endl;
|
||||||
|
XmlService xs;
|
||||||
|
Weather w2 = xs.getWeather("endpoints/weather.xml");
|
||||||
|
std::cout << "Weather object created (XML)" << std::endl;
|
||||||
|
w2.printWeather();
|
||||||
|
}
|
||||||
|
catch (const std::exception& e) {
|
||||||
|
std::cerr << "Error: " << e.what() << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
25526
thirdparty/json.hpp
vendored
Normal file
25526
thirdparty/json.hpp
vendored
Normal file
File diff suppressed because it is too large
Load Diff
80
thirdparty/pugixml-1.15/pugiconfig.hpp
vendored
Normal file
80
thirdparty/pugixml-1.15/pugiconfig.hpp
vendored
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
/**
|
||||||
|
* pugixml parser - version 1.15
|
||||||
|
* --------------------------------------------------------
|
||||||
|
* Copyright (C) 2006-2025, by Arseny Kapoulkine (arseny.kapoulkine@gmail.com)
|
||||||
|
* Report bugs and download new versions at https://pugixml.org/
|
||||||
|
*
|
||||||
|
* This library is distributed under the MIT License. See notice at the end
|
||||||
|
* of this file.
|
||||||
|
*
|
||||||
|
* This work is based on the pugxml parser, which is:
|
||||||
|
* Copyright (C) 2003, by Kristen Wegner (kristen@tima.net)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef HEADER_PUGICONFIG_HPP
|
||||||
|
#define HEADER_PUGICONFIG_HPP
|
||||||
|
|
||||||
|
// Uncomment this to enable wchar_t mode
|
||||||
|
// #define PUGIXML_WCHAR_MODE
|
||||||
|
|
||||||
|
// Uncomment this to enable compact mode
|
||||||
|
// #define PUGIXML_COMPACT
|
||||||
|
|
||||||
|
// Uncomment this to disable XPath
|
||||||
|
// #define PUGIXML_NO_XPATH
|
||||||
|
|
||||||
|
// Uncomment this to disable STL
|
||||||
|
// #define PUGIXML_NO_STL
|
||||||
|
|
||||||
|
// Uncomment this to disable exceptions
|
||||||
|
// #define PUGIXML_NO_EXCEPTIONS
|
||||||
|
|
||||||
|
// Set this to control attributes for public classes/functions, i.e.:
|
||||||
|
// #define PUGIXML_API __declspec(dllexport) // to export all public symbols from DLL
|
||||||
|
// #define PUGIXML_CLASS __declspec(dllimport) // to import all classes from DLL
|
||||||
|
// #define PUGIXML_FUNCTION __fastcall // to set calling conventions to all public functions to fastcall
|
||||||
|
// In absence of PUGIXML_CLASS/PUGIXML_FUNCTION definitions PUGIXML_API is used instead
|
||||||
|
|
||||||
|
// Tune these constants to adjust memory-related behavior
|
||||||
|
// #define PUGIXML_MEMORY_PAGE_SIZE 32768
|
||||||
|
// #define PUGIXML_MEMORY_OUTPUT_STACK 10240
|
||||||
|
// #define PUGIXML_MEMORY_XPATH_PAGE_SIZE 4096
|
||||||
|
|
||||||
|
// Tune this constant to adjust max nesting for XPath queries
|
||||||
|
// #define PUGIXML_XPATH_DEPTH_LIMIT 1024
|
||||||
|
|
||||||
|
// Uncomment this to switch to header-only version
|
||||||
|
// #define PUGIXML_HEADER_ONLY
|
||||||
|
|
||||||
|
// Uncomment this to enable long long support (usually enabled automatically)
|
||||||
|
// #define PUGIXML_HAS_LONG_LONG
|
||||||
|
|
||||||
|
// Uncomment this to enable support for std::string_view (usually enabled automatically)
|
||||||
|
// #define PUGIXML_HAS_STRING_VIEW
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2006-2025 Arseny Kapoulkine
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
13553
thirdparty/pugixml-1.15/pugixml.cpp
vendored
Normal file
13553
thirdparty/pugixml-1.15/pugixml.cpp
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1585
thirdparty/pugixml-1.15/pugixml.hpp
vendored
Normal file
1585
thirdparty/pugixml-1.15/pugixml.hpp
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user