Post your code snippets here.

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

Descartes

Lifer
Oct 10, 1999
13,968
2
0
We REALLY need a way to format the code

No kidding :(

Another C# snippet. Tis a C# clone of 'whereis' that's distributed with many linux distros.

using System;

namespace whereis
{
/// <summary>
/// Clone of whereis from Linux
/// </summary>
class whereis
{
[STAThread]
static void Main(string[] args)
{
string filename;
if (args.Length != 1)
{
// print usage and exit
usage();
return;
}
else
filename = args[0];

// get the path environment variable
string path = Environment.GetEnvironmentVariable("PATH");
// split the path to get the individual directories
string[] dirs = path.Split(';');
string file;
foreach (string directory in dirs)
{
// directory may/may not end with a trailing backslash so we need to account for that
file = directory.EndsWith("\\") ? directory + filename : directory + "\\" + filename;
if (System.IO.File.Exists(file))
{
// found file -- don't want to break out of loop as it may exist in multiple locations
Console.WriteLine(file);
}
}
}

private static void usage()
{
Console.Error.WriteLine("whereis\n");
Console.Error.WriteLine("\tusage: whereis <filename>");
}
}
}
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Here is a nice one I like. Let's say that I have an int that is either 1,2,3,4,5,6 and depending on its value I have to make a string a char being either a,b,c,d,e,f respectively.
If number = 1 then
letter = "a"
else
If number = 2 then
letter = "b"
else
If number = 3 then
letter = "c"
else
If number = 4 then
letter = "d"
else
If number = 5 then
letter = "e"
else
letter = "f"
End If
End If
End If
End If
End If

An easier way would be...

char = Chr(96 + number)

:D
 

911paramedic

Diamond Member
Jan 7, 2002
9,448
1
76
This is the 404 page I made for my site, so far no complaints from Dell. LMAO
(feel free to use it, but I am not to be held liable)


You can see this script (ok, just html) in action with this link.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">

<html>

<head>
</head>

<body>

<p>
&Acirc;  
<p>
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" width="100%">
<TR>
<TD ALIGN=center><img src="delldude.jpg"></TD>
</TR>
</table>

<a><b><center>Dude, you must have a DELL.
<p>
You have reached our error page, something must have gone wrong with YOUR system...
<p>
Call DELL technical support to find out why your browser is not working correctly.
<p>
Even if you are not on a DELL computer, this is a DELL problem.
<p>
Please call Technical Support: 1-800-624-9896
<p>
 &Acirc; 
<p>
 
<p>
Have a nice day.</center></b><a>


</a></b></center>

</body>
</html>

rolleye.gif


Looks like it got a little garbled, the source is on the page itself ;)
 

911paramedic

Diamond Member
Jan 7, 2002
9,448
1
76
sorry notfred, it will not work on that site. I do not have permission from that server to use my htaccess file to point it there.

That is just my personal space given to me by my ISP and I store things there. Note the name of the directory /mischtmlstorage

You have to follow the link to the page directly on that site. I am waiting for my new account on cyberwings, LOL
 

Mark R

Diamond Member
Oct 9, 1999
8,513
16
81
;-----------------Debounce button pushes----------
;This routine debounces an 8 bit input on Port-B
;Inputs are active low.
;Call this routine whenever you wish to poll the
;input lines. It requires inputs to be active for
;4 consecutive runs before an event is verified.
;Verified outputs are returned in Cx and Cy.
;Both outputs are active high. Cx is high only once
;and remains low until reset by allowing the input
;to return high. Cy remains high as long as the
;input remains low.

Debounce
MOVFW PORTB
XORLW 0xFF
MOVWF Cx ; Cx = ~P
ANDWF C1,F ; C1 = C1 & (~P)
ANDWF C2,F ; C2 = C2 & (~P)
ANDWF Cy,F ; Cy = Cy & (~P)
MOVFW C1
XORLW 0XFF
MOVWF C1 ; C1 = ~C1
ANDWF Cx,F ; Cx = Cx & C1
XORWF C2,F ; C2 = C2 ^ C1
MOVFW C2
ANDWF Cx,F ; Cx = Cx & C2
MOVFW Cy
XORLW 0xFF
ANDWF Cx,F ; Cx = Cx & ~Cy
MOVFW Cx
XORWF Cy,F ; Cy = Cy ^ Cx
RETURN

;--------------------End Debounce-----------------
 

Chaotic42

Lifer
Jun 15, 2001
34,898
2,057
126
I was working on an internet roleplaying system called Rogue. My friends and I loved to roleplay (pencil and paper, made our own system), and when we moved apart we wanted to keep playing. It was for Gnome written in Python. It turns out that a project like OpenRPG or something to that effect was already about 70% done, so I dropped it.

Does that count? :)
 

docmanhattan

