Home > Software design >  I want to break this into multiple functions what data type can i use?
I want to break this into multiple functions what data type can i use?

Time:04-10

There's more code than this, but I'm retrieving data from multiple edit controls and an external file to count currency. I want to break this down into multiple smaller functions, but I don't know what data type it would use to run successfully.

HANDLE hBuck = CreateFile(FileName, GENERIC_ALL, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD bytes_read;
DWORD leni = GetFileSize(hBuck, NULL);
LPSTR buffer = (LPSTR)GlobalAlloc(GPTR, leni  1);
ReadFile(hBuck, buffer, leni, &bytes_read, NULL);
double xt = atof(buffer);
CloseHandle(hBuck);
        
DWORD len = GetWindowTextLength(hPen)  1;
buffer = (LPSTR)GlobalAlloc(GPTR, len  1);
GetWindowText(hPen, buffer, len);
double num1 = atof(buffer);

len = GetWindowTextLength(hNick)  1;
buffer = (LPSTR)GlobalAlloc(GPTR, len  1);
GetWindowText(hNick, buffer, len);
double num2 = atof(buffer);

len = GetWindowTextLength(hDime)  1;
buffer = (LPSTR)GlobalAlloc(GPTR, len  1);
GetWindowText(hDime, buffer, len);
double num3 = atof(buffer);
                    
len = GetWindowTextLength(hQuart)  1;
buffer = (LPSTR)GlobalAlloc(GPTR, len  1);
GetWindowText(hQuart, buffer, len);
double num4 = atof(buffer);

CodePudding user response:

Try something like this:

bool ReadDoubleFromFile(LPCSTR FileName, double *value)
{
    if (!value) return false;
    *value = 0.0;

    HANDLE hBuck = CreateFile(FileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hBuck == INVALID_HANDLE_VALUE) return false;

    DWORD len = GetFileSize(hBuck, NULL);
    if (len == INVALID_FILE_SIZE) {
        CloseHandle(hBuck);
        return false;
    }

    LPSTR buffer = (LPSTR) LocalAlloc(LPTR, len   1);
    if (!buffer) {
        CloseHandle(hBuck);
        return false;
    }

    DWORD bytes_read;
    if (!ReadFile(hBuck, buffer, len, &bytes_read, NULL)) {
        LocalFree((HLOCAL)buffer);
        CloseHandle(hBuck);
        return false;
    }

    CloseHandle(hBuck);

    *value = atof(buffer);
    LocalFree((HLOCAL)buffer);

    return true;
}

bool ReadDoubleFromEdit(HWND hEdit, double *value)
{
    if (!value) return false;
    *value = 0.0;

    DWORD len = GetWindowTextLengthA(hEdit);
    if (len > 0) {
          len;

        LPSTR buffer = (LPSTR) LocalAlloc(LPTR, len);
        if (!buffer) return false;

        if (!GetWindowTextA(hEdit, buffer, len)) {
            LocalFree((HLOCAL)buffer);
            return false;
        }

        *value = atof(buffer);
        LocalFree((HLOCAL)buffer);
    }

    return true;
}

...

double xt, num1, num2, num3, num4;

if (ReadDoubleFromFile(FileName, &xt) &&
    ReadDoubleFromEdit(hPen, &num1) &&
    ReadDoubleFromEdit(hNick, &num2) &&
    ReadDoubleFromEdit(hDime, &num3) &&
    ReadDoubleFromEdit(hQuart, &num4))
{
    // use values as needed...
}
else
{
    // error
}

On the other hand, if you are using C instead of C (you did not say which language you are using), this becomes a bit simpler:

double ReadDoubleFromFile(const std::string &FileName)
{
    std::ifstream inFile;
    inFile.exceptions(std::ifstream::failbit);
    inFile.open(FileName.c_str());
    double value;
    inFile >> value;
    return value;
}

double ReadDoubleFromEdit(HWND hEdit)
{
    double value = 0.0;
    DWORD len = GetWindowTextLength(hEdit);
    if (len > 0) {
        std::basic_string<TCHAR> buffer(len, TEXT('\0'));
        if (!GetWindowText(hEdit, buffer.data(), len   1))
            throw std::runtime_error("GetWindowText failed");
        value = std::stod(buffer);
    }
    return value;
}

...

try
{
    double xt   = ReadDoubleFromFile(FileName);
    double num1 = ReadDoubleFromEdit(hPen);
    double num2 = ReadDoubleFromEdit(hNick);
    double num3 = ReadDoubleFromEdit(hDime);
    double num4 = ReadDoubleFromEdit(hQuart);
    // use values as needed...
}
catch (const std::exception &ex)
{
    // handle error as needed...
}
  • Related