diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..eeafe64f --- /dev/null +++ b/Makefile @@ -0,0 +1,460 @@ +# CMAKE generated file: DO NOT EDIT! +# Generated by "Unix Makefiles" Generator, CMake Version 2.8 + +# Default target executed when no arguments are given to make. +default_target: all +.PHONY : default_target + +#============================================================================= +# Special targets provided by cmake. + +# Disable implicit rules so canoncical targets will work. +.SUFFIXES: + +# Remove some rules from gmake that .SUFFIXES does not remove. +SUFFIXES = + +.SUFFIXES: .hpux_make_needs_suffix_list + +# Suppress display of executed commands. +$(VERBOSE).SILENT: + +# A target that is always out of date. +cmake_force: +.PHONY : cmake_force + +#============================================================================= +# Set environment variables for the build. + +# The shell in which to execute make rules. +SHELL = /bin/sh + +# The CMake executable. +CMAKE_COMMAND = /usr/bin/cmake + +# The command to remove a file. +RM = /usr/bin/cmake -E remove -f + +# The program to use to edit the cache. +CMAKE_EDIT_COMMAND = /usr/bin/ccmake + +# The top-level source directory on which CMake was run. +CMAKE_SOURCE_DIR = /home/peri4/pprojects/pip + +# The top-level build directory on which CMake was run. +CMAKE_BINARY_DIR = /home/peri4/pprojects/pip + +#============================================================================= +# Targets provided globally by CMake. + +# Special rule for the target edit_cache +edit_cache: + @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..." + /usr/bin/ccmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) +.PHONY : edit_cache + +# Special rule for the target edit_cache +edit_cache/fast: edit_cache +.PHONY : edit_cache/fast + +# Special rule for the target rebuild_cache +rebuild_cache: + @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..." + /usr/bin/cmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) +.PHONY : rebuild_cache + +# Special rule for the target rebuild_cache +rebuild_cache/fast: rebuild_cache +.PHONY : rebuild_cache/fast + +# The main all target +all: cmake_check_build_system + $(CMAKE_COMMAND) -E cmake_progress_start /home/peri4/pprojects/pip/CMakeFiles /home/peri4/pprojects/pip/CMakeFiles/progress.marks + $(MAKE) -f CMakeFiles/Makefile2 all + $(CMAKE_COMMAND) -E cmake_progress_start /home/peri4/pprojects/pip/CMakeFiles 0 +.PHONY : all + +# The main clean target +clean: + $(MAKE) -f CMakeFiles/Makefile2 clean +.PHONY : clean + +# The main clean target +clean/fast: clean +.PHONY : clean/fast + +# Prepare targets for installation. +preinstall: all + $(MAKE) -f CMakeFiles/Makefile2 preinstall +.PHONY : preinstall + +# Prepare targets for installation. +preinstall/fast: + $(MAKE) -f CMakeFiles/Makefile2 preinstall +.PHONY : preinstall/fast + +# clear depends +depend: + $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1 +.PHONY : depend + +#============================================================================= +# Target rules for targets named pip + +# Build rule for target. +pip: cmake_check_build_system + $(MAKE) -f CMakeFiles/Makefile2 pip +.PHONY : pip + +# fast build rule for target. +pip/fast: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/build +.PHONY : pip/fast + +main.o: main.cpp.o +.PHONY : main.o + +# target to build an object file +main.cpp.o: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/main.cpp.o +.PHONY : main.cpp.o + +main.i: main.cpp.i +.PHONY : main.i + +# target to preprocess a source file +main.cpp.i: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/main.cpp.i +.PHONY : main.cpp.i + +main.s: main.cpp.s +.PHONY : main.s + +# target to generate assembly for a file +main.cpp.s: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/main.cpp.s +.PHONY : main.cpp.s + +piconfig.o: piconfig.cpp.o +.PHONY : piconfig.o + +# target to build an object file +piconfig.cpp.o: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/piconfig.cpp.o +.PHONY : piconfig.cpp.o + +piconfig.i: piconfig.cpp.i +.PHONY : piconfig.i + +# target to preprocess a source file +piconfig.cpp.i: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/piconfig.cpp.i +.PHONY : piconfig.cpp.i + +piconfig.s: piconfig.cpp.s +.PHONY : piconfig.s + +# target to generate assembly for a file +piconfig.cpp.s: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/piconfig.cpp.s +.PHONY : piconfig.cpp.s + +piconsole.o: piconsole.cpp.o +.PHONY : piconsole.o + +# target to build an object file +piconsole.cpp.o: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/piconsole.cpp.o +.PHONY : piconsole.cpp.o + +piconsole.i: piconsole.cpp.i +.PHONY : piconsole.i + +# target to preprocess a source file +piconsole.cpp.i: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/piconsole.cpp.i +.PHONY : piconsole.cpp.i + +piconsole.s: piconsole.cpp.s +.PHONY : piconsole.s + +# target to generate assembly for a file +piconsole.cpp.s: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/piconsole.cpp.s +.PHONY : piconsole.cpp.s + +piethernet.o: piethernet.cpp.o +.PHONY : piethernet.o + +# target to build an object file +piethernet.cpp.o: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/piethernet.cpp.o +.PHONY : piethernet.cpp.o + +piethernet.i: piethernet.cpp.i +.PHONY : piethernet.i + +# target to preprocess a source file +piethernet.cpp.i: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/piethernet.cpp.i +.PHONY : piethernet.cpp.i + +piethernet.s: piethernet.cpp.s +.PHONY : piethernet.s + +# target to generate assembly for a file +piethernet.cpp.s: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/piethernet.cpp.s +.PHONY : piethernet.cpp.s + +pifile.o: pifile.cpp.o +.PHONY : pifile.o + +# target to build an object file +pifile.cpp.o: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/pifile.cpp.o +.PHONY : pifile.cpp.o + +pifile.i: pifile.cpp.i +.PHONY : pifile.i + +# target to preprocess a source file +pifile.cpp.i: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/pifile.cpp.i +.PHONY : pifile.cpp.i + +pifile.s: pifile.cpp.s +.PHONY : pifile.s + +# target to generate assembly for a file +pifile.cpp.s: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/pifile.cpp.s +.PHONY : pifile.cpp.s + +pikbdlistener.o: pikbdlistener.cpp.o +.PHONY : pikbdlistener.o + +# target to build an object file +pikbdlistener.cpp.o: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/pikbdlistener.cpp.o +.PHONY : pikbdlistener.cpp.o + +pikbdlistener.i: pikbdlistener.cpp.i +.PHONY : pikbdlistener.i + +# target to preprocess a source file +pikbdlistener.cpp.i: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/pikbdlistener.cpp.i +.PHONY : pikbdlistener.cpp.i + +pikbdlistener.s: pikbdlistener.cpp.s +.PHONY : pikbdlistener.s + +# target to generate assembly for a file +pikbdlistener.cpp.s: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/pikbdlistener.cpp.s +.PHONY : pikbdlistener.cpp.s + +piprotocol.o: piprotocol.cpp.o +.PHONY : piprotocol.o + +# target to build an object file +piprotocol.cpp.o: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/piprotocol.cpp.o +.PHONY : piprotocol.cpp.o + +piprotocol.i: piprotocol.cpp.i +.PHONY : piprotocol.i + +# target to preprocess a source file +piprotocol.cpp.i: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/piprotocol.cpp.i +.PHONY : piprotocol.cpp.i + +piprotocol.s: piprotocol.cpp.s +.PHONY : piprotocol.s + +# target to generate assembly for a file +piprotocol.cpp.s: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/piprotocol.cpp.s +.PHONY : piprotocol.cpp.s + +piserial.o: piserial.cpp.o +.PHONY : piserial.o + +# target to build an object file +piserial.cpp.o: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/piserial.cpp.o +.PHONY : piserial.cpp.o + +piserial.i: piserial.cpp.i +.PHONY : piserial.i + +# target to preprocess a source file +piserial.cpp.i: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/piserial.cpp.i +.PHONY : piserial.cpp.i + +piserial.s: piserial.cpp.s +.PHONY : piserial.s + +# target to generate assembly for a file +piserial.cpp.s: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/piserial.cpp.s +.PHONY : piserial.cpp.s + +pistring.o: pistring.cpp.o +.PHONY : pistring.o + +# target to build an object file +pistring.cpp.o: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/pistring.cpp.o +.PHONY : pistring.cpp.o + +pistring.i: pistring.cpp.i +.PHONY : pistring.i + +# target to preprocess a source file +pistring.cpp.i: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/pistring.cpp.i +.PHONY : pistring.cpp.i + +pistring.s: pistring.cpp.s +.PHONY : pistring.s + +# target to generate assembly for a file +pistring.cpp.s: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/pistring.cpp.s +.PHONY : pistring.cpp.s + +pithread.o: pithread.cpp.o +.PHONY : pithread.o + +# target to build an object file +pithread.cpp.o: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/pithread.cpp.o +.PHONY : pithread.cpp.o + +pithread.i: pithread.cpp.i +.PHONY : pithread.i + +# target to preprocess a source file +pithread.cpp.i: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/pithread.cpp.i +.PHONY : pithread.cpp.i + +pithread.s: pithread.cpp.s +.PHONY : pithread.s + +# target to generate assembly for a file +pithread.cpp.s: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/pithread.cpp.s +.PHONY : pithread.cpp.s + +pitimer.o: pitimer.cpp.o +.PHONY : pitimer.o + +# target to build an object file +pitimer.cpp.o: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/pitimer.cpp.o +.PHONY : pitimer.cpp.o + +pitimer.i: pitimer.cpp.i +.PHONY : pitimer.i + +# target to preprocess a source file +pitimer.cpp.i: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/pitimer.cpp.i +.PHONY : pitimer.cpp.i + +pitimer.s: pitimer.cpp.s +.PHONY : pitimer.s + +# target to generate assembly for a file +pitimer.cpp.s: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/pitimer.cpp.s +.PHONY : pitimer.cpp.s + +pivariable.o: pivariable.cpp.o +.PHONY : pivariable.o + +# target to build an object file +pivariable.cpp.o: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/pivariable.cpp.o +.PHONY : pivariable.cpp.o + +pivariable.i: pivariable.cpp.i +.PHONY : pivariable.i + +# target to preprocess a source file +pivariable.cpp.i: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/pivariable.cpp.i +.PHONY : pivariable.cpp.i + +pivariable.s: pivariable.cpp.s +.PHONY : pivariable.s + +# target to generate assembly for a file +pivariable.cpp.s: + $(MAKE) -f CMakeFiles/pip.dir/build.make CMakeFiles/pip.dir/pivariable.cpp.s +.PHONY : pivariable.cpp.s + +# Help Target +help: + @echo "The following are some of the valid targets for this Makefile:" + @echo "... all (the default if no target is provided)" + @echo "... clean" + @echo "... depend" + @echo "... edit_cache" + @echo "... pip" + @echo "... rebuild_cache" + @echo "... main.o" + @echo "... main.i" + @echo "... main.s" + @echo "... piconfig.o" + @echo "... piconfig.i" + @echo "... piconfig.s" + @echo "... piconsole.o" + @echo "... piconsole.i" + @echo "... piconsole.s" + @echo "... piethernet.o" + @echo "... piethernet.i" + @echo "... piethernet.s" + @echo "... pifile.o" + @echo "... pifile.i" + @echo "... pifile.s" + @echo "... pikbdlistener.o" + @echo "... pikbdlistener.i" + @echo "... pikbdlistener.s" + @echo "... piprotocol.o" + @echo "... piprotocol.i" + @echo "... piprotocol.s" + @echo "... piserial.o" + @echo "... piserial.i" + @echo "... piserial.s" + @echo "... pistring.o" + @echo "... pistring.i" + @echo "... pistring.s" + @echo "... pithread.o" + @echo "... pithread.i" + @echo "... pithread.s" + @echo "... pitimer.o" + @echo "... pitimer.i" + @echo "... pitimer.s" + @echo "... pivariable.o" + @echo "... pivariable.i" + @echo "... pivariable.s" +.PHONY : help + + + +#============================================================================= +# Special targets to cleanup operation of make. + +# Special rule to run CMake to check the build system integrity. +# No rule that depends on this can have commands that come from listfiles +# because they might be regenerated. +cmake_check_build_system: + $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0 +.PHONY : cmake_check_build_system + diff --git a/main.cpp b/main.cpp index 6c3d4b45..d0c7119f 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,10 @@ #include "pip.h" int main(int argc, char * argv[]) { - cout << PIString("Hello PIP!") << endl; + PIMathMatrix<3, 2> m0(1., 2., 3., + 4., 5., 6.); + PIMathMatrix<3, 2> m1(1., -2., 3., + -4., 5., -6.); + m0 += m1; + cout << m0 << endl; }; diff --git a/piconfig.cpp b/piconfig.cpp index b8a44937..742e1404 100644 --- a/piconfig.cpp +++ b/piconfig.cpp @@ -1,8 +1,8 @@ #include "piconfig.h" -PIConfig::PIConfig(const PIString & path): PIFile(path) { - if (!isOpened()) +PIConfig::PIConfig(const PIString & path, Flags mode): PIFile(path, mode) { + if (!isOpened() && (mode[Write] || mode[New])) open(path, Read | Write | New); parse(); } @@ -110,14 +110,14 @@ void PIConfig::insertLine(uint number, const PIString & name, const PIString & v addLine(name, value, type); return; } - settname.insert(settname.begin() + number, name); - settval.insert(settval.begin() + number, value); - settcom.insert(settcom.begin() + number, ""); - setttab.insert(setttab.begin() + number, setttab[number]); - setttype.insert(setttype.begin() + number, type); - settlines.insert(settlines.begin() + number, settlines[number]); + settname.insert(number, name); + settval.insert(number, value); + settcom.insert(number, ""); + setttab.insert(number, setttab[number]); + setttype.insert(number, type); + settlines.insert(number, settlines[number]); for (uint i = number + 1; i < settlines.size(); i++) settlines[i]++; - all.insert(all.begin() + settlines[number], name + " = " + value + " #" + type); + all.insert(settlines[number], name + " = " + value + " #" + type); flush(); writeAll(); } diff --git a/piconfig.h b/piconfig.h index 3c617016..d465a719 100644 --- a/piconfig.h +++ b/piconfig.h @@ -6,7 +6,7 @@ class PIConfig: public PIFile { public: - PIConfig(const PIString & path); + PIConfig(const PIString & path, Flags mode = Read | Write); ~PIConfig() {;} PIString getValue(const PIString & vname, const PIString & def = "", bool * exist = 0) const; @@ -58,13 +58,13 @@ public: private: void parse(); - vector settname; - vector settval; - vector settcom; - vector setttab; - vector setttype; - vector all; - vector settlines; + PIVector settname; + PIVector settval; + PIVector settcom; + PIVector setttab; + PIVector setttype; + PIVector all; + PIVector settlines; uint lines; }; diff --git a/piethernet.cpp b/piethernet.cpp index 53c2c222..13a0046e 100644 --- a/piethernet.cpp +++ b/piethernet.cpp @@ -13,30 +13,46 @@ PIEthernet::PIEthernet(PIString ip, int port, void * data_, EthernetFunc slot): WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); #endif - sock = socket(PF_INET, SOCK_DGRAM, 0); - sock_s = socket(PF_INET, SOCK_DGRAM, 0); - if (sock == -1 || sock_s == -1) - cout << "[PIEthernet] Unable to create socket" << endl; + initSend(); } PIEthernet::~PIEthernet() { - if (isRunning()) { - stop(); - pthread_cancel(thread); - } - if (sock != -1) { - shutdown(sock, SHUT_RDWR); - close(sock); - } - if (sock_s != -1) { - shutdown(sock_s, SHUT_RDWR); - close(sock_s); - } + terminate(); #ifdef WINDOWS WSACleanup(); #endif delete buffer; + if (sock != -1) { + shutdown(sock, SHUT_RDWR); + close(sock); + sock = -1; + } + if (sock_s != -1) { + shutdown(sock_s, SHUT_RDWR); + close(sock_s); + sock_s = -1; + } +} + + +void PIEthernet::terminate() { + if (!initialized()) return; + if (isRunning()) { + stop(); + pthread_cancel(thread); + } + /*if (sock != -1) { + shutdown(sock, SHUT_RDWR); + shutdown(sock, SHUT_RDWR); + close(sock); + sock = -1; + } + if (sock_s != -1) { + shutdown(sock_s, SHUT_RDWR); + close(sock_s); + sock_s = -1; + }*/ } @@ -65,26 +81,29 @@ void PIEthernet::run() { void PIEthernet::end() { - if (sock != -1) { - shutdown(sock, SHUT_RDWR); - close(sock); - sock = -1; - } - if (sock_s != -1) { - shutdown(sock_s, SHUT_RDWR); - close(sock_s); - sock_s = -1; - } - //cout << "[PIEthernet] end" << endl; + terminate(); } bool PIEthernet::init() { - addr_.sin_addr.s_addr = inet_addr(ip_.data()); + addr_.sin_addr.s_addr = inet_addr(ip_.stdString().data()); addr_.sin_family = PF_INET; addr_.sin_port = htons(port_); + close(sock); + sock = socket(PF_INET, SOCK_DGRAM, 0); if (bind(sock, (sockaddr * )&addr_, sizeof(addr_)) == -1) { - cout << "[PIEthernet] Cant`t bind to " << ip_ << ":" << port_ << endl; + cout << "[PIEthernet] Cant`t bind to " << ip_.stdString() << ":" << port_ << endl; + return false; + } + return true; +} + + +bool PIEthernet::initSend() { + close(sock_s); + sock_s = socket(PF_INET, SOCK_DGRAM, 0); + if (sock_s == -1) { + cout << "[PIEthernet] Unable to create socket" << endl; return false; } return true; @@ -97,7 +116,7 @@ bool PIEthernet::send(PIString ip, int port, char * data, int size) { return false; } saddr_.sin_port = htons(port); - saddr_.sin_addr.s_addr = inet_addr(ip.data()); + saddr_.sin_addr.s_addr = inet_addr(ip.stdString().data()); saddr_.sin_family = PF_INET; wrote = sendto(sock_s, data, size, 0, (sockaddr * )&saddr_, sizeof(saddr_)); if (wrote != size) { @@ -115,7 +134,7 @@ bool PIEthernet::send(char * data, int size) { return false; } saddr_.sin_port = htons(port_s); - saddr_.sin_addr.s_addr = inet_addr(ip_s.data()); + saddr_.sin_addr.s_addr = inet_addr(ip_s.stdString().data()); saddr_.sin_family = PF_INET; //cout << "[PIEthernet] sending in " << sock_s << endl; wrote = sendto(sock_s, data, size, 0, (sockaddr * )&saddr_, sizeof(saddr_)); diff --git a/piethernet.h b/piethernet.h index ddb6cc00..287432ca 100644 --- a/piethernet.h +++ b/piethernet.h @@ -20,17 +20,20 @@ class PIEthernet: public PIThread { public: // slot is any function format "bool (void*, char*)" - PIEthernet(PIString ip, int port, void * data, EthernetFunc slot = 0); + PIEthernet(PIString ip = "", int port = 0, void * data = 0, EthernetFunc slot = 0); ~PIEthernet(); void setSlot(EthernetFunc func) {ret_func = func;} + void setData(void * d) {data = d;} void setReadAddress(PIString ip, int port) {ip_ = ip; port_ = port;} void setSendAddress(PIString ip, int port) {ip_s = ip; port_s = port;} bool send(PIString ip, int port, char * data, int size); bool send(char * data, int size); bool init(); + bool initSend(); bool initialized() const {return sock != -1;} + void terminate(); private: void begin(); diff --git a/pifile.cpp b/pifile.cpp index 1b976227..669c4ea1 100644 --- a/pifile.cpp +++ b/pifile.cpp @@ -4,12 +4,13 @@ bool PIFile::open(const PIString & path_, Flags mode_) { cpath = path_; cmode = mode_; + string st = cpath.stdString(); if (cmode[New]) { - stream.open(cpath.stdString().c_str(), fstream::in | fstream::out | fstream::trunc); + stream.open(st.c_str(), fstream::in | fstream::out | fstream::trunc | fstream::binary); stream.close(); cmode &= ~New; - stream.open(cpath.stdString().c_str(), (fstream::openmode)(int)cmode | fstream::binary); - } else stream.open(cpath.stdString().c_str(), (fstream::openmode)(int)cmode | fstream::binary); + stream.open(st.c_str(), (fstream::openmode)(int)cmode | fstream::binary); + } else stream.open(st.c_str(), (fstream::openmode)(int)cmode | fstream::binary); return isOpened(); } diff --git a/pifile.h b/pifile.h index 40053045..dfb828b4 100644 --- a/pifile.h +++ b/pifile.h @@ -11,17 +11,21 @@ class PIFile { public: PIFile() {;} + enum Mode {Read = fstream::in, Write = fstream::out, Truncate = fstream::trunc, New = fstream::app}; + PIFile(const PIString & path, Flags mode = Read | Write) {open(path, mode);} + PIFile(const PIFile & file) {cpath = file.cpath; cmode = file.cmode;} ~PIFile() {if (isOpened()) close();} bool open(const PIString & path, Flags mode = Read | Write); - void close() {stream.close();} - void clear() {close(); stream.open(cpath.stdString().c_str(), fstream::trunc | (fstream::openmode)(int)cmode);} + inline void close() {stream.close();} + inline void clear() {string st = cpath.stdString(); close(); stream.open(st.c_str(), fstream::trunc | fstream::binary | (fstream::openmode)(int)cmode);} void seek(int position); + inline void seekToEnd() {stream.seekg(0, fstream::end);} void resize(int new_size, char fill = 0); - void fill(char c) {stream.fill(c);} - void flush() {stream.flush();} + inline void fill(char c) {stream.fill(c);} + inline void flush() {stream.flush();} PIString readLine(); PIString path() const {return cpath;} diff --git a/pigeometry.h b/pigeometry.h new file mode 100644 index 00000000..ceead339 --- /dev/null +++ b/pigeometry.h @@ -0,0 +1,90 @@ +#ifndef PIGEOMETRY_H +#define PIGEOMETRY_H + +#include "pimath.h" + +template +class PIPoint { +public: + Type x; + Type y; + + PIPoint() {x = y = 0;}; + PIPoint(Type x_, Type y_) {set(x_, y_);} + + inline void set(Type x_, Type y_) {x = x_; y = y_;} + inline double angleRad() const {return atan2(y, x);} + inline int angleDeg() const {return round(atan2(y, x) * 180. / M_PI);} + + inline PIPoint operator +(const PIPoint & p) {return PIPoint(x + p.x, y + p.y);} + inline PIPoint operator +(const Type & p) {return PIPoint(x + p, y + p);} + inline PIPoint operator -(const PIPoint & p) {return PIPoint(x - p.x, y - p.y);} + inline PIPoint operator -(const Type & p) {return PIPoint(x - p, y - p);} + inline PIPoint operator -() {return PIPoint(-x, -y);} + inline PIPoint operator *(const Type & d) {return PIPoint(x * d, y * d);} + inline PIPoint operator /(const Type & d) {return PIPoint(x / d, y / d);} + inline bool operator ==(const PIPoint & p) const {return (x == p.x && y == p.y);} + inline bool operator !=(const PIPoint & p) const {return (x != p.x || y != p.y);} +}; + +template +class PIRect { +public: + Type x0; + Type y0; + Type x1; + Type y1; + + PIRect() {x0 = y0 = x1 = y1 = 0;}; + PIRect(Type x, Type y, Type w, Type h) {set(x, y, w, h);} + PIRect(const PIPoint & tl, const PIPoint & br) {set(tl.x, tl.y, br.x, br.y);} + + inline void set(Type x, Type y, Type w, Type h) {x0 = x; y0 = y; x1 = x + w; y1 = y + h;} + inline bool pointIn(Type x, Type y) const {return (x <= x1 && x >= x0 && y <= y1 && y >= y0);} + inline bool pointIn(const PIPoint & p) const {return pointIn(p.x, p.y);} + inline bool isEmpty() const {return (x1 - x0 == 0 && y1 - y0 == 0);} + inline PIRect & translate(Type x, Type y) {x0 += x; x1 += x; y0 += y; y1 += y; return *this;} + inline PIRect & translate(const PIPoint & p) {x0 += p.x; x1 += p.x; y0 += p.y; y1 += p.y; return *this;} + inline PIRect translated(Type x, Type y) {PIRect r(*this); r.translate(x, y); return r;} + inline PIRect translated(const PIPoint & p) {PIRect r(*this); r.translate(p); return r;} + inline PIRect & scale(Type x, Type y) {setWidth(width() * x); setHeight(height() * y); return *this;} + inline PIRect & scale(const PIPoint & p) {setWidth(width() * p.x); setHeight(height() * p.y); return *this;} + inline PIRect scaled(Type x, Type y) {PIRect r(*this); r.scale(x, y); return r;} + inline PIRect scaled(const PIPoint & p) {PIRect r(*this); r.scale(p); return r;} + //inline void unite(const PIRect & r) {;} + //inline PIRect & united(const PIRect & r) {unite(r); return *this;} + inline Type top() const {return y0;} + inline Type left() const {return x0;} + inline Type right() const {return x1;} + inline Type bottom() const {return y1;} + inline Type width() const {return x1 - x0;} + inline Type height() const {return y1 - y0;} + inline PIPoint topLeft() {return PIPoint(x0, y0);} + inline PIPoint topRigth() {return PIPoint(x1, y0);} + inline PIPoint bottomLeft() {return PIPoint(x0, y1);} + inline PIPoint bottomRight() {return PIPoint(x1, y1);} + inline void setTop(Type v) {y0 = v;} + inline void setLeft(Type v) {x0 = v;} + inline void setRigth(Type v) {x1 = v;} + inline void setBottom(Type v) {y1 = v;} + inline void setWidth(Type v) {x1 = x0 + v;} + inline void setHeight(Type v) {y1 = y0 + v;} + + inline PIRect operator -() {return PIRect(-x0, -y0, -width(), -height());} + inline void operator +=(Type x) {translate(x, x);} + inline void operator +=(const PIPoint & p) {translate(p);} + inline void operator -=(Type x) {translate(-x, -x);} + inline void operator -=(const PIPoint & p) {translate(-p);} + inline void operator *=(Type p) {x0 *= p; x1 *= p; y0 *= p; y1 *= p;} + inline void operator /=(Type p) {x0 /= p; x1 /= p; y0 /= p; y1 /= p;} +}; + +typedef PIPoint PIPointi; +typedef PIPoint PIPointf; +typedef PIPoint PIPointd; + +typedef PIRect PIRecti; +typedef PIRect PIRectf; +typedef PIRect PIRectd; + +#endif // PIGEOMETRY_H diff --git a/piincludes.h b/piincludes.h index 5259ef07..4229160a 100644 --- a/piincludes.h +++ b/piincludes.h @@ -44,6 +44,20 @@ using std::list; using std::deque; using std::string; +template > +class PIVector: public vector { +public: + inline const Type & at(uint index) const {return (*this)[index];} + inline Type & at(uint index) {return (*this)[index];} + inline const Type * data(uint index = 0) const {return &(*this)[index];} + inline Type * data(uint index = 0) {return &(*this)[index];} + inline void pop_front() {vector::erase(vector::begin());} + inline void push_front(const Type & t) {vector::insert(vector::begin(), t);} + inline void remove(uint num) {vector::erase(vector::begin() + num);} + inline void remove(uint num, uint count) {vector::erase(vector::begin() + num, vector::begin() + num + count);} + inline void insert(uint pos, const Type & t) {vector::insert(vector::begin() + pos, t);} +}; + template class Flags { private: @@ -70,6 +84,9 @@ public: inline operator int() const {return flags;} }; +#ifdef WINDOWS +inline int random() {return rand();} +#endif inline bool atob(const string & str) { return str == "1" ? true : false;}; inline string btos(const bool num) { return num ? "0" : "1";}; diff --git a/pimath.h b/pimath.h new file mode 100644 index 00000000..d38587ad --- /dev/null +++ b/pimath.h @@ -0,0 +1,153 @@ +#ifndef PIMathATH_H +#define PIMathATH_H + +#include +#include +#include +#include "piincludes.h" + +using std::complex; + +typedef complex complexi; +typedef complex complexf; +typedef complex complexd; +typedef complex complexld; + +const double deg2rad = atan(1) / 45.; +const double rad2deg = 45. / atan(1); + +inline int pow2(int p) {return (int)1 << p;} +template inline int pimin(const Type & f, const Type & s) {return (f > s) ? s : f;} +template inline int pimax(const Type & f, const Type & s) {return (f < s) ? s : f;} + +/// Vector + +#define PIMV_FOR(v, s) for (uint v = s; v < Size; ++v) + +template +class PIMathVector { + typedef PIMathVector _CVector; +public: + PIMathVector() {resize(Size);} + PIMathVector(Type fval, ...) {resize(Size); c[0] = fval; va_list vl; va_start(vl, fval); PIMV_FOR(i, 1) c[i] = va_arg(vl, Type); va_end(vl);} + + inline uint size() const {return Size;} + inline _CVector & fill(const Type & v) {PIMV_FOR(i, 0) c[i] = v; return *this;} + inline _CVector & set(Type fval, ...) {c[0] = fval; va_list vl; va_start(vl, fval); PIMV_FOR(i, 1) c[i] = va_arg(vl, Type); va_end(vl); return *this;} + inline _CVector & move(const Type & v) {PIMV_FOR(i, 0) c[i] += v; return *this;} + inline Type lengthSqr() const {Type tv(0); PIMV_FOR(i, 0) tv += c[i] * c[i]; return tv;} + inline Type length() const {return sqrt(lengthSqr());} + inline Type manhattanLength() const {Type tv(0); PIMV_FOR(i, 0) tv += fabs(c[i]); return tv;} + inline Type angleCos(const _CVector & v) const {Type tv = v.length() * length(); return (tv == Type(0) ? Type(0) : ((*this) ^ v) / tv);} + inline Type angleSin(const _CVector & v) const {Type tv = angleCos(v); return sqrt(Type(1) - tv * tv);} + inline Type angleRad(const _CVector & v) const {return acos(angleCos(v));} + inline Type angleDeg(const _CVector & v) const {return acos(angleCos(v)) * rad2deg;} + inline _CVector projection(const _CVector & v) {Type tv = v.length(); return (tv == Type(0) ? _CVector() : v * (((*this) ^ v) / tv));} + inline void normalize() {Type tv = length(); if (tv == Type(1)) return; PIMV_FOR(i, 0) c[i] /= tv;} + inline bool isNull() const {PIMV_FOR(i, 0) if (c[i] != Type(0)) return false; return true;} + inline bool isOrtho(const _CVector & v) const {return ((*this) ^ v) == Type(0);} + + inline Type & at(uint index) {return c[index];} + inline Type at(uint index) const {return c[index];} + inline Type & operator [](uint index) {return c[index];} + inline Type operator [](uint index) const {return c[index];} + inline void operator =(const _CVector & v) {c = v.c;} + inline bool operator ==(const _CVector & v) const {PIMV_FOR(i, 0) if (c[i] != v[i]) return false; return true;} + inline bool operator !=(const _CVector & v) const {return !(*this == c);} + inline void operator +=(const _CVector & v) {PIMV_FOR(i, 0) c[i] += v[i];} + inline void operator -=(const _CVector & v) {PIMV_FOR(i, 0) c[i] -= v[i];} + inline void operator *=(const Type & v) {PIMV_FOR(i, 0) c[i] *= v;} + inline void operator /=(const Type & v) {PIMV_FOR(i, 0) c[i] /= v;} + inline _CVector operator -() {_CVector tv; PIMV_FOR(i, 0) tv[i] = -c[i]; return tv;} + inline _CVector operator +(const _CVector & v) {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] += v[i]; return tv;} + inline _CVector operator -(const _CVector & v) {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] -= v[i]; return tv;} + inline _CVector operator *(const Type & v) {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] *= v; return tv;} + inline _CVector operator /(const Type & v) {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] /= v; return tv;} + inline Type operator ^(const _CVector & v) const {Type tv(0); PIMV_FOR(i, 0) tv += c[i] * v[i]; return tv;} + +private: + inline void resize(uint size, const Type & new_value = Type()) {c.resize(size, new_value);} + PIVector c; + +}; + +template +inline std::ostream & operator <<(std::ostream & s, const PIMathVector & v) {s << '{'; PIMV_FOR(i, 0) {s << v[i]; if (i < Size - 1) s << ", ";} s << '}'; return s;} + +typedef PIMathVector<2u, int> PIMathVector2i; +typedef PIMathVector<3u, int> PIMathVector3i; +typedef PIMathVector<4u, int> PIMathVector4i; +typedef PIMathVector<2u, double> PIMathVector2d; +typedef PIMathVector<3u, double> PIMathVector3d; +typedef PIMathVector<4u, double> PIMathVector4d; + +/// Matrix + +#define PIMM_FOR(c, r) for (uint c = 0; c < Cols; ++c) { for (uint r = 0; r < Rows; ++r) { +#define PIMM_FOR_WB(c, r) for (uint c = 0; c < Cols; ++c) for (uint r = 0; r < Rows; ++r) // without brakes +#define PIMM_FOR_I(c, r) for (uint r = 0; r < Rows; ++r) { for (uint c = 0; c < Cols; ++c) { +#define PIMM_FOR_I_WB(c, r) for (uint r = 0; r < Rows; ++r) for (uint c = 0; c < Cols; ++c) // without brakes +#define PIMM_FOR_C(v) for (uint v = 0; v < Cols; ++v) +#define PIMM_FOR_R(v) for (uint v = 0; v < Rows; ++v) + +template +class PIMathMatrix { + typedef PIMathMatrix _CMatrix; + typedef PIMathMatrix _CMatrixI; + typedef PIMathVector _CMCol; + typedef PIMathVector _CMRow; +public: + PIMathMatrix() {resize(Cols, Rows);} + PIMathMatrix(Type fval, ...) {resize(Cols, Rows); va_list vl; va_start(vl, fval); PIMM_FOR_I_WB(c, r) m[c][r] = (r + c == 0 ? fval : va_arg(vl, Type)); va_end(vl);} + + inline uint cols() const {return Cols;} + inline uint rows() const {return Rows;} + inline _CMCol col(uint index) {_CMCol tv; PIMM_FOR_R(i) tv[i] = m[index][i]; return tv;} + inline _CMRow row(uint index) {_CMRow tv; PIMM_FOR_C(i) tv[i] = m[i][index]; return tv;} + inline _CMatrix & setCol(uint index, const _CMCol & v) {PIMM_FOR_R(i) m[index][i] = v[i]; return *this;} + inline _CMatrix & setRow(uint index, const _CMRow & v) {PIMM_FOR_C(i) m[i][index] = v[i]; return *this;} + inline _CMatrix & fill(const Type & v) {PIMM_FOR_WB(c, r) m[c][r] = v; return *this;} + //inline _CMatrix & set(Type fval, ...) {m[0] = fval; va_list vl; va_start(vl, fval); PIMV_FOR(i, 1) m[i] = va_arg(vl, Type); va_end(vl); return *this;} + //inline void normalize() {Type tv = length(); if (tv == Type(1)) return; PIMV_FOR(i, 0) m[i] /= tv;} + inline bool isSquare() const {return cols() == rows();} + inline bool isIdentity() const {PIMM_FOR_WB(c, r) if ((c == r) ? m[c][r] != Type(1) : m[c][r] != Type(0)) return false; return true;} + inline bool isNull() const {PIMM_FOR_WB(c, r) if (m[c][r] != Type(0)) return false; return true;} + + inline Type & at(uint col, uint row) {return m[col][row];} + inline Type at(uint col, uint row) const {return m[col][row];} + inline PIVector & operator [](uint col) {return m[col];} + inline PIVector operator [](uint col) const {return m[col];} + inline void operator =(const _CMatrix & sm) {m = sm.m;} + inline bool operator ==(const _CMatrix & sm) const {PIMM_FOR_WB(c, r) if (m[c][r] != sm.m[c][r]) return false; return true;} + inline bool operator !=(const _CMatrix & sm) const {return !(*this == sm);} + inline void operator +=(const _CMatrix & sm) {PIMM_FOR_WB(c, r) m[c][r] += sm.m[c][r];} + inline void operator -=(const _CMatrix & sm) {PIMM_FOR_WB(c, r) m[c][r] -= sm.m[c][r];} + inline void operator *=(const Type & v) {PIMM_FOR_WB(c, r) m[c][r] *= v;} + inline void operator /=(const Type & v) {PIMM_FOR_WB(c, r) m[c][r] /= v;} + inline _CMatrix operator -() {_CMatrix tm; PIMM_FOR_WB(c, r) tm.m[c][r] = -m[c][r]; return tm;} + inline _CMatrix operator +(const _CMatrix & sm) {_CMatrix tm = _CMatrix(*this); PIMM_FOR_WB(c, r) tm.m[c][r] += sm.m[c][r]; return tm;} + inline _CMatrix operator -(const _CMatrix & sm) {_CMatrix tm = _CMatrix(*this); PIMM_FOR_WB(c, r) tm.m[c][r] -= sm.m[c][r]; return tm;} + inline _CMatrix operator *(const Type & v) {_CMatrix tm = _CMatrix(*this); PIMM_FOR_WB(c, r) tm.m[c][r] *= v; return tm;} + inline _CMatrix operator /(const Type & v) {_CMatrix tm = _CMatrix(*this); PIMM_FOR_WB(c, r) tm.m[c][r] /= v; return tm;} + +private: + inline void resize(uint cols, uint rows, const Type & new_value = Type()) {m.resize(cols); PIMM_FOR_C(i) m[i].resize(rows, new_value);} + PIVector > m; + +}; + +template +inline std::ostream & operator <<(std::ostream & s, const PIMathMatrix & m) {s << '{'; PIMM_FOR_I(c, r) s << m[c][r]; if (c < Cols - 1 || r < Rows - 1) s << ", ";} if (r < Rows - 1) s << endl;} s << '}'; return s;} + +typedef PIMathMatrix<2u, 2u, int> PIMathMatrix22i; +typedef PIMathMatrix<3u, 3u, int> PIMathMatrix33i; +typedef PIMathMatrix<4u, 4u, int> PIMathMatrix44i; +typedef PIMathMatrix<2u, 2u, double> PIMathMatrix22d; +typedef PIMathMatrix<3u, 3u, double> PIMathMatrix33d; +typedef PIMathMatrix<4u, 4u, double> PIMathMatrix44d; + +#undef PIMV_FOR +#undef PIMM_FOR +#undef PIMM_FOR_WB + +#endif // PIMathATH_H diff --git a/pip.h b/pip.h index d5966f8f..beeb7f18 100644 --- a/pip.h +++ b/pip.h @@ -1,4 +1,6 @@ #include "pitimer.h" #include "pivariable.h" #include "piconsole.h" +#include "pigeometry.h" +#include "pimath.h" #include "piprotocol.h" diff --git a/piserial.cpp b/piserial.cpp index 79ec2f8a..10c08f5e 100644 --- a/piserial.cpp +++ b/piserial.cpp @@ -161,7 +161,7 @@ void PISerial::end() { bool PISerial::init() { #ifdef WINDOWS - hCom = CreateFile(devName.stdString().c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, 0); + hCom = CreateFileA(devName.stdString().c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, 0); if(hCom == INVALID_HANDLE_VALUE) { cout << "[PISerial] Unable to open " << devName << endl; return false; diff --git a/pistring.cpp b/pistring.cpp index 5677d864..c468b01c 100644 --- a/pistring.cpp +++ b/pistring.cpp @@ -71,11 +71,11 @@ PIString & PIString::cutMid(const int start, const int len) { s = 0; } if (l < 0) - erase(begin() + s, end()); + remove(s, size() - s); else { if (l > length() - s) l = length() - s; - erase(begin() + s, begin() + s + l); + remove(s, s + l); } return *this; } diff --git a/pistring.h b/pistring.h index 54bf69c3..90a09dd3 100644 --- a/pistring.h +++ b/pistring.h @@ -3,7 +3,7 @@ #include "piincludes.h" -class PIString: public vector +class PIString: public PIVector { public: PIString() {;} diff --git a/pivariable.cpp b/pivariable.cpp index 438118a9..028a62a9 100644 --- a/pivariable.cpp +++ b/pivariable.cpp @@ -111,7 +111,7 @@ void PIVariable::writeVariable(char * dest) { void PIStruct::parseFile(const PIString & file) { - PIConfig conf(file); + PIConfig conf(file, PIFile::Read); PIVariable var; PIString ts; uint sz = 0; diff --git a/pivariable.h b/pivariable.h index 7c5d72d7..3a41188f 100644 --- a/pivariable.h +++ b/pivariable.h @@ -90,7 +90,7 @@ public: private: uint size_; PIString name_; - vector vars; + PIVector vars; };