Golden Member
Jul 31, 2001
1,332
0
0
This is a script i use to grab a certain file .txt, .pdf, or .tif from an inside machine to the outside webserver as a tempfile, push it out to the user and then delete it.

$image_file = $id;
$base_url = "http://some.url.com/";
//$dir = "messages/";
$dir = "messages/".$custid."/";
$i = 0;
$conn = false;
$file_types = array(".tif",".txt",".pdf");
do {
$path_to_file = $base_url.$dir.$image_file.$file_types[$i];
$file_type = $file_types[$i];
$conn = @fopen($path_to_file,"r"); //open connection
$i++;
//echo "trying $path_to_file ...
";
} while($conn == false && $i < 3);

if ($conn) { //if $conn is successful send GET request

fwrite($conn, "GET /".$dir.$image_file." HTTP/1.0\r\n\r\n");
$tmpFile = tempnam("/tmp","msg_"); //create a temp file in /tmp on localhost with a prefix of "fax_"
$tmpfp = fopen($tmpFile,"w"); //open temp file for writing
while (!feof($conn)) { //go thru each line reading->copying to temp file
$content = fread($conn,4);
fwrite($tmpfp,$content,4);
}
fclose($conn); //close both files
fclose($tmpfp);
$filesize = filesize($tmpFile); //get file size

//echo $path_to_file." is valid!
";
switch($file_type) {
case ".txt":
//echo "TXT";
header("Content-Type: text/plain;");
break;
case ".tif":
//echo "TIF";
header("Content-Type: image/tiff;"); //send file and headers to browser for view/dl-ing
break;
case ".pdf":
//echo "PDF";
header("Content-Type: application/pdf;"); //send PDF header
break;
}

//header("Content-Transfer-Encoding: binary");
$browser = getenv("HTTP_USER_AGENT");
if(strstr($browser,"MSIE 6.0")) {
header("Content-disposition: attachment; filename=$image_file$file_type;");
} else {
header("Content-disposition: inline; filename=$image_file$file_type;");
}
//header("Content-disposition: inline; filename=bobo.tiff;");
header("Content-Length: ".$filesize.";");
readfile($tmpFile) or die("Error Downloading File");
//echo $the_file;
unlink($tmpFile); //delete temp file

} else {
echo "The file requested was not available.";
}
 

DAM

Diamond Member
Jan 10, 2000
6,102
1
76
Here is a CPU controller in verilog.


always @ (posedge clk)
begin
IR=0;
A=0;
X=0;
Md=0;
MARH=0;
MARL=0;
PC=0;
case (state)
init_state:
PC=1;
state=fetch;
fetch:
begin
IR=1;
PC=1;
state=addr1;
end
addr1:
begin
if(opcode_msb==9);
begin
PC = 0;
state=fetch;
end
else PC = 1;
if(opcode_msb==8)
state=stop;
if(opcode_msb==3)
state=data;
if(opcode_msb==4)
begin
Md = 1;
state=output_state;
end
if(opcode_msb==5)
begin
Md = 1;
state=output_state;
end
if(opcode_msb==6)
begin
MARH = 1;
MARL = 1;
state=addx;
end
if(opcode_msb==7)
begin
if (opcode_lsb==6)
A = 1;
else if (opcode_lsb==14)
X = 1;
else
Md = 1;
state=data;
end
if(opcode_msb==10 && (opcode_lsb==0 ||opcode_lsb==4 || opcode_lsb==8 || opcode_lsb==10 ||
opcode_lsb==11))
state=output_state;
else if (opcode_msb==10)
state=fetch;
if(opcode_msb==11)
state=data;
if(opcode_msb==12)
state=addr2;
if(opcode_msb==13)
state=addr2;
if(opcode_msb==14)
state=addx;
if(opcode_msb==15)
state=data;
end
addr2:
begin
if(opcode_msb==12)
state=data;
if(opcode_msb==13)
state=addx;
end
addx:
begin
if(opcode_msb==6)
state=data;
if(opcode_msb==13)
state=data;
if(opcode_msb==14)
state=data;
end

data:
begin
if(opcode_msb==3)
state=rd_mod_wr;
if(opcode_msb==1)
state=rd_mod_wr;
if(opcode_msb==6)
state=rd_mod_wr;
if(opcode_msb==7)
state=rd_mod_wr;
if(opcode_msb > 10 && (opcode_lsb==0 || opcode_lsb==4 || opcode_lsb==8 || opcode_lsb==10 ||
opcode_lsb==11))
state=output_state;
else if (opcode_msb>10)
state=fetch;
end
rd_mod_wr:
begin
if(opcode_msb==3)
state=writeback;
if(opcode_msb==6)
state=writeback;
if(opcode_msb==7)
state=writeback;
end
writeback:
begin
if(opcode_msb==3)
state=fetch;
if(opcode_msb==6)
state=fetch;
if(opcode_msb==7)
state=fetch;
end
output_state:
state=fetch;
stop:
state=stop;
endcase

