You are not logged in.

#1 2011-04-25 15:35:01

lupusarcanus
Member
From: USA
Registered: 2011-04-11
Posts: 35

My First C++ Program, aget

Hi! I'm a newbie at C++. I'm following a video tutorial on YouTube by 'antiRTFM'. I've learned a lot so far, though this program was only made with what I know, so it probably has unnecessary, confusing syntax and is not anywhere near optimized. It was just a hack-together by a newbie (I don't even know anything about pointers or strcat yet!). tongue. It's pretty rough around the edges, but it makes it a painless process to get, install and clean up AUR builds (albeit unsecurely). It depends on and uses makepkg, wget and tar directly. I actually do use it! big_smile

I am kinda proud of it though (it's my first real program!), and thought I'd share it here. Maybe someone would like to use it, maybe some gurus will offer me some kind advice, or maybe it can help someone who is having trouble in some obscure way. It's under the BSD license, so do whatever you want with it. It's probably too simple to even have a license anyway. tongue

Here is the source code:-

#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
    system("mkdir /tmp/.aget");
    chdir("/tmp/.aget");

    for (int i = 1; i < argc; i++)
    {
        string target(argv[i]);
        string docommand("");
        string s1("wget -q http://aur.archlinux.org/packages/");
        string s2("/");
        string s3(".tar.gz");
        docommand += s1;
        docommand += target;
        docommand += s2;
        docommand += target;
        docommand += s3;
        cout << "Downloading AUR tarball for '" << target << "'..." << endl;
        system(docommand.c_str());
    }

    for (int i = 1; i < argc; i++)
    {
        string target(argv[i]);
        string docommand("");
        string s1("tar xf ");
        string s2(".tar.gz");
        docommand += s1;
        docommand += target;
        docommand += s2;
        cout << "Extracting '" << target << ".tar.gz'..." << endl;
        system(docommand.c_str());
    }

    for (int i = 1; i < argc; i++)
    {
        string target(argv[i]);
        string docommand("");
        chdir("/tmp/.aget");
        chdir(target.c_str());
        system("makepkg -csim --noconfirm > /dev/null");
    }

    system("rm -rf /tmp/.aget");

    return 0;
}

Thanks for looking, and sorry for my newbiness/standards-non-compliance(sp?). I guess I'm just kind of happy/excited to share it with someone. C++ is fun!

Offline

#2 2011-04-25 18:01:47

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: My First C++ Program, aget

I'm curious why you chose to use C++ instead of writing a bash script - most of what this does would be possibly easier to accomplish in a script.   Was it for the learning experience?

I don't see anything necessarily bad about the program itself - good indentation, decent style (although I prefer avoiding 'using namespace', myself), no obvious bugs.  Not a bad first attempt. smile

Offline

#3 2011-04-25 18:42:25

SoleSoul
Member
From: Israel
Registered: 2009-06-29
Posts: 319

Re: My First C++ Program, aget

Keep going. You are on the right track!
Just remember, for more complicated applications, that C++ is object oriented while C is procedural. I talk about the paradigm. It confused me in the beginning.

Offline

#4 2011-04-25 18:48:42

tesjo
Member
Registered: 2007-11-30
Posts: 164

Re: My First C++ Program, aget

I agree with both Cerebral and SoleSoul. Here is a simple rewrite (mostly copy and paste) with an object and use of a pointer, for you to chew on.

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

class packagelist{
  int npkg;
  string *target;
 public:
  packagelist (int,char**);
  void download();
  void extract();
  void install();
};
packagelist::packagelist(int n, char* argv[]){
  npkg=n-1;
  target=new string[npkg];
  for (int i=0;i<npkg; i++)
    target[i]=argv[i+1];
}
void packagelist::download(){
  for (int i=0;i<npkg; i++){
    string docommand("");
    string s1("wget -q http://aur.archlinux.org/packages/");
    string s2("/");
    string s3(".tar.gz");
    docommand += s1;
    docommand += target[i];
    docommand += s2;
    docommand += target[i];
    docommand += s3;
    cout << "Downloading AUR tarball for '" << target[i] << "'..." << endl;
    system(docommand.c_str());
  }
}
void packagelist::install(){
  for (int i=0;i<npkg; i++){
    string docommand("");
    chdir("/tmp/.aget");
    chdir(target[i].c_str());
    cout << "Installing '" << target[i] <<"'... "<<  endl;
    system("makepkg -csim --noconfirm > /dev/null");
  }
}
void packagelist::extract(){
  for (int i=0;i<npkg; i++){
    string docommand("");
    string s1("tar xf ");
    string s2(".tar.gz");
    docommand += s1;
    docommand += target[i];
    docommand += s2;
    cout << "Extracting '" << target[i] << ".tar.gz'..." << endl;
    system(docommand.c_str());
  }
}

int main(int argc, char* argv[])
{
    system("mkdir /tmp/.aget");
    chdir("/tmp/.aget");

    packagelist packages(argc,argv);
    packages.download();
    packages.extract();
    packages.install();
  
    system("rm -rf /tmp/.aget");

    return 0;
}

