From 3fd4e0182afec4fe3548689c25a74fe11735d066 Mon Sep 17 00:00:00 2001 From: MatMasIt Date: Wed, 6 Oct 2021 17:50:47 +0200 Subject: [PATCH] Add files via upload --- README.md | 30 ++++++++++++ arabic.txt | 4 ++ install.sh | 16 ++++++ roman.txt | 4 ++ ropipe | Bin 0 -> 17120 bytes ropipe.1 | 18 +++++++ ropipe.c | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 211 insertions(+) create mode 100644 README.md create mode 100644 arabic.txt create mode 100644 install.sh create mode 100644 roman.txt create mode 100644 ropipe create mode 100644 ropipe.1 create mode 100644 ropipe.c diff --git a/README.md b/README.md new file mode 100644 index 0000000..24a1212 --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +# Ropipe +> Convert roman numbers to integers and vice-versa in pipes +## What is ropipe? +Ropipe converts decimal integers to roman numbers and vice versa, reading from stdin and printing to stdout, it is meant to primarily be used in pipes. +Default is integer to roman. +## Options +* -r : Roman to integer. Integer to roman is the default. +## Installation + 1. Clone this repository + 2. `cd` into it + 3. run + ```sh + sudo bash install.sh + ``` + (The program is compiled at this time, you may check the script and the program out beforehand) +## Examples +Once installed, you can find sample files in the project dir. +```sh +ropipe < arabic.txt +``` +```sh +ropipe -r < roman.txt +``` +## Manpage + +A manpage is installed alongside the program, try running `man ropipe` + +## Updates + +There is no automatic update system as of yet, you are advised to regularly visit https://github.com/MatMasIt/ropipe, download and re-run the installer in order to get updates \ No newline at end of file diff --git a/arabic.txt b/arabic.txt new file mode 100644 index 0000000..88df454 --- /dev/null +++ b/arabic.txt @@ -0,0 +1,4 @@ +1 +10 +123 +321 diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..12d0b15 --- /dev/null +++ b/install.sh @@ -0,0 +1,16 @@ +if [ $(id -u) -ne 0 ] + then echo "Please run as root" + exit +fi +if [ -z $(which gcc) ] + then echo "Please install gcc first" + exit +fi +echo "Compiling ropipe" +gcc -o ropipe ropipe.c +mkdir -p /usr/local/share/man/man1/ +echo "Copying man file" +cp ropipe.1 /usr/local/share/man/man1/ropipe.1 +echo "Installing binary" +cp ropipe /usr/bin/ropipe +echo "Done" \ No newline at end of file diff --git a/roman.txt b/roman.txt new file mode 100644 index 0000000..9d62580 --- /dev/null +++ b/roman.txt @@ -0,0 +1,4 @@ +IV +V +VII +MCC diff --git a/ropipe b/ropipe new file mode 100644 index 0000000000000000000000000000000000000000..051cf3352af74e544fdc96dec0d3481cb620ed3b GIT binary patch literal 17120 zcmeHOe{5UFeLsqltt2)jJ657NTg;?!V<#0YJB}mAa6?LZ@>46to+~BpTKAbIDT%cx zs^sHrcTS>0Q4vB_#{RL*e-x-)v}oG2SXQ8eiXn|0uThFMg`J{-+oGsC*qAgqnWEYS z+sWZR-@E%#kIxjc4ck8hkKp^>_w)U6ci+4B-raleo;A8UsyrUS$tyl8B)3i>K`rp_ zVGSXIqD8F5ag(@5tO8t(e^86v^iq`)Fs;>kUQn`Yq)Zw5Ckhr!xrRi^Zl!d3wURIu z^@=CE8p>+kO>49rQUmlBeM7NKr(~e@3Yf}2Ty`_}(bK0Kmh$Xu-+U|_D zW4b^ULSoACq?pjZMaR=k{g6@e%cBr(yF=Q}P206TQ!0bfC$8Ah|4DfVv|Y|G-3T|0 zDp)Y(^8O5V6qo;M;%+|3<*m>U!#e&L1mH- zC9>xsK|JMe!=H3i`_GCIjjaK_82f3WzcE#Q9q-HF;>u0E5%{cdIO2LAVz!5;>;PHYtQK_%qzFVN2s>egMtvf}Av zWcO|>9SINhflb@t$T2H2aLnop#|Oo5#*PexQzC6g<3nON6(6$uL|-gPs*w|6t1mtj z9*loJ28C24F$|P^lb4ijCBm@T*VWnDX6*<(TFmV%=5_=g5mskUw-t@0V*T;79ZU6e zw+$wTVm;xbgE4sPPb7!5H%qCORa9ffi;<&#>bgLAN*;_b8cRH2?u^IRl14&f8zk=) z$2FhX7{7%)egQlszK(JHX7qjHw7yO;en#W9L1|XJ-Y|dZafQ>Ec8>QM2QIJIN=?i< za4v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VwNxaktaVLROyWIZAfctM&7heT_Um09PQg+(T;7fyuSl+>5 z*$FE`dpOeAzY$GZ{e#J);Xx~ECsS!FoH-#P$;9wr%#K9^EzP@{%PY~wlDHL4rNSqz z*pQt%Df&|3M9hk25{Z*Aac~wi?Gn{gayUL5L#0y5M0m)ulTfi^{jn4bI}V1r4a<0< z-NJs7Qq!og+CTY3sJpYRR3bOC0K#@9%P_SEv;Cm3_I2%V4Ru-jJ32mL^jJNi)-Hp* z$}KRFbVfFUx6lNGyc6bwcD=~GHZJv8G;D|Ax^#QaveHhyH)k192)90 z@JG{WUB28bHwBgMP+GR9$cRAtWWo*~1+`O(4sebJ zOKMmIhLUzH&_9$33}ZmWQuav)bTkvkHo16IL!s8rCOh0Or20TOJs<+nlS3#%Q9Gq1 ze-TTi;zakJ)oVG!y*tH&?m$| z6yBIqKGd0^EI5y%!ihMXDRXp!2;i(t;7lubn*Vn(nvvkrhI^VyJRfGt>n|;qx|}Ea zA^d60!}dIHX6lE`nRq?K*e+z8_J{O5n(6(ls3~8+8SZr1^E{lXpA|KA+dl;yuicVd ze_mHI-JC zGLFXBpyEuT|tJ{RJ?rW^#SjTck46H z^gYp03<**C3W1im46UOE9JhgrWd$+%Jx_qt9ZUx&vSVUBY>PpE; zdF&{OItH~vzK;AUUpRd34JdW$GEY;DZg)`DWx}0;lN=j>s^7h`xl*UGvfr}OY E3H?EXQ~&?~ literal 0 HcmV?d00001 diff --git a/ropipe.1 b/ropipe.1 new file mode 100644 index 0000000..e2a71d8 --- /dev/null +++ b/ropipe.1 @@ -0,0 +1,18 @@ +.TH ROPIPE 1 "06 October 2021" +.SH NAME +ropipe - Convert roman numbers to integers and vice-versa in pipes +.SH SYNOPSIS +ropipe [ -r ] +.SH DESCRIPTION +Ropipe converts decimal integers to roman numbers and vice versa, reading from stdin and printing to stdout, it is meant to primarily be used in pipes. +Default is integer to roman. +.SS Options +.TP +-r +Roman to integer. Integer to roman is the default. +.SH FILES +.TP +/usr/bin/ropipe +The program +.SH BUGS +Send bug reports at https://github.com/MatMasIt/ropipe \ No newline at end of file diff --git a/ropipe.c b/ropipe.c new file mode 100644 index 0000000..3bced61 --- /dev/null +++ b/ropipe.c @@ -0,0 +1,139 @@ +#include +#include +#include +#define RDIGITS 14 +/* + * Returns the value of a roman numeral digit + * @param {char} c - Roman digit +*/ +static int roman_to_integer(char c) +{ + switch (c) + { + case 'I': + return 1; + case 'V': + return 5; + case 'X': + return 10; + case 'L': + return 50; + case 'C': + return 100; + case 'D': + return 500; + case 'M': + return 1000; + default: + return 0; + } +} +/* + * Returns the interger value of a roman number + * @param {string} s - Roman number +*/ +int roman_to_int(char *s) +{ + int i, int_num = roman_to_integer(s[0]); + + for (i = 1; s[i] != '\0'; i++) + { + int prev_num = roman_to_integer(s[i - 1]); + int cur_num = roman_to_integer(s[i]); + if (prev_num < cur_num) + { + int_num = int_num - prev_num + (cur_num - prev_num); + } + else + { + int_num += cur_num; + } + } + return int_num; +} +/* + * Map of roman digits symbols and their value + * @elem {string} sym - digit symbol + * @elem {int} val - digit symbol +*/ +typedef struct +{ + char *sym; + int val; +} numeral; +/* + * Returns greatest numeral index + * @elem {numeral} nu - map of roman digits symbols and their value + * @elem {int} num - number +*/ +int maxNume(numeral *nu, int num) +{ + int i, index; + for (i = 0; i < RDIGITS; i++) + { //RDIGITS numerals in array + if (nu[i].val <= num) + index = i; + } + //gretest value numeral index, not greater than number + return index; +} +/* + * Prints roman number from integer + * @elem {numeral} nu - map of roman digits symbols and their value + * @elem {int} num - number +*/ +void decToRoman(numeral *nu, int num) +{ + int max; + if (num != 0) + { + max = maxNume(nu, num); + printf("%s", nu[max].sym); + num -= nu[max].val; //decrease number + decToRoman(nu, num); //recursively print numerals + } +} +/* + * Main + * @elem {int} argc - Argument count + * @elem {string[]} argv - Strings array +*/ +int main(int argc, char *argv[]) +{ + int direction = 0; + /* + * 0: integer to roman (default) + * 1: roman to integer + */ + for (int i = 0; i < argc; i++) + { + if (strcmp(argv[i], "-r")) + { + direction = 1; + } + else + { + direction = 0; + } + } + numeral nume[RDIGITS] = {{"I", 1}, {"IV", 4}, {"V", 5}, {"IX", 9}, {"X", 10}, {"XL", 40}, {"L", 50}, {"XC", 90}, {"C", 100}, {"CD", 400}, {"D", 500}, {"CM", 900}, {"M", 1000}, {"MMMM", 4000}}; + char *str1; + int inputint = 0; + switch (direction) + { + case 0: + while (scanf("%s", &str1) && !feof(stdin)) // read integers and convert to roman + { + printf("%d\n", roman_to_int(&str1)); + } + break; + case 1: + while (scanf("%d", &inputint) && !feof(stdin)) // read roman and convert to integers + { + decToRoman(nume, inputint); + printf("\n"); + } + break; + } + return 0; +}