Sunday, May 31, 2009

Read Text File into String Variable

#include

void ChilkatSample(void)
{
HCkFileAccess fac;
const char * fileText;

fac = CkFileAccess_Create();

// Reads the entire contents of a text file into a string variable.
// The 2nd arg specifies the character encoding used for
// the contents of the text file.
fileText = CkFileAccess_readEntireTextFile(fac,"hamlet.xml","utf-8");

printf("%s\n",fileText);

CkFileAccess_Dispose(fac);

}

Send MHTML Email

#include
#include

void ChilkatSample(void)
{
HCkMailMan mailman;
BOOL success;
HCkEmail email;

// The mailman object is used for sending and receiving email.
mailman = CkMailMan_Create();

// Any string argument automatically begins the 30-day trial.

success = CkMailMan_UnlockComponent(mailman,"30-day trial");
if (success != TRUE) {
printf("Component unlock failed\n");
return;
}

// Set the SMTP server.
CkMailMan_putSmtpHost(mailman,"mail.chilkatsoft.com");

// You may or may not need SMTP authentication -- it depends
// on your SMTP server...
CkMailMan_putSmtpUsername(mailman,"mylogin");
CkMailMan_putSmtpPassword(mailman,"mypassword");

// Create a new email object
email = CkEmail_Create();

// Note: .mht files are MIME, and are really no different
// than .eml. The only difference is the file extension
// and the intended use. You can load any .mht into
// an email object by calling LoadEml:
success = CkEmail_LoadEml(email,"myMhtml.mht");
if (success != TRUE) {
printf("%s\n",CkEmail_lastErrorText(email));
return;
}

// To complete the email, add a subject, From address,
// and recipients...
CkEmail_putSubject(email,"This is an MHTML email test");
CkEmail_putFrom(email,"Chilkat ");
CkEmail_AddTo(email,"Chilkat Support","support@chilkatsoft.com");

// Send the MHTML mail.
success = CkMailMan_SendEmail(mailman,email);
if (success != TRUE) {
printf("%s\n",CkMailMan_lastErrorText(mailman));
}
else {
printf("Mail Sent!\n");
}


CkMailMan_Dispose(mailman);
CkEmail_Dispose(email);

}

TCP/IP Socket Connect to Remote Host:Port

#include

void ChilkatSample(void)
{
HCkSocket socket;
BOOL success;
BOOL ssl;
long maxWaitMillisec;
const char * receivedMsg;

socket = CkSocket_Create();

success = CkSocket_UnlockComponent(socket,"Anything for 30-day trial");
if (success != TRUE) {
printf("Failed to unlock component\n");
return;
}

// Connect to port 5555 of localhost.
// The string "localhost" is for testing on a single computer.
// It would typically be replaced with an IP hostname, such
// as "www.chilkatsoft.com".

ssl = FALSE;

maxWaitMillisec = 20000;
success = CkSocket_Connect(socket,"localhost",5555,ssl,maxWaitMillisec);
if (success != TRUE) {
printf("%s\n",CkSocket_lastErrorText(socket));
return;
}

// Set maximum timeouts for reading an writing (in millisec)
CkSocket_putMaxReadIdleMs(socket,10000);
CkSocket_putMaxSendIdleMs(socket,10000);

// The server (in this example) is going to send a "Hello World!"
// message. Read it:

receivedMsg = CkSocket_receiveString(socket);
if (receivedMsg == 0 ) {
printf("%s\n",CkSocket_lastErrorText(socket));
return;
}

// Close the connection with the server
// Wait a max of 20 seconds (20000 millsec)
CkSocket_Close(socket,20000);

printf("%s\n",receivedMsg);

CkSocket_Dispose(socket);

}

To accomplish this in pseudocode we write the following:

procedure bubbleSort( A : list of sortable items ) defined as:
n := length( A )
do
swapped := false
n := n - 1
for each i in 0 to n - 1 inclusive do:
if A[ i ] > A[ i + 1 ] then
swap( A[ i ], A[ i + 1 ] )
swapped := true
end if
end for
while swapped
end procedure

HTML with CSS

/* Generic Selectors */

body {

font-family: Georgia, "Times New Roman", Times, serif;

font-size: 14px;

color: #333333;

background-color: #F9F9F9;

}

p {

width: 80%;

}

li {

list-style-type: none;

line-height: 150%;

list-style-image: url(../images/arrowSmall.gif);

}

h1 {

font-family: Georgia, "Times New Roman", Times, serif;

font-size: 18px;

font-weight: bold;

color: #000000;

}