//////////////
addr1:
begin
if(opcode_msb==9)
PC=0;
else PC=1;
if(opcode_msb==4)
Md=1;
if(opcode_msb==5)
Md=1;
if(opcode_msb==6)
begin
MARH=1;
MARL=1;
end
if(opcode_msb==7)
begin
MARH=1;
MARL=1;
end
if(opcode_msb==10)
begin
if(opcode_lsb==6)
A=1;
else if(opcode_lsb==14)
X=1;
else
Md=1;
end
if(opcode_msb==11 || opcode_msb==3)
begin
MARH=1;
MARL=1;
end
if(opcode_msb==12)
MARH=1;
if(opcode_msb==13)
MARH=1;
if(opcode_msb==14)
begin
MARH=1;
MARL=1;
end
if(opcode_msb==15)
begin
MARH=1;
MARL=1;
end
end
addr2:
begin
PC=1;
MARL=1;
end
addx:
begin
MARL=1;
MARH=1;
end
data:
begin
if(opcode_lsb==6)
A=1;
else if(opcode_lsb==14)
X=1;
else if(opcode_lsb==7);
else Md=1;
end
rd_mod_wr:
A=1;
output_state:
begin
if(opcode_msb==5)
X=1;
else
A=1;
end
stop:
begin
IR=0;
A=0;
X=0;
Md=0;
MARH=0;
MARL=0;
PC=0;
end
endcase
end



dam()
 
Jun 18, 2000
11,209
775
126
/**************************************************************************************
Procedure Name      :      sp_dyn_create_ing_view
Returns                  :      nothing
Uses Tables            :      user_nested_ing_extension and whatever other table gets passed to it
                                    via argument.
Calls Fns            :      none
Called by            :      create_ing_view_all
Description            :      This procedure creates a view dynamically based off of the passed table
                                    and user specified name. It is joined to the material and ingredient id
                                    from user_nested_ing_extensions. These views will be primarily used
                                    for rules.
Created On            :      3/5/02
Created By            :      Mendo
**************************************************************************************/
CREATE OR REPLACE PROCEDURE sp_dyn_create_ing_view
      (      ais_table_name                        VARCHAR2,
            ais_ing_view_name                  VARCHAR2, // NULL if you don't want an ingredient extension view.
            ais_ing_ext_view_name      VARCHAR2) // NULL if you don't want an ingredient view.
AS
      // Local variable instantiation
      ls_col                        VARCHAR2(30);
      ls_concat_cols1      VARCHAR2(4000);
      ls_concat_cols2      VARCHAR2(4000);
      ls_concat_cols3      VARCHAR2(4000);

      ls_table_exists      VARCHAR2(15);

      cursor_handle            INTEGER;
      sql_feedback            INTEGER;

      error_feedback            EXCEPTION;

      // Cursor to retrieve all columns for the passed table.
      CURSOR c_get_columns IS
            SELECT cname
            FROM sys.col
            WHERE LOWER(tname) = LOWER(ais_table_name) AND
                        LOWER(cname) <> 'material_id'
            ORDER BY colno;

      // Retrieve from sys.col to see if the passed table exists.
      CURSOR c_table_exists IS
            SELECT 'TRUE'
            FROM sys.col
            WHERE LOWER(tname) = LOWER(ais_table_name);

