sobota, 29 lutego 2020

Animation (C++ Builder)



__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
 srand(time(NULL));
}
void __fastcall TForm1::Sort(int *tab, int size)
{
 int i,j,k;
 for(i=1;i<size;i++)
 {
  j=i;
  k=tab[j];
  while((j>0) && (tab[j-1]>k))
  {
   tab[j]=tab[j-1];
   j--;
  }
  tab[j]=k;
 }
}
TColor __fastcall TForm1::draw_Color(int value)
{
 TColor color;
 switch(value)
 {
  case 0: color=clWhite;break;
  case 1: color=clYellow;break;
  case 2: color=clBlue;break;
  case 3: color=clRed;break;
  case 4: color=clSilver;
  default: color=clBlack; break;

 }
 return color;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 TCanvas *tab_Canvas[5];
 tab_Canvas[0]=PaintBox1->Canvas;
 tab_Canvas[1]=PaintBox2->Canvas;
 tab_Canvas[2]=PaintBox3->Canvas;
 tab_Canvas[3]=PaintBox4->Canvas;
 tab_Canvas[4]=PaintBox5->Canvas;
 int i,j,k,l;
 int anime=500;
 int width=PaintBox1->Width;
 int height=PaintBox1->Height;
 int r_width=width/2;
 int r_height=height/2;
 int *r_Eclipse=new int[20];
 for(i=0;i<20;i++)
 {
  r_Eclipse[i]=rand()%(r_height-10)+10;
 }
 Sort(r_Eclipse,20);
 for(i=0;i<anime;i++)
 {
  j=rand()%5;
  tab_Canvas[0]->Brush->Style=bsSolid;
  tab_Canvas[0]->Brush->Color=draw_Color(j);
  tab_Canvas[0]->Rectangle(0,0,width,height);
  tab_Canvas[0]->Brush->Style=bsClear;
  for(k=0;k<20;k++)
  {
   r_Eclipse[k]+=2;
   tab_Canvas[0]->Ellipse(r_width-r_Eclipse[k],r_height-r_Eclipse[k],r_width+r_Eclipse[k],r_height+r_Eclipse[k]);
  }
  for(l=1;l<5;l++)
  {
   tab_Canvas[l]->CopyRect(Rect(0,0,width,height),tab_Canvas[0],Rect(0,0,width,height));
  }
 }

 delete r_Eclipse;

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 Close();
}

poniedziałek, 17 lutego 2020

Shared memory (C++ Linux)

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <stdio.h>
#include <sys/shm.h>

#define m_size 10
#define m_mem 10000
using namespace std;



 const string capitals[m_size]={"Warsaw","Berlin","Moscow","Paris","Athens","Kiev",
"Prague","Roma","Lisbon","Amsterdam"};
 const int segments[m_size]={0x1500,0x2500,0x3500,0x4500,0x5500,0x6500,0x7500,
0x8500,0x9500,0x4900};
 const int new_segments[m_size]={0x100000,0x200000,0x300000,0x4000000,0x5000000,
 0x6000000,0x7000000,0x8000000,0x9000000,0x783838};
 int value_Size[10];
 int value_Segm[10];
int main(int argc, char **argv)
{
int i;
char **mem=new char *[m_size];
for(i=0;i<m_size;i++)
mem[i]=new char[m_mem];

struct shmid_ds* mem_Contr=new shmid_ds[m_size];
for(i=0;i<m_size;i++)
{
value_Segm[i]=shmget(IPC_PRIVATE,segments[i],IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
mem[i]=(char*)shmat(value_Segm[i],0,0);
shmctl(value_Segm[i],IPC_STAT,&mem_Contr[i]);
value_Size[i]=mem_Contr[i].shm_segsz;
cout<<"Size memory: "<<value_Size[i]<<endl;
sprintf(mem[i],capitals[i].c_str());
shmdt(mem[i]);
mem[i]=(char*)shmat(value_Segm[i],(void*)new_segments[i],0);
printf("New memory: %p    %s\n",mem[i],mem[i]);
shmdt(mem[i]);
shmctl(value_Segm[i],IPC_RMID,0);
}
for(i=0;i<m_size;i++)
delete [] mem[i];
delete mem;
delete mem_Contr;




return 0;
}

czwartek, 6 lutego 2020

Copying files (C++ Linux)

#include <iostream>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
#include <sys/types.h>

#define size_F 10

using namespace std;

string proc_Date[size_F]={"/proc/dma","/proc/misc","/proc/meminfo","/proc/kcore","/proc/keys","/proc/iomen",
"/proc/slabinfo","/proc/version","/proc/zoneinfo","/proc/cpuinfo"};
string dest_Date[size_F]={"dma1","misc1","meminfo1","kcore1","keys1","iomen1","slabinfo1","version1","zoneinfo1",
"cpuinfo1"};

off_t *o_F;
int *r,*w;
struct stat* run_Stat;

int main(int argc, char **argv)
{
o_F=new off_t[size_F];
run_Stat=new struct stat[size_F];
r=new int[size_F];
w=new int[size_F];
for(int i=0;i<size_F;i++)
{
r[i]=open(proc_Date[i].c_str(),O_RDONLY);
fstat(r[i],&run_Stat[i]);
w[i]=open(dest_Date[i].c_str(),O_WRONLY | O_CREAT,run_Stat[i].st_size);
sendfile(w[i],r[i],&o_F[i],run_Stat[i].st_size);
close(r[i]);
close(w[i]);
}
delete w;
delete r;
delete run_Stat;
delete o_F;

return 0;
}

sobota, 1 lutego 2020

Counting the given text in the file (C++ Builder)


char* __fastcall TForm1::read_Ch(AnsiString name_File, int f_Size)
{
char *znaki=new char[f_Size];

FILE* fp;
fp=fopen(name_File.c_str(),"r");
for(int i=0;i<f_Size;i++)
fscanf(fp,"%c",&znaki[i]);
fclose(fp);
return znaki;

}
void __fastcall TForm1::init_Memo(AnsiString name_File, int f_Size)
{
Memo1->Lines->Append(AnsiString(read_Ch(name_File,f_Size)));
}
int __fastcall TForm1::sum_Txt(char *phrase)
{
int sum=0,i,count=Memo1->Lines->Count;
AnsiString *buff=new AnsiString[count];
for(i=0;i<count;i++)
{
buff[i]=Memo1->Lines->Text[i];
if(strcmp(buff[i].c_str(),phrase))
++sum;
}
delete buff;
return sum;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 AnsiString path;
 path="C:\\Users\\Public\\Documents\\Embarcadero\\Studio\\18.0\\GetItInstall.log";
 init_Memo(path,2048);
  AnsiString temp=Edit1->Text;
 int how_Many=sum_Txt(temp.c_str());
 Label1->Caption=IntToStr(how_Many);
 }
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
 Close();
}

