czwartek, 30 maja 2019

Reading directory (class, C++, g++ Linux)


#include <iostream>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#define PP PATH_MAX+1
using namespace std;

class ls_Directory
{
private:
size_t size_path;
DIR *dir_function;
    const char *directory_path;
char main_path[PP];
struct dirent *dir_entr;
public:
ls_Directory()
{
;
}

void read_Path(const char *directory)
{
directory_path=directory;
strncpy(main_path,directory_path,sizeof(main_path));
size_path=strlen(directory_path);
char end_char='/';
size_t temp_s=size_path-1;
if(main_path[temp_s]!=end_char)
{
main_path[size_path]=end_char;
main_path[temp_s+2]='\0';
++size_path;
}
dir_function=opendir(directory_path);

}
void write_Path()
{
while(((dir_entr=readdir(dir_function))&& dir_entr!=NULL))
{
char *temp;
strncpy(main_path+size_path,dir_entr->d_name,sizeof(main_path)-size_path);
cout<<temp<<"  : "<<endl;
}
closedir(dir_function);
}
};


int main(int argc, char **argv)
{
string dir_unix[3]={"usr","lib","dev"};
ls_Directory *DIR_LINUX=new ls_Directory[3];
int i;
for(i=0;i<3;i++)
{
DIR_LINUX[i].read_Path(dir_unix[i].c_str());
DIR_LINUX[i].write_Path();
}
delete DIR_LINUX;
return 0;
}

środa, 22 maja 2019

Reading paths in C++ Builder




void __fastcall TForm1::Button1Click(TObject *Sender)
{
 char end='\0';
 char *char_v="C:";
 char *info_a="C:\\Program Files\\Embarcadero\\Studio\\18.0\\Path-After-Install.txt";
 char *info_b="C:\\Program Files\\Embarcadero\\Studio\\18.00\\Path-Before-Install.txt";
 FILE *before_path_file,*after_path_file;
 size_t bytes_val[2];
 char buf_after[512],buf_before[512];
 char *temp_after,*temp_before;
 char *info_after,*info_before;
 after_path_file=fopen(info_a,"r");
 bytes_val[0]=fread(buf_after,1,sizeof(buf_after),after_path_file);
 if(bytes_val[0]==0)
 {
  UnicodeString info; info=info_a;
  ShowMessage("Error! Can't open file"+info);
 }
 buf_after[bytes_val[0]]=end;
 temp_after=strstr(buf_after,char_v);
 sscanf(temp_after,char_v,"%s",info_after);
 before_path_file=fopen(info_b,"r");
 bytes_val[1]=fread(buf_before,1,sizeof(buf_before),before_path_file);
 if(bytes_val[1]==0)
 {
  UnicodeString info; info=info_b;
  ShowMessage("Error! Can't open file"+info);
 }
 buf_before[bytes_val[1]]=end;
 temp_before=strstr(buf_before,char_v);
 sscanf(temp_before,char_v,"%s",info_before);
 Edit1->Text=info_before;
 Edit2->Text=info_after;

}

czwartek, 9 maja 2019

table-StringGrid - C++ Builder


private: // User declarations
   bool __fastcall is_First(int value);
   int tab_First[1000];
   int date[9][9];
   void __fastcall init_First();
   void __fastcall init_Date();

bool __fastcall TForm1::is_First(int value)
{
int i,sum;
sum=0;
for(i=1;i<value+1;i++)
if(value%i==0)
  ++sum;
if(sum==2)
return true;
else
     return false;
}
void __fastcall TForm1::init_First()
{
int i,j;
j=2;i=0;
do
{
  if(is_First(j))
  {
  tab_First[i]=j;
  ++i;
  }
  ++j;
}while(i<1000);
}
void __fastcall TForm1::init_Date()
{
int i,j,k,centr,left,right,s;
centr=4;
i=0;
right=i+1;
left=right+1;
do
{


  j=centr+1;
  s=1;
  do

  {


   date[i][j]=tab_First[s*(i+1)];
   ++s;
   ++j;
  }while(j<9);
  k=centr-1;
  s=1;
  do
  {
   date[i][k]=tab_First[s*(i+1)];
   ++s;
   --k;
  }while(k>-1);

  ++i;
}while(i<9);
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
 init_First();
 init_Date();
 int i,j;
 for(i=0;i<9;i++)
 {
for(j=0;j<9;j++)
{
StringGrid1->Cells[j][i]=IntToStr(date[i][j]);
     }
 }
}

środa, 8 maja 2019

FileOpen and FileSeek - C++ Builder

void __fastcall TForm1::Mtext_File(UnicodeString file_Name, TListBox *lBox,UnicodeString new_File)
{
UnicodeString result,temp_File;
ofstream n_File;
wchar_t *buff;
int i,handle,size;
temp_File=file_Name;
handle=FileOpen(temp_File.w_str(),fmOpenRead);
size=FileSeek(handle,0,2);
if(size==-1)
{
result="Error! Can't open file - "+temp_File;
ShowMessage(result);
}
else
{

FileSeek(handle,0,0);
buff=new wchar_t[size+1];
FileRead(handle,buff,size);
FileClose(handle);
result=buff;

delete buff;
lBox->Items->Add(result);
n_File.open(new_File.w_str());
n_File<<result.w_str();
n_File.close();

}

}
void __fastcall TForm1::Button1Click(TObject *Sender)
{

  Mtext_File("readme.txt",TForm1::ListBox1,"new_question.txt");
}

wtorek, 7 maja 2019

mmap/munmap gcc

#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#define size_Value 1000
#define size_F 0x100

int *first_Tab;
double *sqrt_Tab;
void *use_Func1;
void *use_Func2;
int file_Date;
int is_First(int value)
{
int i,sum;
sum=0;
for(i=1;i<value+1;i++)
if(value%i==0)
 ++sum;
if(sum==2)
return 1;
else
return 0;
}
void init_File(char *name_File)
{
file_Date=open(name_File,O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
lseek(file_Date,size_F+1,SEEK_SET);
write(file_Date,"",1);
lseek(file_Date,0,SEEK_SET);
}
void init_Tab()
{
int i,j;
srand(time(NULL));
first_Tab=malloc(size_Value*sizeof(int));
sqrt_Tab=malloc(size_Value*sizeof(double));
i=0;j=2;
do
{
if(is_First(j))
{
first_Tab[i]=j;
sqrt_Tab[i]=sqrt(1.0*j);
}
++j;
}while(i<size_Value);
}
int read_Int(int value)
{
return first_Tab[value];
}
int read_Double(int value)
{
return sqrt_Tab[value];
}
int random_Result()
{
int value;
value=rand()%size_Value;
}
void destroy_Tables()
{
free(sqrt_Tab);
free(first_Tab);
}

int main(int argc, char **argv)
{
int i;
char *name="test_mmap";
init_Tab();
i=random_Result();
init_File(name);
use_Func1=mmap(0,size_F,PROT_WRITE,MAP_SHARED,file_Date,0);
use_Func2=mmap(0,size_F,PROT_WRITE,MAP_SHARED,file_Date,0);
close(file_Date);
sprintf((char*) use_Func1,"%d\n",read_Int(i));
sprintf((char*) use_Func2,"%.2f\n",read_Double(i));
destroy_Tables();
munmap(use_Func2,size_F);
munmap(use_Func1,size_F);

return 0;
}