BEGIN
      // Must intialize to a ' '. If not, the length() conditional below will always fail.
      ls_concat_cols1 := ' ';
      ls_concat_cols2 := ' ';

      OPEN c_table_exists;
      FETCH c_table_exists INTO ls_table_exists;
      CLOSE c_table_exists;

      // If the table doesn't exist, skip the view creation logic.
      IF ls_table_exists IS NOT NULL THEN

            // Open our cursor and loop to concat all the columns.
            OPEN c_get_columns;
            LOOP
                  FETCH c_get_columns INTO ls_col;
                  EXIT WHEN c_get_columns%notfound;

                  // If we are nearing the end of the length limit for our variable, move onto variable 2,
                  // and then variable 3.
                  // 1) column limit per table is 255
                  // 2) each column name can be 30 characters
                  // 3) 770 chars max for ',x.'
                  // I.E. 255 * 30 + 255 * 3 = 8415
                  IF LENGTH(ls_concat_cols1) < 3969 THEN
                        ls_concat_cols1 := ls_concat_cols1 || ',x.' || ls_col;
                  ELSIF LENGTH(ls_concat_cols2) < 3969 THEN
                        ls_concat_cols2 := ls_concat_cols2 || ',x.' || ls_col;
                  ELSE
                        ls_concat_cols3 := ls_concat_cols3 || ',x.' || ls_col;
                  END IF;
            END LOOP;
            CLOSE c_get_columns;

            // Strip off the comma at the beginning of concat_cols1 if it is not null.
            // Start at char(3) since the beginning will look like: ' ,x.column_name'
            ls_concat_cols1 := SUBSTR(ls_concat_cols1, 3);

            // Only process the view if the user passes an ingredient (non nested) view name.
            IF ais_ing_view_name IS NOT NULL THEN
                  cursor_handle := DBMS_SQL.OPEN_CURSOR;
                  DBMS_SQL.PARSE (cursor_handle,
                        'CREATE OR REPLACE VIEW ' || ais_ing_view_name || ' AS SELECT i.material_id AS material_id, i.ingredient_id AS ingredient_id,' ||
                        ls_concat_cols1 || ls_concat_cols2 || ls_concat_cols3 || ' FROM pmcat_ingredients i,' || ais_table_name ||
                        ' x WHERE i.ingredient_id = x.material_id',
                        DBMS_SQL.NATIVE);
                  DBMS_SQL.CLOSE_CURSOR (cursor_handle);
            END IF;

            // Only process the view if the user passes an ingredient extension view name.
            IF ais_ing_ext_view_name IS NOT NULL THEN
                  cursor_handle := DBMS_SQL.OPEN_CURSOR;
                  DBMS_SQL.PARSE (cursor_handle,
                        'CREATE OR REPLACE VIEW ' || ais_ing_ext_view_name || ' AS SELECT i.material_id AS material_id, i.ingredient_id AS ingredient_id,' ||
                        ls_concat_cols1 || ls_concat_cols2 || ls_concat_cols3 || ' FROM user_nested_ing_extensions i,' || ais_table_name ||
                        ' x WHERE i.ingredient_id = x.material_id',
                        DBMS_SQL.NATIVE);
                  DBMS_SQL.CLOSE_CURSOR (cursor_handle);
            END IF;
      END IF;

// Used only for testing purposes. You can change the error message to display whatever you want.
EXCEPTION
      WHEN error_feedback THEN
            RAISE_APPLICATION_ERROR (-20001, 'Table does not exists. Press Yes to continue to the next views.');

END;^
 

Spamela

Diamond Member
Oct 30, 2000
3,859
0
76
here's a little routine used in my JNI work.

/*
convert Java string to asciiz & copy to buffer supplied.
n.b., buffer must be sufficiently large.
returns buffer supplied
*/
char * JStringToAscii (JNIEnv &Env, const jstring &Name, char *const szBuffer)
{
jboolean IsCopy;
const char *const szName = Env.GetStringUTFChars (Name, &IsCopy);
strcpy (szBuffer, szName);
Env.ReleaseStringUTFChars (Name, szName);
return szBuffer;
}
 

tigerbait

Diamond Member
Jan 8, 2001
5,155
1
0
this is the last program we had for my micro lab (deals with assembly with SDK-386)
considering I'm an EE focusing in power, i don't know why I have to take this
rolleye.gif


.386p
.model large use32

CODE_SEG SEGMENT
ASSUME CS:CODE_SEG
ORG 300H

OFFSET1 EQU 8

BEGIN:
LEA EBX, INT_ROUT
MOV DS:[01A0H], BX
MOV DS:[01A2H], 0008H
MOV DS:[01A4H], 8F00H
MOV CL, 10H
SHR EBX, CL
MOV DS:[01A6H], BX

;ALLOW INTERUPTS

STI

;DISPLAY MESSAGE ROUTINE

LOOP1: LEA EBX, MSG1
MOV SI, 02ECH
MOV AL, DS:[EBX]
CALL DISP
CALL DELAY
JMP LOOP1
INT 3


DISP: MOV BYTE PTR DS:[SI], AL
MOV EDI, 0FF9767H

CALL EDI
INC EBX
INC SI
MOV AL, DS:[EBX]
CMP AL, '$'
JNE DISP
RET
;****************************************************************
;THE DELAY ROUTINE

DELAY: PUSH ECX
MOV ECX, 0FFFFFH
L1: LOOP L1
POP ECX
RET
;*****************************************************************
;THE INTERUPT ROUTINE

INT_ROUT: PUSH ESI
PUSH EBX
MOV SI, 02ECH
ADD SI, OFFSET1;************
LEA EBX, MSG2
MOV AL, DS:[EBX]
CALL DISP
CALL DELAY
MOV SI, 02ECH
LEA EBX, CLEAR
MOV AL, DS:[EBX]
CALL DISP
POP EBX
POP ESI
DB 0CFH
;**************************************************************
;DEFINITIONS AND DECLARATIONS

MSG1 DB 'GEAUX$'
MSG2 DB 'TIGERS$'
CLEAR DB ' $'

CODE_SEG ENDS
END BEGIN