h2 {

font-family: Georgia, "Times New Roman", Times, serif;

font-size: 16px;

font-weight: bold;

color: #000000;

border-bottom: 1px solid #C6EC8C;

}



/**************** Pseudo classes ****************/

a:link {

color: #00CC00;

text-decoration: underline;

font-weight: bold;

}

li :link {

color: #00CC00;

text-decoration: none;

font-weight: bold;

}

a:visited {

color: #00CC00;

text-decoration: underline;

font-weight: bold;

}

li a:visited {

color: #00CC00;

text-decoration: none;

font-weight: bold;

}

a:hover {

color: rgb(0, 96, 255);

padding-bottom: 5px;

font-weight: bold;

text-decoration: underline;

}

li a:hover {

display: block;

color: rgb(0, 96, 255);

padding-bottom: 5px;

font-weight: bold;

border-bottom-width: 1px;

border-bottom-style: solid;

border-bottom-color: #C6EC8C;

}

a:active {

color: rgb(255, 0, 102);

font-weight: bold;

}

/************************* ID's *************************/

#navigation {

position: absolute;

width: 210px;

height: 600px;

margin: 0;

margin-top: 50px;

border-right: 1px solid #C6EC8C;

font-weight: normal;

}

#centerDoc {

position: absolute;

padding: 0 0 20px 0; /*top right bottom left*/

margin-top: 50px;

margin-left: 235px;

}

Build Google IG like Ajax

public UserPageSetup NewUserVisit( )
{
        var properties = new Dictionary();
        properties.Add("UserName", this._UserName);
        var userSetup = new UserPageSetup();
        properties.Add("UserPageSetup", userSetup);

        WorkflowHelper.ExecuteWorkflow( typeof( NewUserSetupWorkflow ),
                                      properties );

        return userSetup;
}


var db = new DashboardData(ConnectionString);

var newPage = new Page();
newPage.UserId = UserId;
newPage.Title = Title;
newPage.CreatedDate = DateTime.Now;
newPage.LastUpdate = DateTime.Now;

db.Pages.Add(newPage);
db.SubmitChanges();
NewPageId = newPage.ID;

var page = db.Pages.Single( p => p.ID == PageId );
page.Title = PageName;
db.SubmitChanges();
Here only one row is selected.

You can also select a single value:

 Collapse
var UserGuid = (from u in db.AspnetUsers
where u.LoweredUserName == UserName &&
      u.ApplicationId == DatabaseHelper.ApplicationGuid
select u.UserId).Single();
And here's the Projection I was talking about:

 Collapse
var users = from u in db.AspnetUsers
select { UserId = u.UserId, UserName = u.LoweredUserName };

foreach( var user in users )
{
Debug.WriteLine( user.UserName );
}
If you want to do some paging like select 20 rows from 100th rows:

 Collapse
var users = (from u in db.AspnetUsers
select { UserId = u.UserId, UserName = u.LoweredUserName }).Skip(100).Take(20);

foreach( var user in users )
{
Debug.WriteLine( user.UserName );
}
If you are looking for transaction, see how simple it is:

 Collapse
using( TransactionScope ts = new TransactionScope() )
{
List pages = db.Pages.Where( p => p.UserId == oldGuid ).ToList();
foreach( Page page in pages )
page.UserId = newGuid;

// Change setting ownership

UserSetting setting = db.UserSettings.Single( u => u.UserId == oldGuid );
db.UserSettings.Remove(setting);

setting.UserId = newGuid;
db.UserSettings.Add(setting);
db.SubmitChanges();

ts.Complete();
}






public UserPageSetup LoadUserSetup( )
{
        var properties = new Dictionary();
        properties.Add("UserName", this._UserName);
        var userSetup = new UserPageSetup();
        properties.Add("UserPageSetup", userSetup);

        WorkflowHelper.ExecuteWorkflow( typeof( UserVisitWorkflow ),
                              properties );

        return userSetup;

}



334ec662-0e45-4f1c-bf2c-cd3a27014691 Activity: Get User Guid        0.078125
b030692b-5181-41f9-a0c3-69ce309d9806 Activity: Get User Pages       0.0625
b030692b-5181-41f9-a0c3-69ce309d9806 Activity: Get User Setting     0.046875
b030692b-5181-41f9-a0c3-69ce309d9806 Activity: Get Widgets in page: 189 0.0625
334ec662-0e45-4f1c-bf2c-cd3a27014691 Total: Existing user visit     0.265625