optimizing function

Discussion in 'C++' started by puneet vyas, Feb 26, 2009.

  1. puneet vyas

    puneet vyas Guest

    hello, this is the function i wrote for making a series of directory
    and subdirectory,is there any optimized way of doing the operation
    which i am doing,i mean using string functions without iterating over
    string

    // recursive directories.cpp : Defines the entry point for the console
    application.
    //

    #include "stdafx.h"
    //#include "WriteDir.h"
    #include<stdio.h>
    #include <afx.h>
    #include <CString>
    #include <direct.h>
    #include <string>
    #include <iostream>
    #include <vector>
    using namespace std;

    /*
    BOOL WriteDirectory(CString dd)
    {


    HANDLE fFile; // File Handle
    WIN32_FIND_DATA fileinfo; // File Information Structure
    CStringArray m_arr; // CString Array to hold Directory
    Structures
    vector<string> paths;
    BOOL tt; // BOOL used to test if Create
    Directory was successful
    int x1 = 0; // Counter
    CString tem = ""; // Temporary CString Object
    string temporary="";
    // Before we go to a lot of work.
    // Does the file exist

    fFile = FindFirstFile(dd,&fileinfo);

    // if the file exists and it is a directory
    if(fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
    {
    // Directory Exists close file and return
    FindClose(fFile);
    return TRUE;
    }

    m_arr.RemoveAll();
    int length=dd.GetLength();
    string dir=dd;
    int length1=dir.length();
    // Not really necessary - Just habit
    for(x1=0;x1<length;x1++) // Parse the supplied CString Directory
    String
    {
    if(dd.GetAt(x1) != '\\') // if the Charachter is not a \
    tem += dd.GetAt(x1); // else add character to Temp String
    else
    {
    paths.push_back()
    m_arr.Add(tem); // if the Character is a \ Add the Temp
    String to the CString Array
    tem += "\\"; // Now add the \ to the temp string
    }
    if(x1 == length-1) // If we reached the end of the file add the
    remaining string
    m_arr.Add(tem);
    }


    // Close the file
    FindClose(fFile);
    int size=m_arr.GetSize();
    // Now lets cycle through the String Array and create each directory
    in turn
    for(x1=1;x1<size;x1++)
    {
    tem = m_arr.GetAt(x1);
    tt = CreateDirectory(tem,NULL);

    // If the Directory exists it will return a false
    if(tt)
    SetFileAttributes(tem,FILE_ATTRIBUTE_NORMAL);
    // If we were successful we set the attributes to normal
    }
    m_arr.RemoveAll();
    // Now lets see if the directory was successfully created
    fFile = FindFirstFile(dd,&fileinfo);

    // if the file exists and it is a directory
    if(fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
    {
    // Directory Exists close file and return
    FindClose(fFile);
    return TRUE;
    }
    else
    {
    FindClose(fFile);
    return FALSE;
    }
    }

    */

    BOOL WriteDirectory(string dd)
    {

    HANDLE fFile; // File Handle
    WIN32_FIND_DATA fileinfo; // File Information Structure
    // CString Array to hold Directory Structures
    //vector<string> paths;
    BOOL tt; // BOOL used to test if Create
    Directory was successful
    int x1 = 0; // Counter
    string tem = ""; // Temporary CString Object

    // Before we go to a lot of work.
    // Does the file exist

    fFile = FindFirstFile(dd.c_str(),&fileinfo);

    // if the file exists and it is a directory
    if(fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
    {
    // Directory Exists close file and return
    FindClose(fFile);
    return TRUE;
    }

    //m_arr.RemoveAll();
    int length=dd.length();


    for(x1=0;x1<length;x1++) // Parse the supplied CString
    Directory String
    {
    if(dd.at(x1) != '\\') // if the Charachter is not a \
    tem += dd.at(x1); // else add character to Temp
    String
    else
    {

    tt = CreateDirectory(tem.c_str(),NULL);
    if(tt)
    SetFileAttributes(tem.c_str(),FILE_ATTRIBUTE_NORMAL);

    // if the Character is a \ Add the Temp String to the
    CString Array
    tem += "\\"; // Now add the \ to the temp
    string
    }

    if(x1 == length-1) // If we reached the end of the file
    add the remaining string
    {
    // paths.push_back(tem);
    tt = CreateDirectory(tem.c_str(),NULL);
    if(tt)
    SetFileAttributes(tem.c_str(),FILE_ATTRIBUTE_NORMAL);
    }

    }


    // Close the file
    FindClose(fFile);


    // Now lets see if the directory was successfully created
    fFile = FindFirstFile(dd.c_str(),&fileinfo);

    // if the file exists and it is a directory
    if(fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
    {
    // Directory Exists close file and return
    FindClose(fFile);
    return TRUE;
    }
    else
    {
    FindClose(fFile);
    return FALSE;
    }
    }



    int _tmain(int argc, _TCHAR* argv[])
    {
    //string Fullpath[];
    //_mkdir ();
    int IsDirectoryCreated=WriteDirectory("c:\\puneettest1\\test2");

    if(IsDirectoryCreated)
    {

    cout<<"dir created\n";
    }
    char buffer[3400];
    char *p=buffer;
    int i;

    i=::GetCurrentDirectory(3400,p);
    if(i==0)
    {
    printf("sdasd");
    }


    /*if(IsDirectoryCreated==0)
    {

    }*/

    cout<<p<<endl;
    return 0;
    }



    // recursive directories.cpp : Defines the entry point for the console
    application.
    //

    #include "stdafx.h"
    //#include "WriteDir.h"
    #include<stdio.h>
    #include <afx.h>
    #include <CString>
    #include <direct.h>
    #include <string>
    #include <iostream>
    #include <vector>
    using namespace std;

    /*
    BOOL WriteDirectory(CString dd)
    {


    HANDLE fFile; // File Handle
    WIN32_FIND_DATA fileinfo; // File Information Structure
    CStringArray m_arr; // CString Array to hold Directory
    Structures
    vector<string> paths;
    BOOL tt; // BOOL used to test if Create
    Directory was successful
    int x1 = 0; // Counter
    CString tem = ""; // Temporary CString Object
    string temporary="";
    // Before we go to a lot of work.
    // Does the file exist

    fFile = FindFirstFile(dd,&fileinfo);

    // if the file exists and it is a directory
    if(fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
    {
    // Directory Exists close file and return
    FindClose(fFile);
    return TRUE;
    }

    m_arr.RemoveAll();
    int length=dd.GetLength();
    string dir=dd;
    int length1=dir.length();
    // Not really necessary - Just habit
    for(x1=0;x1<length;x1++) // Parse the supplied CString Directory
    String
    {
    if(dd.GetAt(x1) != '\\') // if the Charachter is not a \
    tem += dd.GetAt(x1); // else add character to Temp String
    else
    {
    paths.push_back()
    m_arr.Add(tem); // if the Character is a \ Add the Temp
    String to the CString Array
    tem += "\\"; // Now add the \ to the temp string
    }
    if(x1 == length-1) // If we reached the end of the file add the
    remaining string
    m_arr.Add(tem);
    }


    // Close the file
    FindClose(fFile);
    int size=m_arr.GetSize();
    // Now lets cycle through the String Array and create each directory
    in turn
    for(x1=1;x1<size;x1++)
    {
    tem = m_arr.GetAt(x1);
    tt = CreateDirectory(tem,NULL);

    // If the Directory exists it will return a false
    if(tt)
    SetFileAttributes(tem,FILE_ATTRIBUTE_NORMAL);
    // If we were successful we set the attributes to normal
    }
    m_arr.RemoveAll();
    // Now lets see if the directory was successfully created
    fFile = FindFirstFile(dd,&fileinfo);

    // if the file exists and it is a directory
    if(fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
    {
    // Directory Exists close file and return
    FindClose(fFile);
    return TRUE;
    }
    else
    {
    FindClose(fFile);
    return FALSE;
    }
    }

    */

    BOOL WriteDirectory(string dd)
    {

    HANDLE fFile; // File Handle
    WIN32_FIND_DATA fileinfo; // File Information Structure
    // CString Array to hold Directory Structures
    //vector<string> paths;
    BOOL tt; // BOOL used to test if Create
    Directory was successful
    int x1 = 0; // Counter
    string tem = ""; // Temporary CString Object

    // Before we go to a lot of work.
    // Does the file exist

    fFile = FindFirstFile(dd.c_str(),&fileinfo);

    // if the file exists and it is a directory
    if(fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
    {
    // Directory Exists close file and return
    FindClose(fFile);
    return TRUE;
    }

    //m_arr.RemoveAll();
    int length=dd.length();


    for(x1=0;x1<length;x1++) // Parse the supplied CString
    Directory String
    {
    if(dd.at(x1) != '\\') // if the Charachter is not a \
    tem += dd.at(x1); // else add character to Temp
    String
    else
    {

    tt = CreateDirectory(tem.c_str(),NULL);
    if(tt)
    SetFileAttributes(tem.c_str(),FILE_ATTRIBUTE_NORMAL);

    // if the Character is a \ Add the Temp String to the
    CString Array
    tem += "\\"; // Now add the \ to the temp
    string
    }

    if(x1 == length-1) // If we reached the end of the file
    add the remaining string
    {
    // paths.push_back(tem);
    tt = CreateDirectory(tem.c_str(),NULL);
    if(tt)
    SetFileAttributes(tem.c_str(),FILE_ATTRIBUTE_NORMAL);
    }

    }


    // Close the file
    FindClose(fFile);


    // Now lets see if the directory was successfully created
    fFile = FindFirstFile(dd.c_str(),&fileinfo);

    // if the file exists and it is a directory
    if(fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
    {
    // Directory Exists close file and return
    FindClose(fFile);
    return TRUE;
    }
    else
    {
    FindClose(fFile);
    return FALSE;
    }
    }



    int _tmain(int argc, _TCHAR* argv[])
    {
    //string Fullpath[];
    //_mkdir ();
    int IsDirectoryCreated=WriteDirectory("c:\\puneettest1\\test2");

    if(IsDirectoryCreated)
    {

    cout<<"dir created\n";
    }
    char buffer[3400];
    char *p=buffer;
    int i;

    i=::GetCurrentDirectory(3400,p);
    if(i==0)
    {
    printf("sdasd");
    }


    /*if(IsDirectoryCreated==0)
    {

    }*/

    cout<<p<<endl;
    return 0;
    }
     
    puneet vyas, Feb 26, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nickolay Kolev

    Optimizing a text statistics function

    Nickolay Kolev, Apr 21, 2004, in forum: Python
    Replies:
    13
    Views:
    573
    Terry Reedy
    Apr 22, 2004
  2. Jack
    Replies:
    2
    Views:
    347
  3. puneet vyas

    optimizing function

    puneet vyas, Feb 26, 2009, in forum: C++
    Replies:
    0
    Views:
    298
    puneet vyas
    Feb 26, 2009
  4. carl

    Optimizing a function?

    carl, Dec 12, 2009, in forum: C++
    Replies:
    10
    Views:
    494
    joseph cook
    Dec 13, 2009
  5. Optimizing pow() function

    , Apr 22, 2013, in forum: C Programming
    Replies:
    45
    Views:
    845
    Seebs
    Apr 26, 2013
Loading...

Share This Page