Thursday, September 20, 2012

Read CSV files very fast

    ifstream fileRead;
    fileRead.open(file.c_str());
    string line;
    int indice = 0;
    string str;
    double d;

    while(fileRead)
              {
                  indice++;
                  vector< double > desc;
                 
                  fileRead > > str;
                  if(str.length() < 2 )
                      {
                            break;
                            cout<<"break";
                      }
 
                  //read from descriptor file
                  const char * c = str.c_str();
                  const char * pch = c;
                  const char * pchAux = c;

                  while (pch!=NULL)
                    {
                      pch=strchr(pchAux,',');
                      if(pch == NULL) break;
                      char cDouble[pch-pchAux+1];
                      strncpy (cDouble, pchAux ,pch-pchAux);
                      cDouble[pch-pchAux]='\0';
                      d = strtod(cDouble, NULL);
                      desc.push_back(d);
                      pchAux = pch+1;
                    }


              }
    fileRead.close();

Wednesday, September 19, 2012

MS SQL Select full content of a varchar column


DECLARE @S varchar(max)
SELECT @S = ''
SELECT @S = @S + '
' + OBJECT_DEFINITION(OBJECT_ID) FROM SYS.PROCEDURES
SELECT @S AS [processing-instruction(x)] FOR XML PATH('')

Monday, August 13, 2012

Access GUI from background thread using C# Forms

using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Threads
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        delegate void SetTextCallback(string text, int value);

        private void Form1_Load(object sender, EventArgs e)
        {
            ThreadTest t = new ThreadTest(this);
            Thread th = new Thread(t.DoWork);
            th.Start();
            //th.Join();
        
        }

        public void Populate(string text, int value)
        {

            if (this.lblMesaj.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(Populate);
                this.Invoke(d, new object[] { text,value });
                this.Invoke(d, new object[] { text,value });

            }
            else
            {
                this.lblMesaj.Text = text;
                this.progressBarFinalize.Value = value;
            }

            lblMesaj.Text = text;
        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }

    class ThreadTest
    {
        public delegate void populateTextBoxDelegate(string text);
        Form1 formMain;

        public ThreadTest(Form1 form)
        {
            formMain = form;
        }


        public void DoWork()
        {
            for (int i = 0; i < 1000; i++)
            {
                formMain.Populate("In progress ....", i/10);
                for (int j = 0; j < 1000; j++)
                {
                    for (int k = 0; k < 100; k++)
                    {
                        double b = Math.Sqrt(923409290423) / Math.Sqrt(423423);
                    }
                }
            }

            formMain.Populate("Thread has finished!",100);

        }
    }

}

Monday, August 6, 2012

Generate Random String c#

private string GeneratePassword()
    {

        Random rng = new Random();
        string _chars =  
                         "ABCDEFGHIJKLMNOPQRSTUVWXYZqwertyuiopplkjhgfdsazxcvbnm1234567890";
        int size = rng.Next(8,12);

        char[] buffer = new char[size];

        for (int i = 0; i < size; i++)
        {
            buffer[i] = _chars[rng.Next(_chars.Length)];
        }
        return new string(buffer);
    }

Friday, June 15, 2012

BackTracking c#

 class Program
    {

        static int[] a;
        static int k;
        static int SIZE = 4;
        static void Main(string[] args)
        {
            a = new int[20];
            bkp();
            System.Console.ReadLine();


        }

        static void bkp()
        {
            k = 1;
            a[k] = 0;
            bool getSuccesor;
            while (k > 0)
            {
                do
                {
                    getSuccesor = HaveSuccesor();
                }
                while (getSuccesor && !IsValid());

                if (getSuccesor)
                {
                   
                    if (IsValidSolution())
                    {
                        ShowSolution();
                    }
                    else
                    {
                        k++;
                        a[k] = 0;
                    }
                }
                else
                {
                    a[k] = 0;
                    k--;
                }

            }
        }

        static bool HaveSuccesor()
        {
            if (a[k] < SIZE)
            {
                a[k]++;
                return true;
            }
            else
                return false;
        }

        public static bool IsValid()
        {
           if (k > 1)
                if (a[k] < a[k - 1]) return false;
            for (int i = 1; i < k; i++)
                if (a[i] == a[k]) return false;
           
            return true;
        }

        public static bool IsValidSolution()
        {
            if(k==SIZE) return true;
            return false;
        }

        static void ShowSolution()
        {
            for (int i = 1; i < = SIZE; i++)
                System.Console.Write(a[i] + " ");
            System.Console.Write(Environment.NewLine);
        }

        static void ShowTowers()
        {
            for (int i = 1; i < = SIZE; i++)
            {
                for (int j = 1; j < = SIZE; j++)
                {
                    if(a[i]==j)
                        System.Console.Write("T");
                    else
                        System.Console.Write("*");
                }
                System.Console.Write(Environment.NewLine);
             }
            System.Console.Write(Environment.NewLine);
          
        }


    }
}

Monday, May 28, 2012

Copy an image from internet:

private void CopyFile(string file)
        {
            WebRequest req = WebRequest.Create(@"http:....." + file);
            req.Proxy = new WebProxy("1.1.1.1", 8080);
            req.Proxy.Credentials = new NetworkCredential(@"user", "pass");
            WebResponse responsePic = req.GetResponse();
            Image webImage = Image.FromStream(responsePic.GetResponseStream()); // Error
            webImage.Save(@"D:\\pictures\" + file);
        }