Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting away from the CLX 1

Status
Not open for further replies.

Ruggie

Programmer
Aug 2, 2003
46
US
I use a few batch files on a slow computer and needed a wait program so I decided I'd just make one. I used the CLX.h because of the features it offers (like the vcl)

My problem/question is, the program works perfectly, but compiled with no external dependencies its 369k and once loaded into memory takes up 1,324k. I'd like to move away from the CLX to save the resources.

Heres the code:
Code:
//---------------------------------------------------------------------------

#include <clx.h>
#include <iostream.h>
#pragma hdrstop

//---------------------------------------------------------------------------

#define RETURN_SUCCESS 0
#define RETURN_ERROR 1

//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{
    if (argc == 1) // No arguments passed.
    {
        cout << endl;
        cout << "Usage:" << endl;
        cout << "wait.exe <SecondsToWait>" << endl;
        cout << endl;
        cout << "Example:" << endl;
        cout << "wait.exe 30" << endl;
        cout << "(Waits for 30 seconds, then exits)" << endl;
        cout << endl << endl;
        cout << "Returns 0 on success, 1 on fail." << endl;
        return RETURN_ERROR;
    }
    else if (argc == 2) // Exactly 1 argument passed
    {
        try
        {
                // Process input
                AnsiString as_SleepTime;
                as_SleepTime = AnsiString(argv[1]);
                unsigned long ul_SleepTime;
                ul_SleepTime = as_SleepTime.ToInt();
                ul_SleepTime = ul_SleepTime * 1000;
                // Post msg
                cout << "Waiting for " <<  argv[1] << " seconds." << endl;
                cout << "Use CTRL+C to skip." << endl;
                // Sleep
                Sleep(ul_SleepTime);
                return RETURN_SUCCESS;
        }
        catch (...)  // Argument passed was not a number
        {
                cout << "ERROR!" << endl;
                cout << "Was not able to convert input into an integer." << endl;
                return RETURN_ERROR;
        }
    }
    else  // More then one argument passed.
    {
        cout << "ERROR!  Improper usage!" << endl;
        cout << "Usage:" << endl;
        cout << "wait.exe <SecondsToWait>" << endl;
        return RETURN_ERROR;
    }
}
//---------------------------------------------------------------------------

I'm not the greatest programmer but I'm always trying to learn new ways to do things.

I've replaced the clx.h with windows.h
But now AnsiString is no longer valid nor is string.
(this time I'm trying to use VC++ 6 instead of Builder6)

I don't know of any way to convert the char input from argv to an unsigned long, or if that input is even valid anymore.

Please post your comments on this.

Always appreciated!

-Ruggie
 
Yes, I know there are other already existing methods, but this is a simple task and the problems are valid. The more I learn now the less trouble I'll have later on.

Besides, the best teacher is experience. :)

Please someone help me out here.

-Ruggie
 
Then, you could do something like this, which compiles to 54k:
Code:
#include <stdio.h>
#include <windows.h>
#define RETURN_SUCCESS 0
#define RETURN_ERROR 1

int main(int argc, char* argv[])
{
  if (argc == 1) // No arguments passed.
  {
    printf
    (
      "\nUsage:\n"
      "wait.exe <SecondsToWait>\n\n\n"
      "Example:\n"
      "wait.exe 30\n"
      "(Waits for 30 seconds, then exits)\n\n\n"
      "Returns 0 on success, 1 on fail.\n"
    );
    return RETURN_ERROR;
  }
  else // 1 or more arguments passed
  {
    unsigned long ul_SleepTime=strtoul(argv[1],0,10);
    if(argc>2||ul_SleepTime==0)
      // more than 1 argument, or invalid argument
    {
      printf("ERROR!  Improper usage!\nUsage:\nwait.exe <SecondsToWait>\n");
      return RETURN_ERROR;
    }

    // Exactly 1 argument passed - Process input
    printf("Waiting for %i seconds.\nUse CTRL+C to skip.\n",ul_SleepTime);
    Sleep(ul_SleepTime*1000);
    return RETURN_SUCCESS;
  }
}
This ignores Borland component libraries, which would add a lot to the program size.
 
Sorry, that last printf statement should really be:
Code:
printf("Waiting for %lu seconds.\nUse CTRL+C to skip.\n",ul_SleepTime);
 
I didn't know about the strtoul command at all! Is that in stdio.h or windows.h?
Also you pass 0 and 10 to strtoul, why?

Finally, are there anymore xxxtoxxx commands and if so where is the documentation on them?

Sorry about all the questions, I haven't made many applications without the vcl or clx.

Thank you, you showed me a few ways to do things differently.

-Ruggie
 
strtoul is in stdlib.h which is included in windows.h; it converts a string to an unsigned long. The second argument is an optional char** which receives a pointer to the first invalid character in the string, and the 10 is the number base to be used. You'll find it, and many other conversion routines, in the documentation for the C Runtime Library, which is included with C++ Builder.
 
Thanks again for all the info.

-Ruggie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top