czwartek, 30 stycznia 2020

Data from the file WindowsUpdate.log (Visual C++)


 System::Void read_Windows(String^ phrase)
{
System::Int64 i;
System::Int64 count_Line=0;
    System::String^ temp_Text;
String^ path="C:\\Users\\luk\\Documents\\C_plus\\Windows_U.txt.txt";
StreamReader^ my_File=gcnew StreamReader(path,System::Text::Encoding::Default);
 array<System::String^>^ buff=gcnew array<System::String^>(1024);
while(temp_Text=my_File->ReadLine())
{
 
 buff[count_Line]=temp_Text+System::Environment::NewLine;
 ++count_Line;
}
my_File->Close();
for(i=0;i<count_Line;i++)
  if(buff[i]->Contains(phrase))
  if(buff[i]->Contains("="))
  listBox1->Items->Add(buff[i]->Remove(buff[i]->IndexOf("="))+System::Environment::NewLine);
  else
  listBox1->Items->Add(buff[i]+System::Environment::NewLine);
 
 }
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

array<System::String^>^ windows_Update=gcnew array<System::String^>(8);
windows_Update[0]="Computer Brand";
windows_Update[1]="Computer Model";
windows_Update[2]="Bios Revision";
windows_Update[3]="Bios Name";
windows_Update[4]="OS Version";
windows_Update[5]="Process:";
windows_Update[6]="Module:";
windows_Update[7]="Base directory:";
for(System::Int64 i=0;i<8;i++)
read_Windows(windows_Update[i]);

 
}

sobota, 28 grudnia 2019

Save from text file to html (c++ function)

#define max_Size 256
void Save_to_HTML(char *text_File, char *new_html_File)
{

    int i,j,line=0;
    fstream file_Text;
    string buff[max_Size];
    file_Text.open(text_File,ios::in);
    while(!file_Text.eof())
    {
        getline(file_Text,buff[line]);
        ++line;
    }
    file_Text.close();
    int sum_Count=line;
    string begin_Page[6]={"<HTML>","<HEAD>","<TITLE>","</TITLE","</HEAD>","<BODY>"};
    string end_Page[2]={"</BODY>","</HTML>"};
    int size_html_Page=sum_Count+6+2;
    string *page_HTML=new string[size_html_Page];
    for(i=0;i<6;i++)
   {
    page_HTML[i]=begin_Page[i];
   }
   for(i=0,j=6;i<sum_Count;i++,j++)
   {
    page_HTML[j]=buff[i]+" <br> ";
   }
   for(i=0,j=6+sum_Count;i<2;i++,j++)
   {
    page_HTML[j]=end_Page[i];
   }
   fstream file_HTML;
   file_HTML.open(new_html_File, ios::out | ios::app);
   if(file_HTML.good() == true)
   {
       for(i=0;i<size_html_Page;i++)
       {
           file_HTML<<page_HTML[i]<<"\n";
       }
       file_HTML.close();
   }
   delete [] page_HTML;

}


int main()
{
 Save_to_HTML("readme.txt","READ2.html");
    return 0;
}

czwartek, 26 grudnia 2019

utsname values (html) gcc (linux)

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/utsname.h>

struct uts_Values
{
char* sysname;
char* release;
char* machine;
char* version;
};

int main(int argc, char **argv)
{
size_t length_Table;
int file_HTML;
char *filename="Version.html";
struct utsname sys_Utsname;
struct uts_Values m_Values;
uname(&sys_Utsname);
m_Values.sysname=sys_Utsname.sysname;
m_Values.release=sys_Utsname.release;
m_Values.machine=sys_Utsname.machine;
m_Values.version=sys_Utsname.version;
static char* html_page=
"<html>\n"
"<body>\n"
"<table>\n"
"<thead>\n"
"<tr>\n"
"<th>m_Values.sysname</th>\n"
"<th>m_Values.release</th>\n"
"<th>m_Values.machine</th>\n"
"<th>m_Values.version</th>\n"
"</tr>\n"
"</thead>\n"
"</table>\n"
"</body>\n"
"</thml>\n";
length_Table=strlen(html_page);
file_HTML=open(filename,O_WRONLY);
write(file_HTML,html_page,length_Table);
close(file_HTML);


return 0;
}