int host2ip(const char *server, char *ip);
int is_num(const char *moji);
-void itoa(int n, char s[]);
+int itoa(int n, char s[]);
+int reverse_char(char s[]);
int rmkdir(const char *check_dir);
int remove_path(const char *path);
int remove_special_char(const char *str, char *dst);
int safe_strcat(char *dst, const char *append);
-int safe_strncat(char *dst, const char *append, const int len);
+int safe_strncat(char *dst, const char *append, const unsigned int len);
int shmf(const char *file, char *shmfile);
#ifdef __cplusplus
--- /dev/null
+#ifndef INCLUDED_1stclass
+#define INCLUDED_1stclass
+#include <string>
+#include <iostream>
+
+using namespace std;
+
+class firstclass {
+public:
+ string host2ip(const string server);
+ int is_num(const string moji);
+ int itoa(int n, char s[]);
+ int rmkdir(const string check_dir);
+ int remove_path(const string path);
+ int remove_special_char(string &str);
+ int reverse_char(char s[]);
+ int safe_strcat(char *dst, const char *append);
+ int safe_strncat(char *dst, const char *append, const unsigned int len);
+ string shmf(const string file);
+};
+#endif
--- /dev/null
+0.1.0 Mon Sep 2010 04:00:00\r
+Initial release for public\r
-.PHONY: all
-all: lib1stclass.a
+all: lib1stclass.a libb1stclass.so lib1stclass_cxx.a lib1stclass_cxx.so t/test
-lib1stclass.a: host2ip.o rmkdir.o safe_strcat.o safe_strncat.o is_num.o itoa.o remove_path.o remove_special_char.o lib1stclass.h
- ar rvs lib1stclass.a host2ip.o itoa.o rmkdir.o safe_strcat.o safe_strncat.o is_num.o remove_path.o remove_special_char.o;
+lib1stclass_cxx.a: host2ip_cxx.o is_num_cxx.o remove_path_cxx.o remove_special_char_cxx.o rmkdir_cxx.o 1stclass.hpp safe_strcat_cxx.o safe_strncat_cxx.o shmf_cxx.o itoa_cxx.o reverse_char_cxx.o
+ ar rvs lib1stclass_cxx.a host2ip_cxx.o is_num_cxx.o remove_path_cxx.o remove_special_char_cxx.o rmkdir_cxx.o safe_strcat_cxx.o safe_strncat_cxx.o shmf_cxx.o itoa_cxx.o reverse_char_cxx.o
+ ranlib lib1stclass_cxx.a;
+
+lib1stclass_cxx.so: host2ip_cxx.o is_num_cxx.o remove_path_cxx.o remove_special_char_cxx.o rmkdir_cxx.o 1stclass.hpp safe_strcat_cxx.o safe_strncat_cxx.o shmf_cxx.o reverse_char_cxx.o
+ g++ -shared -fPIC -o lib1stclass_cxx.so host2ip_cxx.o is_num_cxx.o remove_path_cxx.o remove_special_char_cxx.o rmkdir_cxx.o safe_strcat_cxx.o safe_strncat_cxx.o shmf_cxx.o reverse_char_cxx.o
+
+lib1stclass.a: host2ip.o rmkdir.o safe_strcat.o safe_strncat.o is_num.o itoa.o remove_path.o remove_special_char.o shmf.o reverse_char.o 1stclass.h
+ ar rvs lib1stclass.a host2ip.o itoa.o rmkdir.o safe_strcat.o safe_strncat.o is_num.o remove_path.o remove_special_char.o shmf.o reverse_char.o
ranlib lib1stclass.a;
+libb1stclass.so: host2ip.o rmkdir.o safe_strcat.o safe_strncat.o is_num.o itoa.o remove_path.o remove_special_char.o shmf.o reverse_char.o 1stclass.h
+ g++ -shared -fPIC -o lib1stclass.so host2ip.o itoa.o rmkdir.o safe_strcat.o safe_strncat.o is_num.o remove_path.o remove_special_char.o shmf.o reverse_char.o
+
host2ip.o: host2ip.c
gcc -Wall -c host2ip.c;
+host2ip_cxx.o: host2ip.c
+ g++ -Wall -o host2ip_cxx.o -c host2ip.c;
+
+is_num.o: is_num.c
+ gcc -Wall -c is_num.c;
+
+is_num_cxx.o: is_num.c
+ g++ -Wall -o is_num_cxx.o -c is_num.c;
+
itoa.o: itoa.c
gcc -Wall -c itoa.c
+itoa_cxx.o: itoa.c
+ g++ -Wall -o itoa_cxx.o -c itoa.c
+
+reverse_char.o: reverse_char.c;
+ gcc -Wall -c reverse_char.c;
+
+reverse_char_cxx.o: reverse_char.c;
+ g++ -Wall -o reverse_char_cxx.o -c reverse_char.c;
+
remove_special_char.o: remove_special_char.c;
gcc -Wall -c remove_special_char.c;
+remove_special_char_cxx.o: remove_special_char.c;
+ g++ -Wall -o remove_special_char_cxx.o -c remove_special_char.c;
+
remove_path.o: remove_path.c
gcc -Wall -c remove_path.c;
+remove_path_cxx.o: remove_path.c
+ g++ -Wall -o remove_path_cxx.o -c remove_path.c;
+
rmkdir.o: rmkdir.c
gcc -Wall -c rmkdir.c;
+rmkdir_cxx.o: rmkdir.c
+ g++ -Wall -o rmkdir_cxx.o -c rmkdir.c;
+
safe_strcat.o: safe_strcat.c
gcc -Wall -c safe_strcat.c;
+safe_strcat_cxx.o: safe_strcat.c
+ g++ -Wall -o safe_strcat_cxx.o -c safe_strcat.c;
+
safe_strncat.o: safe_strncat.c
gcc -Wall -c safe_strncat.c;
-is_num.o: is_num.c
- gcc -Wall -c is_num.c;
+safe_strncat_cxx.o: safe_strncat.c
+ g++ -Wall -o safe_strncat_cxx.o -c safe_strncat.c;
+
+shmf.o: shmf.c
+ gcc -Wall -c shmf.c;
+
+shmf_cxx.o: shmf.c
+ g++ -Wall -o shmf_cxx.o -c shmf.c;
+
+t/test: t/test.cpp lib1stclass_cxx.a lib1stclass.a;
+ g++ -Wall -o t/test.o -c t/test.cpp;
+ g++ -o t/test t/test.o lib1stclass_cxx.a lib1stclass.a;
+ chmod 755 t/test;
+
-.PHONY: install
install:
mkdir -p /usr/local/lib;
cp lib1stclass.a /usr/local/lib;
- cp lib1stclass.h /usr/local/include;
+ cp lib1stclass.so /usr/local/lib;
+ cp lib1stclass_cxx.a /usr/local/lib;
+ cp lib1stclass_cxx.so /usr/local/lib;
+ cp 1stclass.h /usr/local/include;
+ cp 1stclass.hpp /usr/local/include;
+ perl add_version.pl;
-.PHONY: clean
clean:
rm *.o -f *.o;
- rm lib1stclass.a -f lib1stclass.a;
+ rm *.a -f *.a;
+ rm *.so -f *.so;
+ rm t/test -f test;
+ rm t/*.o -f t/*.o
+
+real_clean:
+ rm t/*~ -f t/*~;
+ rm *~ -f *~;
+ rm *.o -f *.o;
+ rm *.a -f *.a;
+ rm *.so -f *.so;
+ rm t/test -f test;
+ rm t/*.o -f t/*.o
+test:
+ t/test;
--- /dev/null
+[About this module]\r
+This library offers the procedure which you want to use in many cases when you are developing, especially when you are developing for web application or backend application for it.\r
+This library is created by Hajime Kurita.\r
+The name 1stclass derives from the fact that the code is used for web application of http://www.1stclass.co.jp/\r
+\r
+Example:\r
+Blog search & analysis system: http://minakoe.jp/\r
+Japanese works database: http://www.accessup.org/anime/\r
+\r
+The code is distributed on http://sourceforge.jp/projects/lib1stclass/\r
+More detailed information is avaiable on blog.\r
+Japanese: http://www.accessup.org/pj/6_B4C9CDFDBFCDA4B5A4F3/5/list.html\r
+\r
+[License]\r
+You can use this under the license of GPL ver.3.\r
+\r
+[Install]\r
+make clean;\r
+make;\r
+make test;\r
+make install;\r
+\r
+[Usage]\r
+For C,\r
+lib1stclass.so\r
+lib1stclass.a\r
+\r
+For C++,\r
+lib1stclass_cxx.so\r
+lib1stclass_cxx.a\r
+\r
+are available.\r
+\r
+You can find functions, methods and how to use this library by reading the source code of t/test.cpp.\r
--- /dev/null
+#!/usr/bin/perl\r
+use strict;\r
+use FileHandle;\r
+my $version='';\r
+if(my $fh=new FileHandle("Changes")){\r
+ while(<$fh>){\r
+ my $line=$_;\r
+ if($line=~ m!^([\d\.]+)!){\r
+ $version=$1;\r
+ }\r
+ last;\r
+ }\r
+ $fh->close();\r
+}\r
+if($version){\r
+ my @files=('/usr/local/include/lib1stclass.hpp', '/usr/local/include/lib1stclass.h');\r
+ foreach my $file (@files){\r
+ if(my $fh=new FileHandle($file)){\r
+ local $/=undef;\r
+ my $con=<$fh>;\r
+ $fh->close();\r
+ unless($con=~ m!// VERSION=[\d\.]+ //!){\r
+ if(my $fh=new FileHandle('>> '.$file)){\r
+ print $fh "\n// VERSION=".$version."//\n";\r
+ $fh->close();\r
+ }\r
+ }\r
+ }\r
+ }\r
+}\r
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
-#include "lib1stclass.h"
+#ifdef __cplusplus
+#include "1stclass.hpp"
+string firstclass::host2ip(const string server_str){
+ char ip[32]="";
+ char server[128]="";
+ safe_strcat(server, server_str.c_str());
+#else
+#include "lib1stclass.h"
int host2ip(const char *server, char *ip){
+#endif
+
struct addrinfo hint, *res, *p;
int error=0;
char ipstr[INET6_ADDRSTRLEN];
if(error){
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(error));
+
+#ifdef __cplusplus
+ return "";
+#else
return 1;
+#endif
}
for(p = res;p != NULL; p = p->ai_next) {
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
}
freeaddrinfo(res); // free the linked list
+
+#ifdef __cplusplus
+ firstclass::safe_strcat(ip, ipstr);
+ string last_ip(ip);
+ return last_ip;
+#else
safe_strcat(ip, ipstr);
return 0;
+#endif
+
}
+#ifdef __cplusplus
+
+#include "1stclass.hpp"
+int firstclass::is_num(const string in_moji){
+ char *moji=NULL;
+ int i;
+ int is_num_flag=0;
+
+ try{
+
+ moji=new char[sizeof(char)*(in_moji.length()+1)];
+ safe_strcat(moji, in_moji.c_str());
+
+#else
+
+#include "1stclass.h"
int is_num(const char *moji){
int i;
- int is_num=0;
+ int is_num_flag=0;
+
+#endif
for(i=0; moji[i]; i++){
if( (moji[i] < '0') || (moji[i] > '9') ){
+
+#ifdef __cplusplus
+ delete moji;
+#endif
return 0;
}
else{
- if(is_num==0){
- is_num++;
+ if(is_num_flag==0){
+ is_num_flag++;
}
}
}
- return is_num;
+
+#ifdef __cplusplus
+ delete moji;
+ }
+ catch(...){
+ if(moji!=NULL)
+ delete moji;
+ }
+#endif
+
+ return is_num_flag;
}
#include <string.h>
-void reverse(char s[]){
- int c, i, j;
-
- for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
- c = s[i];
- s[i] = s[j];
- s[j] = c;
- }
-}
-
-void itoa(int n, char s[]){
+#ifdef __cplusplus
+#include "1stclass.hpp"
+int firstclass::itoa(int n, char s[]){
+#else
+#include "1stclass.h"
+int itoa(int n, char s[]){
+#endif
int i, sign;
- if ((sign = n) < 0) /* record sign */
- n = -n; /* make n positive */
+ if ((sign = n) < 0)
+ n = -n;
i = 0;
- do { /* generate digits in reverse order */
- s[i++] = n % 10 + '0'; /* get next digit */
- } while ((n /= 10) > 0); /* delete it */
+ do { // generate digits in reverse order
+ s[i++] = n % 10 + '0'; // get next digit
+ } while ((n /= 10) > 0); // delete it
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
- reverse(s);
+ reverse_char(s);
+ return 0;
}
+++ /dev/null
-.PHONY: all
-all: lib1stclass.a
-
-lib1stclass.a: host2ip.o rmkdir.o safe_strcat.o safe_strncat.o is_num.o itoa.o remove_path.o remove_special_char.o lib1stclass.h
- ar rvs lib1stclass.a host2ip.o itoa.o rmkdir.o safe_strcat.o safe_strncat.o is_num.o remove_path.o remove_special_char.o;
- ranlib lib1stclass.a;
-
-host2ip.o: host2ip.c
- gcc -Wall -c host2ip.c;
-
-itoa.o: itoa.c
- gcc -Wall -c itoa.c
-
-remove_special_char.o: remove_special_char.c;
- gcc -Wall -c remove_special_char.c;
-
-remove_path.o: remove_path.c
- gcc -Wall -c remove_path.c;
-
-rmkdir.o: rmkdir.c
- gcc -Wall -c rmkdir.c;
-
-safe_strcat.o: safe_strcat.c
- gcc -Wall -c safe_strcat.c;
-
-safe_strncat.o: safe_strncat.c
- gcc -Wall -c safe_strncat.c;
-
-is_num.o: is_num.c
- gcc -Wall -c is_num.c;
-
-.PHONY: install
-install:
- mkdir -p /usr/local/lib;
- cp lib1stclass.a /usr/local/lib;
- cp lib1stclass.h /usr/local/include;
-
-.PHONY: clean
-clean:
- rm *.o -f *.o;
- rm lib1stclass.a -f lib1stclass.a;
-
#include <dirent.h>
#include <unistd.h>
#include <string.h>
-#include "lib1stclass.h"
+
+#ifdef __cplusplus
+
+#include "1stclass.hpp"
+int firstclass::remove_path(const string in_path){
+ char *path=NULL;
+ try {
+ path=new char[sizeof(char)*(in_path.length()+1)];
+ safe_strcat(path, in_path.c_str());
+
+#else
+
+#include "1stclass.h"
int remove_path(const char *path){
+
+#endif
+
DIR *dirp;
struct dirent *dp;
if((dirp = opendir(path))==NULL){
+
+#ifdef __cplusplus
+ delete path;
+#endif
+
return 0;
}
else{
closedir(dirp);
rmdir(path);
}
+#ifdef __cplusplus
+ delete(path);
+ }
+ catch (...) {
+ if(path!=NULL)
+ delete(path);
+ return 1;
+ }
+#endif
return 0;
}
#include<string.h>
#include<stdio.h>
-#include "lib1stclass.h"
+#ifdef __cplusplus
+#include "1stclass.hpp"
+int firstclass::remove_special_char(string &in_str){
+ char *str=NULL;
+ char *dst=NULL;
+ str=new char[sizeof(char)*(in_str.length()+1)];
+ dst=new char[sizeof(char)*(in_str.length()+1)];
+ try {
+ safe_strcat(str, in_str.c_str());
+#else
+#include "lib1stclass.h"
int remove_special_char(const char *str, char *dst){
+#endif
int zero=0;
if(sizeof(dst)<sizeof(str)){
printf("Bad parameter for remove_special_character\n");
+#ifdef __cplusplus
+ free(str);
+ free(dst);
+#endif
return 1;
}
if((strrchr(str, '\0'))==NULL){
printf("\\0 is missing for the input parameter of remove_special_character\n");
+#ifdef __cplusplus
+ free(str);
+ free(dst);
+#endif
return 1;
}
for( ; *str!='\0'; str++, dst++){
}
}
*dst='\0';
+#ifdef __cplusplus
+ string result(dst);
+ free(str);
+ free(dst);
+ in_str=result;
+ }
+ catch(...){
+ if(str!=NULL)
+ free(str);
+ if(dst!=NULL)
+ free(dst);
+ }
+#endif
return 0;
}
--- /dev/null
+#include <string.h>
+#ifdef __cplusplus
+#include "1stclass.hpp"
+int firstclass::reverse_char(char s[]){
+
+#else
+#include "1stclass.h"
+int reverse_char(char s[]){
+
+#endif
+
+ int c, i, j;
+
+ for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
+ c = s[i];
+ s[i] = s[j];
+ s[j] = c;
+ }
+ return 0;
+}
#include <dirent.h>
#include <string.h>
+#ifdef __cplusplus
+#include "1stclass.hpp"
+int firstclass::rmkdir(const string in_dir){
+ char *check_dir=NULL;
+ try{
+ check_dir=new char[sizeof(char)*(in_dir.length()+1)];
+ safe_strcat(check_dir, in_dir.c_str());
+#else
+#include "1stclass.h"
int rmkdir(const char *check_dir){
+#endif
+
struct stat st;
char *token_p=NULL;
char dir_path[512]="";
if(mkdir(dir_path, 0755)==0){
}
else{
+#ifdef __cplusplus
+ delete check_dir;
+#endif
return 1;
}
}
if(mkdir(dir_path, 0755)==0){
}
else{
+#ifdef __cplusplus
+ delete check_dir;
+#endif
return 1;
}
}
}
}
}
+#ifdef __cplusplus
+ delete check_dir;
+ }
+ catch(...){
+ if(check_dir!=NULL)
+ delete check_dir;
+ return 1;
+ }
+#endif
return 0;
}
#include <stdio.h>
#include <string.h>
-#include "lib1stclass.h"
+#ifdef __cplusplus
+#include "1stclass.hpp"
+int firstclass::safe_strcat(char *dst, const char *append){
+#else
+#include "lib1stclass.h"
int safe_strcat(char *dst, const char *append){
+#endif
if( sizeof(append) >sizeof(dst) ){
printf("\"%s\" is too long for this system.\n", append);
return 1;
#include <stdio.h>
#include <string.h>
-int safe_strncat(char *dst, const char *append, const int len){
+#ifdef __cplusplus
+#include "1stclass.hpp"
+int firstclass::safe_strncat(char *dst, const char *append, const unsigned int len){
+#else
+#include "1stclass.h"
+int safe_strncat(char *dst, const char *append, const unsigned int len){
+#endif
if( strlen(dst)>len ){
printf("\"%s\" is too long for this system.\n", append);
return 1;
#include <string.h>
-char *shmf(const char *file, char *shmfile){
+#ifdef __cplusplus
+#include "1stclass.hpp"
+string firstclass::shmf(const string in_file){
+ const char *file=in_file.c_str();
+ char shm_file[1024]="";
+#else
+#include "lib1stclass.h"
+int shmf(const char *file, char *shm_file){
+#endif
+
char tmp_file[1024]="";
int i;
safe_strcat(tmp_file, file);
break;
}
}
- safe_strcat(shmfile, "/dev/shm/");
- safe_strcat(shmfile, tmp_file);
- return tmp_file;
+
+ safe_strcat(shm_file, "/dev/shm/");
+ safe_strcat(shm_file, tmp_file);
+
+#ifdef __cplusplus
+ string result(shm_file);
+ return result;
+#else
+ return 0;
+#endif
}
--- /dev/null
+#include <string>
+#include <string.h>
+#include <sys/stat.h>
+#include "../1stclass.hpp"
+#include "../1stclass.h"
+using namespace std;
+
+class tester{
+public:
+ tester(){ok=0;bad=0;};
+ void test();
+ void report();
+private:
+ int ok;
+ int bad;
+};
+
+void tester::test(){
+ firstclass fst;
+ struct stat st;
+ if(fst.is_num("2") && fst.is_num("test")==0){
+ cout << "is_num/C++: ok "<< endl;
+ ok++;
+ }
+ else{
+ cout << "is_num/C++: bad" << endl;
+ bad++;
+ }
+
+ if(is_num("2") && is_num("test")==0){
+ cout << "is_num/C: ok "<< endl;
+ ok++;
+ }
+ else{
+ cout << "is_num/C: bad" << endl;
+ bad++;
+ }
+
+ string server="www.yahoo.co.jp";
+ string ip=fst.host2ip(server);
+ if( (ip.at(0) - '1'>=0) && (ip.at(0) - '1'<=9) ){
+ cout << "host2ip/C++: ok" << endl;
+ ok++;
+ }
+ else{
+ cout << "host2ip/C++: bad" << endl;
+ bad++;
+ }
+
+ char c_server[]="www.yahoo.co.jp";
+ char c_ip[32]="";
+ host2ip(c_server, c_ip);
+ if( (c_ip[0] - '1' >=0) && (c_ip[0] - '1' <=9) ){
+ cout << "host2ip/C: ok" << endl;
+ ok++;
+ }
+ else{
+ cout << "host2ip/C: bad" << endl;
+ bad++;
+ }
+
+ fst.rmkdir("test_dir");
+ if(stat("test_dir", &st)==0){
+ cout << "rmkdir/C++: ok" << endl;
+ ok++;
+ fst.remove_path("test_dir");
+ if(stat("test_dir", &st)!=0){
+ cout << "remove_path/C++: ok" << endl;
+ ok++;
+ }
+ else{
+ cout << "remove_path/C++: bad" << endl;
+ bad++;
+ }
+ }
+ else{
+ cout << "rmkdir/C++: bad" << endl;
+ bad++;
+ }
+
+ rmkdir("test_dir");
+ if(stat("test_dir", &st)==0){
+ cout << "rmkdir/C: ok" << endl;
+ ok++;
+ remove_path("test_dir");
+ if(stat("test_dir", &st)!=0){
+ cout << "remove_path/C: ok" << endl;
+ ok++;
+ }
+ else{
+ cout << "remove_path/C: bad" << endl;
+ bad++;
+ }
+ }
+ else{
+ cout << "rmkdir/C: bad" << endl;
+ bad++;
+ }
+
+ int integer=12345;
+ char itoa_str[6]="";
+
+ fst.itoa(integer, itoa_str);
+ if (strcmp(itoa_str,"12345") == 0){
+ cout << "itoa/C++: ok" << endl;
+ ok++;
+ }
+ else{
+ cout << "itoa/C++: bad" << endl;
+ bad++;
+ }
+
+ itoa_str[0]='\0';
+ itoa(integer, itoa_str);
+ if (strcmp(itoa_str,"12345") == 0){
+ cout << "itoa/C: ok" << endl;
+ ok++;
+ }
+ else{
+ cout << "itoa/C: bad" << endl;
+ bad++;
+ }
+
+ char example1[]="1234";
+ fst.reverse_char(example1);
+ if (strcmp(example1,"4321") == 0){
+ cout << "reverse_char/C++: ok" << endl;
+ ok++;
+ }
+ else{
+ cout << "reverse_char/C++: bad" << endl;
+ bad++;
+ }
+
+ char example2[]="1234";
+ reverse_char(example2);
+ if (strcmp(example2,"4321") == 0){
+ cout << "reverse_char/C: ok" << endl;
+ ok++;
+ }
+ else{
+ cout << "reverse_char/C: bad" << endl;
+ bad++;
+ }
+}
+
+void tester::report(){
+ cout << "ok=" << ok << "\t" << "bad=" << bad << endl;
+}
+
+int main(){
+ tester tester;
+ tester.test();
+ tester.report();
+}