?login_element?

Subversion Repositories NedoOS

Rev

Rev 1976 | Rev 1998 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download

  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <oscalls.h>
  6. #include <osfs.h>
  7. #include <intrz80.h>
  8. #include <ctype.h>
  9. #include <tcp.h>
  10. #include <graphic.h>
  11. #include <terminal.c>
  12. struct fileStruct
  13. {
  14.   long picId;
  15.   unsigned int picYear;
  16.   unsigned long totalAmount;
  17.   unsigned char picRating[8];
  18.   unsigned char picName[256];
  19.   unsigned char picType[64];
  20.   unsigned char authorIds[64];
  21.   unsigned char authorTitle[64];
  22.   unsigned char authorRealName[64];
  23.   unsigned char afn[128];
  24.   unsigned char pfn[128];
  25.   unsigned char fileName[128];
  26. } curFileStruct;
  27. unsigned char ver[] = "2.3";
  28. unsigned char netbuf[2048];
  29. unsigned char picture[16384];
  30. unsigned char crlf[2] = {13, 10};
  31. unsigned char status, keypress, verbose, randomPic, slideShow;
  32. struct sockaddr_in targetadr;
  33. struct readstructure readStruct;
  34. unsigned long contLen;
  35. unsigned long count;
  36. unsigned int headlng;
  37. unsigned int slideShowTime = 0;
  38.  
  39. struct packetStruct
  40. {
  41.   unsigned long contLen;
  42.   unsigned int headlng;
  43. } pack;
  44.  
  45. void emptyKeyBuf(void)
  46. {
  47.   do
  48.   {
  49.   } while (_low_level_get() != 0);
  50. }
  51.  
  52. void printHelp(void)
  53. {
  54.   ATRIB(95);
  55.   printf("   GETPIC [%s] zxart.ee picture viewer for nedoNET\n\r", ver);
  56.   ATRIB(33);
  57.   ATRIB(40);
  58.   printf("-------------------------------------------------------\n\r");
  59.   printf(" Управление:\n\r");
  60.   printf(" 'ESC' - выход из программы;\n\r");
  61.   printf(" '<-' или 'B' к последним картинкам;\n\r");
  62.   printf(" '->' или 'Пробел' к более старым картинкам\n\r");
  63.   printf(" 'J' Прыжок на  указанную по счету картинку\n\r");
  64.   printf(" 'I' Просмотр экрана информации о картинках\n\r");
  65.   printf(" 'S' Сохранить картинку на диск в текущую папку\n\r");
  66.   printf(" 'V' не выводить информацию об авторах\n\r");
  67.   printf(" 'R' переход в режим  случайная картинка с рейтингом 4+\n\r");
  68.   printf(" 'A' переход в режим  слайд-шоу\n\r");
  69.   printf(" 'H' Данная справочная информация\n\r");
  70.   printf("-----------------Нажмите любую кнопку------------------\n\r");
  71.   ATRIB(93);
  72.   do
  73.   {
  74.     YIELD();
  75.     keypress = _low_level_get();
  76.   } while (keypress == 0);
  77.   emptyKeyBuf();
  78. }
  79. void delay(unsigned long counter)
  80. {
  81.   unsigned long start, finish;
  82.   counter = counter / 20;
  83.   if (counter < 1)
  84.   {
  85.     counter = 1;
  86.   }
  87.   start = time();
  88.   finish = start + counter;
  89.  
  90.   while (start < finish)
  91.   {
  92.     start = time();
  93.   }
  94. }
  95.  
  96. void errorPrint(unsigned int error)
  97. {
  98.   switch (error)
  99.   {
  100.   case 2:
  101.     printf("02 SHUT_RDWR");
  102.     break;
  103.   case 4:
  104.     printf("04 ERR_INTR");
  105.     break;
  106.   case 23:
  107.     printf("23 ERR_NFILE");
  108.     break;
  109.   case 35:
  110.     printf("35 ERR_EAGAIN");
  111.     break;
  112.   case 37:
  113.     printf("37 ERR_ALREADY");
  114.     break;
  115.   case 38:
  116.     printf("38 ERR_NOTSOCK");
  117.     break;
  118.   case 40:
  119.     printf("40 ERR_EMSGSIZE");
  120.     break;
  121.   case 41:
  122.     printf("41 ERR_PROTOTYPE");
  123.     break;
  124.   case 47:
  125.     printf("47 ERR_AFNOSUPPORT");
  126.     break;
  127.   case 53:
  128.     printf("53 ERR_ECONNABORTED");
  129.     break;
  130.   case 54:
  131.     printf("54 ERR_CONNRESET");
  132.     break;
  133.   case 57:
  134.     printf("57 ERR_NOTCONN");
  135.     break;
  136.   case 65:
  137.     printf("65 ERR_HOSTUNREACH");
  138.     break;
  139.   default:
  140.     printf("%u UNKNOWN ERROR", error);
  141.     break;
  142.   }
  143. }
  144.  
  145. signed char OpenSock(unsigned char family, unsigned char protocol)
  146. {
  147.   signed char socket;
  148.   unsigned int todo;
  149.   todo = OS_NETSOCKET((family << 8) + protocol);
  150.   if (todo > 32767)
  151.   {
  152.     printf("OS_NETSOCKET: [ERROR:");
  153.     errorPrint(todo & 255);
  154.     printf("] Press any key.");
  155.     getchar();
  156.     exit(0);
  157.   }
  158.   else
  159.   {
  160.     socket = ((todo & 65280) >> 8);
  161.     // printf("OS_NETSOCKET: Socket #%d created\n\r", socket);
  162.   }
  163.   return socket;
  164. }
  165.  
  166. signed char netShutDown(signed char socket, unsigned char type)
  167. {
  168.   unsigned int todo;
  169.   todo = OS_NETSHUTDOWN(socket, type);
  170.   if (todo > 32767)
  171.   {
  172.     printf("OS_NETSHUTDOWN: [ERROR:");
  173.     errorPrint(todo & 255);
  174.     printf("] Press any key.");
  175.     return -1;
  176.   }
  177.   else
  178.   {
  179.     // printf("Socket #%d closed.\n\r", socket);
  180.   }
  181.   return 1;
  182. }
  183.  
  184. unsigned char netConnect(signed char socket)
  185. {
  186.   unsigned int todo, retry = 10;
  187.  
  188.   targetadr.family = AF_INET;
  189.   targetadr.porth = 00;
  190.   targetadr.portl = 80;
  191.   targetadr.b1 = 217;
  192.   targetadr.b2 = 146;
  193.   targetadr.b3 = 69;
  194.   targetadr.b4 = 13;
  195.   while (retry > 0)
  196.   {
  197.     todo = OS_NETCONNECT(socket, &targetadr);
  198.  
  199.     if (todo > 32767)
  200.     {
  201.       retry--;
  202.       printf("OS_NETCONNECT [ERROR:");
  203.       errorPrint(todo & 255);
  204.       printf("] [Retry:%u] [Pic:%lu]\r\n", retry, count);
  205.       YIELD();
  206.       netShutDown(socket, 0);
  207.       socket = OpenSock(AF_INET, SOCK_STREAM);
  208.     }
  209.     else
  210.     {
  211.       // printf("OS_NETCONNECT: connection successful, %u\n\r", (todo & 255));
  212.       return 1;
  213.     }
  214.   }
  215.   getchar();
  216.   exit(0);
  217.   return 0;
  218. }
  219.  
  220. unsigned int tcpSend(signed char socket, unsigned int messageadr, unsigned int size)
  221. {
  222.   unsigned char retry = 10;
  223.   unsigned int todo;
  224.   readStruct.socket = socket;
  225.   readStruct.BufAdr = messageadr;
  226.   readStruct.bufsize = size;
  227.   readStruct.protocol = SOCK_STREAM;
  228.   while (retry > 0)
  229.   {
  230.     todo = OS_WIZNETWRITE(&readStruct);
  231.     if (todo > 32767)
  232.     {
  233.       printf("OS_WIZNETWRITE: [ERROR:");
  234.       errorPrint(todo & 255);
  235.       printf("] [Retry:%u] [Pic:%lu]\r\n", retry, count);
  236.       YIELD();
  237.       retry--;
  238.     }
  239.     else
  240.     {
  241.       // printf("OS_WIZNETWRITE: %u bytes written. \n\r", todo);
  242.       return todo;
  243.     }
  244.   }
  245.  
  246.   getchar();
  247.   exit(0);
  248.   return todo;
  249. }
  250.  
  251. unsigned int tcpRead(signed char socket)
  252. {
  253.   unsigned char retry = 10;
  254.   unsigned int todo;
  255.  
  256.   readStruct.socket = socket;
  257.   readStruct.BufAdr = (unsigned int)&netbuf;
  258.   readStruct.bufsize = sizeof(netbuf);
  259.   readStruct.protocol = SOCK_STREAM;
  260.  
  261.   while (retry > 0)
  262.   {
  263.     todo = OS_WIZNETREAD(&readStruct);
  264.  
  265.     if (todo > 32767)
  266.     {
  267.       if ((todo & 255) != ERR_EAGAIN)
  268.       {
  269.         printf("OS_WIZNETREAD: [ERROR:");
  270.         errorPrint(todo & 255);
  271.         printf("] [Retry:%u] [Pic:%lu]\r\n", retry, count);
  272.         YIELD();
  273.         retry--;
  274.       }
  275.     }
  276.     else
  277.     {
  278.       // printf("OS_WIZNETREAD: %u bytes read. \n\r", todo);
  279.       return todo;
  280.     }
  281.   }
  282.   getchar();
  283.   exit(0);
  284.   return todo;
  285. }
  286.  
  287. unsigned int cutHeader(unsigned int todo)
  288. {
  289.   unsigned int q;
  290.   unsigned char *count1;
  291.   count1 = strstr(netbuf, "Content-Length:");
  292.   if (count1 == NULL)
  293.   {
  294.     printf("contLen  not found \r\n");
  295.     contLen = 0;
  296.   }
  297.   else
  298.   {
  299.     contLen = atol(count1 + 15);
  300.     // printf ("Dlinna  soderzhimogo = %lu \n\r", contLen);
  301.   }
  302.  
  303.   count1 = strstr(netbuf, "\r\n\r\n");
  304.   if (count1 == NULL)
  305.   {
  306.     printf("header not found\r\n");
  307.   }
  308.   else
  309.   {
  310.     headlng = ((unsigned int)count1 - (unsigned int)netbuf + 4);
  311.     q = todo - headlng;
  312.     // memcpy(&netbuf, count1 + 4, q);
  313.     // printf("header removed. %u bytes\r\n", headlng);
  314.   }
  315.  
  316.   return q;
  317. }
  318.  
  319. char *str_replace(char *dst, int num, const char *str,
  320.                   const char *orig, const char *rep)
  321. {
  322.   const char *ptr;
  323.   size_t len1 = strlen(orig);
  324.   size_t len2 = strlen(rep);
  325.   char *tmp = dst;
  326.  
  327.   num -= 1;
  328.   while ((ptr = strstr(str, orig)) != NULL)
  329.   {
  330.     num -= (ptr - str) + len2;
  331.     if (num < 1)
  332.       break;
  333.  
  334.     strncpy(dst, str, (size_t)(ptr - str));
  335.     dst += ptr - str;
  336.     strncpy(dst, rep, len2);
  337.     dst += len2;
  338.     str = ptr + len1;
  339.   }
  340.  
  341.   for (; (*dst = *str) && (num > 0); --num)
  342.   {
  343.     ++dst;
  344.     ++str;
  345.   }
  346.   return tmp;
  347. }
  348.  
  349. void fillPicture(signed char socket)
  350. {
  351.   unsigned int todo, w, pPos, headskip;
  352.   headskip = 0;
  353.   pPos = 0;
  354.   while (1)
  355.   {
  356.     headlng = 0;
  357.     todo = tcpRead(socket);
  358.     if (todo == 0)
  359.     {
  360.       break;
  361.     }
  362.     if (headskip == 0)
  363.     {
  364.       headskip = 1;
  365.       todo = cutHeader(todo);
  366.     }
  367.  
  368.     if (pPos + todo > sizeof(picture))
  369.     {
  370.       printf("dataBuffer overrun... %u reached \n\r", pPos + todo);
  371.       break;
  372.     }
  373.  
  374.     for (w = 0; w < todo; w++)
  375.     {
  376.       picture[w + pPos] = netbuf[w + headlng];
  377.     }
  378.     pPos = pPos + todo;
  379.     if (pPos == contLen)
  380.     {
  381.       break;
  382.     }
  383.   }
  384.   netShutDown(socket, 0);
  385. }
  386. void nameRepair(unsigned char *pfn, unsigned int tfnSize)
  387. {
  388.  
  389.   str_replace(pfn, tfnSize, pfn, "\\", "_");
  390.   str_replace(pfn, tfnSize, pfn, "/", "_");
  391.   str_replace(pfn, tfnSize, pfn, ":", "_");
  392.   str_replace(pfn, tfnSize, pfn, "*", "_");
  393.   str_replace(pfn, tfnSize, pfn, "?", "_");
  394.   str_replace(pfn, tfnSize, pfn, "<", "_");
  395.   str_replace(pfn, tfnSize, pfn, ">", "_");
  396.   str_replace(pfn, tfnSize, pfn, "|", "_");
  397.   str_replace(pfn, tfnSize, pfn, " ", "_");
  398.   str_replace(pfn, tfnSize, pfn, "&#039;", "'");
  399.   str_replace(pfn, tfnSize, pfn, "&amp;", "&");
  400.   str_replace(pfn, tfnSize, pfn, "&quot;", "'");
  401.   str_replace(pfn, tfnSize, pfn, "&gt;", ")");
  402.   str_replace(pfn, tfnSize, pfn, "&lt;", "(");
  403.   str_replace(pfn, tfnSize, pfn, "\"", "'");
  404. }
  405.  
  406. void stringRepair(unsigned char *pfn, unsigned int tSize)
  407. {
  408.   str_replace(pfn, tSize, pfn, "&#039;", "'");
  409.   str_replace(pfn, tSize, pfn, "&amp;", "&");
  410.   str_replace(pfn, tSize, pfn, "&gt;", ">");
  411.   str_replace(pfn, tSize, pfn, "&lt;", "<");
  412.   str_replace(pfn, tSize, pfn, "&quot;", "\"");
  413.   str_replace(pfn, tSize, pfn, "\\/", "/");
  414. }
  415.  
  416. unsigned char getPic(unsigned long fileId)
  417. {
  418.   unsigned int todo;
  419.   unsigned char buffer[] = "0000000000";
  420.   signed char socket;
  421.   socket = OpenSock(AF_INET, SOCK_STREAM);
  422.   todo = netConnect(socket);
  423.   sprintf(buffer, "%lu", fileId);
  424.   strcpy(netbuf, "GET /file/id:");
  425.   strcat(netbuf, buffer);
  426.   strcat(netbuf, " HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: User-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)\r\n\r\n\0");
  427.   todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf));
  428.   fillPicture(socket);
  429.   return 0;
  430. }
  431. unsigned char savePic(unsigned long fileId)
  432. {
  433.   FILE *fp2;
  434.   unsigned char afnSize, tfnSize;
  435.   unsigned char fileIdChar[10];
  436.  
  437.   afnSize = sizeof(curFileStruct.afn) - 1;
  438.   tfnSize = sizeof(curFileStruct.pfn) - 1;
  439.  
  440.   strcpy(curFileStruct.afn, curFileStruct.authorTitle);
  441.   nameRepair(curFileStruct.afn, afnSize);
  442.  
  443.   strcpy(curFileStruct.pfn, curFileStruct.picName);
  444.   nameRepair(curFileStruct.pfn, tfnSize);
  445.  
  446.   sprintf(curFileStruct.fileName, "%s-%s-%ld.scr", curFileStruct.afn, curFileStruct.pfn, fileId);
  447.   if (strlen(curFileStruct.fileName) > 62)
  448.   {
  449.     sprintf(fileIdChar, "-%ld", fileId);
  450.     str_replace(curFileStruct.fileName, sizeof(curFileStruct.fileName) - 1, curFileStruct.fileName, fileIdChar, "");
  451.     curFileStruct.fileName[50] = '\0';
  452.     strcat(curFileStruct.fileName, fileIdChar);
  453.     strcat(curFileStruct.fileName, ".scr");
  454.   }
  455.   OS_SETSYSDRV();
  456.   OS_MKDIR("../downloads");        // Create if not exist
  457.   OS_MKDIR("../downloads/getpic"); // Create if not exist
  458.   OS_CHDIR("../downloads/getpic");
  459.   fp2 = OS_CREATEHANDLE(curFileStruct.fileName, 0x80);
  460.   if (((int)fp2) & 0xff)
  461.   {
  462.     printf("%s creating error\r\n", curFileStruct.fileName);
  463.     getchar();
  464.     exit(0);
  465.   }
  466.   OS_WRITEHANDLE(picture, fp2, 6912);
  467.   OS_CLOSEHANDLE(fp2);
  468.   return 0;
  469. }
  470.  
  471. int pos(unsigned char *s, unsigned char *c, unsigned int n, unsigned int startPos)
  472. {
  473.   unsigned int i, j;
  474.   unsigned int lenC, lenS;
  475.  
  476.   for (lenC = 0; c[lenC]; lenC++)
  477.     ;
  478.   for (lenS = 0; s[lenS]; lenS++)
  479.     ;
  480.  
  481.   for (i = startPos; i <= lenS - lenC; i++)
  482.   {
  483.     for (j = 0; s[i + j] == c[j]; j++)
  484.       ;
  485.  
  486.     if (j - lenC == 1 && i == lenS - lenC && !(n - 1))
  487.       return i;
  488.     if (j == lenC)
  489.       if (n - 1)
  490.         n--;
  491.       else
  492.         return i;
  493.   }
  494.   return -1;
  495. }
  496.  
  497. const char *parseJson(unsigned char *property)
  498. {
  499.   unsigned int w, lng, lngp1, findEnd, listPos;
  500.   unsigned char terminator;
  501.   int n;
  502.   n = -1;
  503.   //netbuf[0] = '\0';
  504.   n = pos(picture, property, 1, 0);
  505.   if (n == -1)
  506.   {
  507.     strcpy(netbuf, "-");
  508.     // printf("Property %s not found", property);
  509.     return netbuf;
  510.   }
  511.   lng = n - 1 + strlen(property);
  512.   if (picture[lng] == ':')
  513.   {
  514.     terminator = '\0';
  515.   }
  516.   if (picture[lng] == '\"')
  517.   {
  518.     terminator = '\"';
  519.   }
  520.   if (picture[lng] == '[')
  521.   {
  522.     terminator = ']';
  523.   }
  524.  
  525.   findEnd = 1;
  526.   lngp1 = lng + 1;
  527.  
  528.   while (42)
  529.   {
  530.  
  531.     if ((picture[lngp1 + findEnd] == ','))
  532.     {
  533.       if (terminator == '\0')
  534.       {
  535.         break;
  536.       }
  537.       if ((picture[lng + findEnd] == terminator))
  538.       {
  539.         findEnd--;
  540.         break;
  541.       }
  542.     }
  543.     findEnd++;
  544.   }
  545.   listPos = 0;
  546.   for (w = lngp1; w < findEnd + lngp1; w++)
  547.   {
  548.     netbuf[listPos] = picture[w];
  549.     listPos++;
  550.   }
  551.   netbuf[listPos] = '\0';
  552.   return netbuf;
  553. }
  554. void convert866(void)
  555. {
  556.   unsigned int lng, targetPos, w, q = 0;
  557.   unsigned char buffer[8], one, two;
  558.   unsigned int decVal;
  559.   lng = strlen(netbuf);
  560.   targetPos = lng + 1;
  561.  
  562.   while (q < lng)
  563.   {
  564.     one = netbuf[q];
  565.     two = netbuf[q + 1];
  566.     if (one == 92 && two == 117)
  567.     {
  568.       q = q + 2;
  569.       for (w = 0; w < 4; w++)
  570.       {
  571.         buffer[w] = netbuf[q + w];
  572.       }
  573.       q = q + 4;
  574.       buffer[4] = '\0';
  575.       decVal = (unsigned int)strtol(buffer, NULL, 16);
  576.  
  577.       if (decVal < 1088)
  578.       {
  579.         decVal = decVal - 912;
  580.       }
  581.       if (decVal > 1087)
  582.       {
  583.         decVal = decVal - 864;
  584.       }
  585.       if (decVal == 1025)
  586.       {
  587.         decVal = 240;
  588.       }
  589.       if (decVal == 1105)
  590.       {
  591.         decVal = 241;
  592.       }
  593.       netbuf[targetPos] = decVal;
  594.     }
  595.     else
  596.     {
  597.       netbuf[targetPos] = netbuf[q];
  598.       q++;
  599.     }
  600.     targetPos++;
  601.   }
  602.   netbuf[targetPos] = 0;
  603.  
  604.   for (w = lng + 1; w < targetPos + 1; w++)
  605.   {
  606.     netbuf[w - lng - 1] = netbuf[w];
  607.   }
  608. }
  609.  
  610. long processJson(unsigned long startPos, unsigned char limit, unsigned char queryNum)
  611. {
  612.   unsigned int retry, tSize;
  613.   unsigned int todo;
  614.   unsigned char buffer[] = "000000000";
  615.   unsigned char *count1, socket;
  616.   unsigned char userAgent[] = " HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS; GetPic)\r\n\r\n\0";
  617.  
  618.   switch (queryNum)
  619.   {
  620.   case 0:
  621.     strcpy(netbuf, "GET /api/export:zxPicture/filter:zxPictureType=standard/limit:");
  622.     sprintf(buffer, "%u", limit);
  623.     strcat(netbuf, buffer);
  624.     strcat(netbuf, "/start:");
  625.     sprintf(buffer, "%lu", startPos);
  626.     strcat(netbuf, buffer);
  627.     strcat(netbuf, "/order:date,desc");
  628.     strcat(netbuf, userAgent);
  629.     break;
  630.  
  631.   case 1:
  632.     strcat(netbuf, "GET /api/types:zxPicture/export:zxPicture/language:eng/start:0/limit:1/order:rand/filter:zxPictureMinRating=4;zxPictureType=standard");
  633.     strcat(netbuf, userAgent);
  634.     break;
  635.  
  636.   case 3: // /api/export:author/filter:authorId=2202
  637.     strcat(netbuf, "GET /api/export:author/filter:authorId=");
  638.     sprintf(buffer, "%lu", startPos);
  639.     strcat(netbuf, buffer);
  640.     strcat(netbuf, userAgent);
  641.     break;
  642.  
  643.   case 99: // GET /jsonElementData/elementId:182798
  644.     strcpy(netbuf, "GET /jsonElementData/elementId:");
  645.     sprintf(buffer, "%lu", startPos);
  646.     strcat(netbuf, buffer);
  647.     strcat(netbuf, userAgent);
  648.     break;
  649.   }
  650.   retry = 10;
  651.   while (42)
  652.   {
  653.     socket = OpenSock(AF_INET, SOCK_STREAM);
  654.     netConnect(socket);
  655.     todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf));
  656.     fillPicture(socket);
  657.  
  658.     count1 = strstr(picture, "responseStatus\":\"success");
  659.     if (count1 == NULL)
  660.     {
  661.       retry--;
  662.       ATRIB(91);
  663.       printf("PROCESS JSON: [ERROR: Bad responseStatus.] [Retry:%u] [Pic:%lu]\r\n", retry, startPos);
  664.       YIELD();
  665.       if (retry < 1)
  666.       {
  667.         return -1;
  668.       }
  669.     }
  670.     else
  671.     {
  672.       break;
  673.     }
  674.   }
  675.  
  676.   count1 = strstr(picture, "\"id\":");
  677.   if (count1 == NULL)
  678.   {
  679.     ATRIB(91);
  680.     printf("PROCESS JSON: [ERROR: ID not found.] [Pic:%lu]\r\n", startPos);
  681.     YIELD();
  682.     return -2;
  683.   }
  684.  
  685.   netbuf[0] = '\0';
  686.   if (queryNum < 3)
  687.   {
  688.     parseJson("\"id\":");
  689.     curFileStruct.picId = atol(netbuf);
  690.     parseJson(",\"title\":\"");
  691.     convert866();
  692.     strcpy(curFileStruct.picName, netbuf);
  693.  
  694.     tSize = sizeof(curFileStruct.picName);
  695.     stringRepair(curFileStruct.picName, tSize);
  696.  
  697.     parseJson(",\"type\":\"");
  698.     strcpy(curFileStruct.picType, netbuf);
  699.     parseJson("\"rating\":\"");
  700.     strcpy(curFileStruct.picRating, netbuf);
  701.     parseJson("\"year\":\"");
  702.     curFileStruct.picYear = atoi(netbuf);
  703.     parseJson("\"totalAmount\":");
  704.     curFileStruct.totalAmount = atol(netbuf);
  705.     parseJson("\"authorIds\":[");
  706.     strcpy(curFileStruct.authorIds, netbuf);
  707.   }
  708.   if (queryNum == 99)
  709.   {
  710.     parseJson(",\"title\":\"");
  711.     convert866();
  712.     strcpy(curFileStruct.authorTitle, netbuf);
  713.     parseJson(",\"realName\":\"");
  714.     convert866();
  715.     strcpy(curFileStruct.authorRealName, netbuf);
  716.   }
  717.   return curFileStruct.picId;
  718. }
  719.  
  720. void printData(void)
  721. {
  722.   ATRIB(93);
  723.   printf(" #: ");
  724.   ATRIB(97);
  725.   printf("%lu", count);
  726.   ATRIB(93);
  727.   printf(" ID: ");
  728.   ATRIB(97);
  729.   printf("%lu ", curFileStruct.picId);
  730.   ATRIB(93);
  731.   printf(" Total Pics: ");
  732.   ATRIB(97);
  733.   printf("%lu\r\n", curFileStruct.totalAmount);
  734.   ATRIB(93);
  735.   printf(" Author: ");
  736.   ATRIB(96);
  737.   printf("%s\r\n", curFileStruct.authorTitle);
  738.   ATRIB(93);
  739.   printf(" TITLE: ");
  740.   ATRIB(95);
  741.   printf("%s\r\n", curFileStruct.picName);
  742.   ATRIB(93);
  743.   printf(" RATING: ");
  744.   ATRIB(97);
  745.   printf("%s", curFileStruct.picRating);
  746.   ATRIB(93);
  747.   printf(" YEAR: ");
  748.   ATRIB(97);
  749.   printf("%u\r\n", curFileStruct.picYear);
  750.   ATRIB(93);
  751.   printf(" AuthorsIDs ");
  752.   ATRIB(97);
  753.   printf("%s", curFileStruct.authorIds);
  754.   ATRIB(93);
  755.   printf(" Real name: ");
  756.   ATRIB(97);
  757.   printf("%s\r\n", curFileStruct.authorRealName);
  758.   ATRIB(96);
  759.   printf("\r\n");
  760.   YIELD();
  761. }
  762. void safeKeys(unsigned char keypress)
  763. {
  764.   if (keypress == 27)
  765.   {
  766.     printf("Good bye...\r\n");
  767.     ATRIB(37);
  768.     ATRIB(40);
  769.     exit(0);
  770.   }
  771.  
  772.   if (keypress == 'j' || keypress == 'J')
  773.   {
  774.     printf("Jump to picture:");
  775.     scanf("%lu", &count);
  776.     if (count > curFileStruct.totalAmount - 1)
  777.     {
  778.       count = curFileStruct.totalAmount - 1;
  779.     }
  780.   }
  781.  
  782.   if (keypress == 'v' || keypress == 'V')
  783.   {
  784.     verbose = !verbose;
  785.  
  786.     if (verbose == 0)
  787.     {
  788.       BOX(1, 1, 80, 25, 40);
  789.       AT(1, 1);
  790.     }
  791.   }
  792.  
  793.   if (keypress == 'h' || keypress == 'H')
  794.   {
  795.     printHelp();
  796.   }
  797.  
  798.   if (keypress == 'r' || keypress == 'R')
  799.   {
  800.     randomPic = !randomPic;
  801.  
  802.     if (verbose == 1)
  803.     {
  804.       if (randomPic == 1)
  805.       {
  806.         printf("    Random mode enabled...\r\n");
  807.       }
  808.       else
  809.       {
  810.         printf("    Sequental mode enabled...\r\n");
  811.       }
  812.     }
  813.   }
  814.   if (keypress == 'a' || keypress == 'A')
  815.   {
  816.     slideShow = !slideShow;
  817.     if (slideShow == 1)
  818.     {
  819.       if (verbose == 1)
  820.         printf("    SlideShow mode enabled...\r\n\r\n");
  821.       slideShowTime = 250;
  822.     }
  823.     else
  824.     {
  825.       if (verbose == 1)
  826.         printf("    Manual mode enabled...\r\n\r\n");
  827.       slideShowTime = 0;
  828.     }
  829.   }
  830. }
  831.  
  832. C_task main(void)
  833. {
  834.   unsigned char errno;
  835.   unsigned long ipadress;
  836.   long iddqd, idkfa;
  837.   os_initstdio();
  838.  
  839.   count = 0;
  840.   verbose = 1;
  841.   randomPic = 0;
  842.   slideShow = 0;
  843.  
  844.   BOX(1, 1, 80, 25, 40);
  845.   AT(1, 1);
  846.   printHelp();
  847.   safeKeys(keypress);
  848.  
  849. start:
  850.   emptyKeyBuf();
  851.  
  852.   switch (randomPic)
  853.   {
  854.   case 0:
  855.     iddqd = processJson(count, 1, 0);
  856.     break;
  857.   case 1:
  858.     iddqd = processJson(0, 1, 1);
  859.     break;
  860.   }
  861.  
  862.   if (iddqd < 0)
  863.   {
  864.     count++;
  865.     goto start;
  866.   }
  867.  
  868.   if (verbose == 1)
  869.   {
  870.     idkfa = processJson(atol(curFileStruct.authorIds), 0, 99);
  871.  
  872.     if (idkfa < 0)
  873.     {
  874.       printf(" Cant parse curFileStruct.authorIds = %s \r\n\r\n", curFileStruct.authorIds);
  875.       count++;
  876.       goto start;
  877.     }
  878.  
  879.     printData();
  880.   }
  881.   else
  882.   {
  883.     // ATRIB(97);
  884.     // printf(" Getting picture...\r\n");
  885.   }
  886.   if (!strcmp(curFileStruct.picType, "standard"))
  887.  
  888.   {
  889.     errno = getPic(iddqd);
  890.  
  891.   review:
  892.     keypress = viewScreen6912((unsigned int)&picture, slideShowTime);
  893.     emptyKeyBuf();
  894.   }
  895.   else
  896.   {
  897.     printf("  >>Format %s not supported, skipped \n\r", curFileStruct.picType);
  898.     count++;
  899.     goto start;
  900.   }
  901.  
  902.   ///// Keys only for pictures
  903.   if (keypress == 's' || keypress == 'S')
  904.   {
  905.     savePic(iddqd);
  906.     if (verbose == 1)
  907.       printf("        ID:%lu    TITLE:%s  SAVED\r\n\r\n", curFileStruct.picId, curFileStruct.picName);
  908.     count++;
  909.   }
  910.  
  911.   if (keypress == 248 || keypress == 'b' || keypress == 'B')
  912.   {
  913.     if (count > 0)
  914.     {
  915.       count--;
  916.     }
  917.   }
  918.   if (keypress == 251 || keypress == 32)
  919.   {
  920.     count++;
  921.     goto start;
  922.   }
  923.   if (keypress == 'i' || keypress == 'I')
  924.   {
  925.     do
  926.     {
  927.       YIELD();
  928.     } while (_low_level_get() == 0);
  929.     emptyKeyBuf();
  930.     goto review;
  931.   }
  932.   safeKeys(keypress);
  933.   goto start;
  934. }
  935.