Offline

#5 2011-04-26 03:56:03

fumbles
Member
Registered: 2006-12-22
Posts: 246

Re: My First C++ Program, aget

One way to extend your programme and learn a bit more is to replace the system commands with system calls: eg system(mkdir) with mkdir(), system(rm) with unlink etc . You could extend it again by adding checks: Open the parent directory and check if the folder already exists and so on.

Last edited by fumbles (2011-04-26 03:56:31)

Offline

#6 2011-04-27 11:58:17

lupusarcanus
Member
From: USA
Registered: 2011-04-11
Posts: 35

Re: My First C++ Program, aget

Cerebral wrote:

I'm curious why you chose to use C++ instead of writing a bash script - most of what this does would be possibly easier to accomplish in a script.   Was it for the learning experience?

I don't see anything necessarily bad about the program itself - good indentation, decent style (although I prefer avoiding 'using namespace', myself), no obvious bugs.  Not a bad first attempt. smile

Thanks! It was for the learning experience, although I had fun while coding it too. smile

SoleSoul wrote:

Keep going. You are on the right track!
Just remember, for more complicated applications, that C++ is object oriented while C is procedural. I talk about the paradigm. It confused me in the beginning.

Heh, I'll sure try too!

Tesjo wrote:

I agree with both Cerebral and SoleSoul. Here is a simple rewrite (mostly copy and paste) with an object and use of a pointer, for you to chew on.

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

class packagelist{
  int npkg;
  string *target;
 public:
  packagelist (int,char**);
  void download();
  void extract();
  void install();
};
packagelist::packagelist(int n, char* argv[]){
  npkg=n-1;
  target=new string[npkg];
  for (int i=0;i<npkg; i++)
    target[i]=argv[i+1];
}
void packagelist::download(){
  for (int i=0;i<npkg; i++){
    string docommand("");
    string s1("wget -q http://aur.archlinux.org/packages/");
    string s2("/");
    string s3(".tar.gz");
    docommand += s1;
    docommand += target[i];
    docommand += s2;
    docommand += target[i];
    docommand += s3;
    cout << "Downloading AUR tarball for '" << target[i] << "'..." << endl;
    system(docommand.c_str());
  }
}
void packagelist::install(){
  for (int i=0;i<npkg; i++){
    string docommand("");
    chdir("/tmp/.aget");
    chdir(target[i].c_str());
    cout << "Installing '" << target[i] <<"'... "<<  endl;
    system("makepkg -csim --noconfirm > /dev/null");
  }
}
void packagelist::extract(){
  for (int i=0;i<npkg; i++){
    string docommand("");
    string s1("tar xf ");
    string s2(".tar.gz");
    docommand += s1;
    docommand += target[i];
    docommand += s2;
    cout << "Extracting '" << target[i] << ".tar.gz'..." << endl;
    system(docommand.c_str());
  }
}

int main(int argc, char* argv[])
{
    system("mkdir /tmp/.aget");
    chdir("/tmp/.aget");

    packagelist packages(argc,argv);
    packages.download();
    packages.extract();
    packages.install();
  
    system("rm -rf /tmp/.aget");

    return 0;
}

Wow! Thank you. I'm studying it now. smile

fumbles wrote:

One way to extend your programme and learn a bit more is to replace the system commands with system calls: eg system(mkdir) with mkdir(), system(rm) with unlink etc . You could extend it again by adding checks: Open the parent directory and check if the folder already exists and so on.

Hmm, that's a good idea. I'll think I'll try to do that!

Offline

#7 2011-04-27 14:31:19

stqn
Member
Registered: 2010-03-19
Posts: 1,191
Website

Re: My First C++ Program, aget

tesjo wrote:

I agree with both Cerebral and SoleSoul. Here is a simple rewrite (mostly copy and paste) with an object and use of a pointer, for you to chew on.

I have to say the original version was much more readable (and shorter.) smile

I'm not a big fan of using OO for the sake of using OO. I know that it was just an example for lupusarcanus to study; I'm just saying I'm in the camp of those who prefer to use whatever is simpler and gets the job done.

Offline

#8 2011-04-27 16:52:09

tesjo
Member
Registered: 2007-11-30
Posts: 164

Re: My First C++ Program, aget

stqn wrote:
tesjo wrote:

I agree with both Cerebral and SoleSoul. Here is a simple rewrite (mostly copy and paste) with an object and use of a pointer, for you to chew on.

I have to say the original version was much more readable (and shorter.) smile

I'm not a big fan of using OO for the sake of using OO. I know that it was just an example for lupusarcanus to study; I'm just saying I'm in the camp of those who prefer to use whatever is simpler and gets the job done.

To be clear I agree with you completely, I am no OO zealot. Use the right tool for the right job.  I write Fortran and C more than C++, but OO has its place.  Of course this is just an example, but as an appplication I agree with Cerebral bash would be my choice.

Last edited by tesjo (2011-04-27 16:52:39)

Offline

Board footer

Powered by FluxBB